Android的硬件加速及可能导致的问题

http://blog.chenming.info/blog/2012/09/18/android-hardware-accel/


Android Drawing Models

When hardware acceleration is enabled, the Android framework utilizes a new drawing model that utilizes display lists torender your application to the screen. To fully understand display lists and how they might affect your application, itis useful to understand how Android draws views without hardware acceleration as well. The following sections describethe software-based and hardware-accelerated drawing models.

Software-based drawing model

In the software drawing model, views are drawn with the following two steps:

  1. Invalidate the hierarchy
  2. Draw the hierarchy

Whenever an application needs to update a part of its UI, it invokes invalidate() (or one of its variants) on any viewthat has changed content. The invalidation messages are propagated all the way up the view hierarchy to compute theregions of the screen that need to be redrawn (the dirty region). The Android system then draws any view in thehierarchy that intersects with the dirty region. Unfortunately, there are two drawbacks to this drawing model:

  • First, this model requires execution of a lot of code on every draw pass. For example, if your application callsinvalidate() on a button and that button sits on top of another view, the Android system redraws the view even though ithasn’t changed.
  • The second issue is that the drawing model can hide bugs in your application. Since the Android system redraws viewswhen they intersect the dirty region, a view whose content you changed might be redrawn even though invalidate() was notcalled on it. When this happens, you are relying on another view being invalidated to obtain the proper behavior. Thisbehavior can change every time you modify your application. Because of this, you should always call invalidate() on yourcustom views whenever you modify data or state that affects the view’s drawing code.

Note: Android views automatically call invalidate() when their properties change, such as the background color or thetext in a TextView.

Hardware accelerated drawing model

The Android system still uses invalidate() and draw() to request screen updates and to render views, but handles theactual drawing differently. Instead of executing the drawing commands immediately, the Android system records theminside display lists, which contain the output of the view hierarchy’s drawing code. Another optimization is that theAndroid system only needs to record and update display lists for views marked dirty by an invalidate() call. Views thathave not been invalidated can be redrawn simply by re-issuing the previously recorded display list. The new drawingmodel contains three stages:

  1. Invalidate the hierarchy
  2. Record and update display lists
  3. Draw the display lists

With this model, you cannot rely on a view intersecting the dirty region to have its draw() method executed. To ensurethat the Android system records a view’s display list, you must call invalidate(). Forgetting to do so causes a view tolook the same even after changing it, which is an easier bug to find if it happens.

Using display lists also benefits animation performance because setting specific properties, such as alpha or rotation,does not require invalidating the targeted view (it is done automatically). This optimization also applies to views withdisplay lists (any view when your application is hardware accelerated.) For example, assume there is a LinearLayout thatcontains a ListView above a Button. The display list for the LinearLayout looks like this:

  • DrawDisplayList(ListView)
  • DrawDisplayList(Button)

Assume now that you want to change the ListView’s opacity. After invoking setAlpha(0.5f) on the ListView, the displaylist now contains this:

  • SaveLayerAlpha(0.5)
  • DrawDisplayList(ListView)
  • Restore
  • DrawDisplayList(Button)

The complex drawing code of ListView was not executed. Instead, the system only updated the display list of the muchsimpler LinearLayout. In an application without hardware acceleration enabled, the drawing code of both the list and itsparent are executed again.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值