Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

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

Open
#7 8 comments 133 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
25/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Stale
Tech stack
android, java

Research direction

Review the translated Android hardware-acceleration content in the issue together with the linked original and translation URLs. Check how AndroidNote organizes similar notes; because no target file or requested change is specified, confirm the intended documentation scope before considering the work done.

Written by the indexing model from the issue text.

Description

常见问题

原文地址: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的形式进行缓存

Dominant language
Java
Stars
9.3k
Forks
2.1k
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from GcsSloop/AndroidNote

All issues in GcsSloop/AndroidNote

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.