您的位置:首页 > 移动开发 > Android开发

在android studio开发中,关于绘制GifView异常,硬件加速问题的解决办法

2016-09-22 16:07 302 查看
Hardware Acceleration硬件加速

关于绘制GifView异常:



09-22 16:09:50.769 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.779 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.779 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.779 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.789 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.789 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.789 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.799 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.799 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.xxx.gifview.MainActivity.access$super
'android.view.SearchEvent', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.809 10394-10394/com.example.xxx.gifview
4000
E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.xxx.gifview.MainActivity.access$super
09-22 16:09:50.859 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
09-22 16:09:50.889 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.view.ViewOutlineProvider', referenced from method com.example.xxx.gifview.view.GifView.access$super
09-22 16:09:50.889 10394-10394/com.example.xxx.gifview E/dalvikvm: Could not find class 'android.animation.StateListAnimator', referenced from method


我的处理方式是:

public GifView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
resources = context.getResources();
//android 4.0硬件加
if (Build.VERSION.SDK_INT >=       Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

// 从描述文件中读出gif的值,创建出Movie实例
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifView);
int resourceId = array.getResourceId(R.styleable.GifView_src, -1);
setGifResource(resourceId);
array.recycle();
}


Beginning in Android 3.0 (API level 11), the Android 2D rendering pipeline is designed to better support hardware acceleration. Hardware acceleration carries out all drawing operations that are performed on a View’s canvas using the GPU. Because of the increased resources required to enable hardware acceleration, your app will consume more RAM.

根据官网翻译可得:从Android 3.0(API级别11)开始,Android 2 d渲染管道是为了更好地支持硬件加速。硬件加速进行的所有绘图操作上执行一个视图使用GPU的画布。因为增加的资源需要启用硬件加速,你的应用将会消耗更多的内存。


The easiest way to enable hardware acceleration is to turn it on globally for your entire application. If your application uses only standard views and Drawables, turning it on globally should not cause any adverse drawing effects. However, because hardware acceleration is not supported for all of the 2D drawing operations, turning it on might affect some of your applications that use custom views or drawing calls. Problems usually manifest themselves as invisible elements, exceptions, or wrongly rendered pixels. To remedy this, Android gives you the option to enable or disable hardware acceleration at the following levels:

- Application
- Activity
- Window
- View

> 启用硬件加速的最简单方法是将全局整个应用程序。如果您的应用程序只使用标准视图和画板,把它在全局范围内不应造成任何不良影响。然而,由于不支持硬件加速的2D 绘图操作,将其在可能会影响你的一些使用自定义视图或绘制调用的应用程序。问题通常表现为无形的元素、异常或错误地显示像素。为了补救,Android给你选择启用或禁用硬件加速在以下级别:
1.Application
2. Activity
3. Window
4. View

----------
Controlling Hardware Acceleration


Application level

In your Android manifest file, add the following attribute to the tag to enable hardware acceleration for your entire application:

应用程序级别
在你的Android清单文件中,添加以下属性的<应用>标记为您的整个应用程序启用硬件加速:
<application android:hardwareAccelerated="true" ...>


Activity level

If your application does not behave properly with hardware acceleration turned on globally, you can control it for individual activities as well. To enable or disable hardware acceleration at the activity level, you can use the android:hardwareAccelerated attribute for the element. The following example enables hardware acceleration for the entire application but disables it for one activity:

`

如果您的应用程序不正确的行为和硬件加速全球打开,你可以控制它对个人Activity。启用或禁用硬件加速在活动级别,您可以使用android:hardwareAccelerated 元素的属性。下面的例子启用硬件加速整个应用程序,但禁用这一活动:

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


Window level

If you need even more fine-grained control, you can enable hardware acceleration for a given window with the following code:

如果你需要更准确的控制,您可以启用硬件加速对于一个给定的窗口下面的代码:

getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);


View level

You can disable hardware acceleration for an individual view at runtime with the following code:

你可以禁用硬件加速单个视图在运行时使用以下代码:


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
{
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐