Disable hardware acceleration, backward compatibility

点点圈 提交于 2019-12-07 06:59:57

问题


I have a problem with a function (setLayerType(LAYER_TYPE_NONE, null)) available in api >=11, and my code should run on android 1.6 (API level 4) too. I have tried to use reflection like this:

try {

        Method method = View.class.getMethod("setLayerType", Integer.TYPE, null);
        method.invoke(LAYER_TYPE_NONE, null);
        view.setLayerType(LAYER_TYPE_NONE, null);
    } catch (Throwable e) {
        Log.e("_________________test", "Function not found");
    }

but my app crash at view.setLayerType with java.lang.VerifyError....

Does anybody have any idea how can I workaround this crash and get a backward compatibility with this function in lower level api?

Thanks, Arkde


回答1:


Just remove

view.setLayerType(LAYER_TYPE_NONE, null);

and you should be fine, for security reasons, Java/Android would first verify that it has at least a shot of running a given class before proceeding, and on older Android OS it doesn't know how to execute "view.setLayerType(LAYER_TYPE_NONE, null);", it would throw a hard Error before any code from that class could be run.

Since you've run the code using reflection, you shouldn't need that line of code anyway.




回答2:


This should be useful: http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html

If it really has something to do with hardware acceleration, you could add following to your manifest:

<application android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />
</application>

src: http://developer.android.com/guide/topics/graphics/hardware-accel.html




回答3:


I couldn't get it to work with your code. The problem I found was on the method type parameterization. Also you weren't invoking the method on the view you wish to set hardware acceleration. This was my solution:

public static void setHardwareAccelerated(View view){
    try {
        Method method = View.class.getMethod("setLayerType", int.class, Paint.class);
        method.invoke(view, View.LAYER_TYPE_HARDWARE, null);
    } catch (Exception e) {
        Log.e("RD", "Hardware Acceleration not supported on API " + android.os.Build.VERSION.SDK_INT, e);
    }
}


来源:https://stackoverflow.com/questions/9886772/disable-hardware-acceleration-backward-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!