Closed GoogleCodeExporter closed 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
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
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
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
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
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
Original issue reported on code.google.com by
papp.robert.s
on 7 Dec 2014 at 9:28