Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[转]Android如何关闭硬件加速 #7

Open
GcsSloop opened this issue Jan 29, 2016 · 7 comments
Open

[转]Android如何关闭硬件加速 #7

GcsSloop opened this issue Jan 29, 2016 · 7 comments
Labels

Comments

@GcsSloop
Copy link
Owner

@GcsSloop GcsSloop commented Jan 29, 2016

原文地址:http://developer.android.com/guide/topics/graphics/hardware-accel.html
译文地址:http://blog.chenming.info/blog/2012/09/18/android-hardware-accel/

硬件加速

Android 3.0 (API level 11), 开始支持
所有的View 的canvas都会使用GPU,但是硬件的加速会占有一定的RAM。
在API >= 14上,默认是开启的,如果你的应用只是标准的View和Drawable,全局都打开硬件加速,是不会有任何问题的。
然而,硬件加速并不支持所有的2D画图的操作,这时开着它,可能会影响到你的自定义控件或者绘画,出现异常等行为,
所以android对于硬件加速提供了可选性

如果你的应用执行了自定义的绘画,可以通过在真机上测试开启硬件加速查找问题

硬件加速的级别

Application

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

Activity

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

Window

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

View

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Note: 你可以关闭View级别的硬件加速,但是你不能在View级别开启硬件加速,因为它还依赖其他的设置

两种获取是否支持硬件加速的方式

View.isHardwareAccelerated()   //returns true if the View is attached to a hardware accelerated window.
Canvas.isHardwareAccelerated() //returns true if the Canvas is hardware accelerated

如果必须进行这样的验证,建议你在draw的代码块中使用:Canvas.isHardwareAccelerated(),因为如果一个View被attach到一个硬件加速的Window上,
即使没有硬件加速的Canvas,它也是可以被绘制的。比如:将一个View以bitmap的形式进行缓存

@GcsSloop GcsSloop changed the title Android的硬件加速及可能导致的问题 [转]Android的硬件加速及可能导致的问题 Jul 25, 2016
@GcsSloop GcsSloop changed the title [转]Android的硬件加速及可能导致的问题 [转]Android如何关闭硬件加速 Nov 21, 2016
@iDragonfly2050
Copy link

@iDragonfly2050 iDragonfly2050 commented Feb 19, 2017

后面的部分硬是没看懂。。。

@jv-lee
Copy link

@jv-lee jv-lee commented Mar 14, 2017

这个确实没有遇到过。

@xiaweizi
Copy link

@xiaweizi xiaweizi commented May 26, 2017

抢的一批

@chrisLeeCreate
Copy link

@chrisLeeCreate chrisLeeCreate commented Jun 30, 2017

一如既往的稳健

@nesger
Copy link

@nesger nesger commented Oct 17, 2017

老哥稳,细节问题都考虑到了

@zidanpiaoguo
Copy link

@zidanpiaoguo zidanpiaoguo commented Mar 5, 2018

我感觉还是最好不要关闭硬件加速器,关闭之后,toolbar不显示阴影,cardview不显示圆角边框,circleimageview 不显示边框阴影。有很多后遗症。

@po1arbear
Copy link

@po1arbear po1arbear commented Nov 19, 2019

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 之后,myview.isHardwareAccelerated())依然返回true,关闭无效啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants
You can’t perform that action at this time.