almosr / android-svg-code-render

Convert SVG files into Java source and use it in your Android app
Apache License 2.0
24 stars 3 forks source link

FAQ promotes extremely bad backward-compatibility practice #48

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
https://code.google.com/p/androidsvg/wiki/FAQ#My_SVG_doesn't_render_if_I_try_to_
do_Canvas.drawPicture()_o

What is the expected output? What do you see instead?
Instead of the horrible reflection, please replace it with one of the following 
solutions (or both).
Note how the two code blocks are simpler and shorter (even together) than the 
current one.

If the project is using support-v4:

import android.support.v4.view.ViewCompat;
ViewCompat.setLayerType(view, ViewCompat.LAYER_TYPE_SOFTWARE, null);

If it's a plain android project targeting API 11+ (which is where the method 
was added, not API 13):

import android.os.Build.*;
@TargetApi(VERSION_CODES.HONEYCOMB)
public static void setSoftwareLayerType(View view) {
    if (VERSION_CODES.HONEYCOMB <= VERSION.SDK_INT) {
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    } // else: no layer support, so no hardware support to disable
}

Original issue reported on code.google.com by papp.robert.s on 7 Dec 2014 at 9:28

GoogleCodeExporter commented 9 years ago
I just noticed another point against the current solution: you can simply use 
getField(...).getInt(null) to get a static field's value, no need to create a 
new View(getContext()).

Original comment by papp.robert.s on 7 Dec 2014 at 9:31

GoogleCodeExporter commented 9 years ago
Thanks for that.  I didn't know that ViewCompat had that method.  However I 
should have updated that answer with the other new way of doing it.

I've updated the answer now.

I've kept the old-style way in the FAQ just in case anyone is still using an 
old build environment.

Thanks again Robert.

Original comment by paul.leb...@gmail.com on 8 Dec 2014 at 11:35

GoogleCodeExporter commented 9 years ago
Ahaa, I didn't know the @TargetApi was added in 16 :)
Still the SDK_INT was added in API 4 and I don't think anyone is targeting less 
than that... so even if @TargetApi is not available because of old target the 
Build.VERSION method works.

@TargetApi is only used to silence lint, like @SuppressLint("NewApi") which is 
API 16 too. I guess before API 16 the lint.xml was used to make it shut up when 
SDK_INT is used to make stuff compatible.

Original comment by papp.robert.s on 8 Dec 2014 at 11:55

GoogleCodeExporter commented 9 years ago
Yeah you are right.  That section made no sense.  I think I have fixed it now. 
:)

Original comment by paul.leb...@gmail.com on 8 Dec 2014 at 12:26

GoogleCodeExporter commented 9 years ago
Some of the numbers and terminology is still strange, here're the paragraphs 
fixed to my best knowledge:

If you are using the v4 support library in your project (also included with 
latest v7 appcompat library), compatibility will be taken care of for you:

If you are targeting API 4 or later, you can use the constants in the 
android.os.Build class to prevent setLayerType() being invoked when it is not 
available on the user's device.

Please note that the @TargetApi annotation was added in API 16, so if your 
target is between API 11 and API 15 and your minSdkVersion is less than API 11 
you'll have to find another way to suppress the lint warning about 
setLayerType() not being available.

In almost all cases you will be able to use one of the first two approaches. We 
recommend you use one of them if you can. However some people may be forced for 
some reason to use Java reflection to call the setLayerType() method if it is 
available.

private void setSoftwareLayerType() {
    try {
        Method setLayerTypeMethod = View.class.getMethod("setLayerType", Integer.TYPE, Paint.class);
        int LAYER_TYPE_SOFTWARE = View.class.getField("LAYER_TYPE_SOFTWARE").getInt(null);
        setLayerTypeMethod.invoke(this, LAYER_TYPE_SOFTWARE, null);
    } catch (NoSuchMethodException e) {
        // Android platform too old, no setLayerType
    } catch (NoSuchFieldException e) {
        // Android platform too old, no LAYER_TYPE_SOFTWARE
    } catch (Exception e) {
        Log.w("MySVGView", "Unexpected failure calling setLayerType", e);
    }
}

Original comment by papp.robert.s on 8 Dec 2014 at 12:50

GoogleCodeExporter commented 9 years ago
Thanks.  I have incorporated your suggestions.

> If you are targeting API 4 or later, you can use the constants...

Yes, the constants are available in API 4, but you can't use 
View.setLayerType() till API 11.

Original comment by paul.leb...@gmail.com on 8 Dec 2014 at 1:29