Android view 的cache


1 View 相关的六个个函数分别如下:
public void setDrawingCacheEnabled (boolean enabled)  API Level1
Enables or disables the drawing cache. When the drawing cache is enabled, the next call to getDrawingCache() or buildDrawingCache() will draw the view in a bitmap. Calling draw(android.graphics.Canvas) will not draw from the cache when the cache is enabled. To benefit from the cache, you must request the drawing cache by calling getDrawingCache() and draw it on screen if the returned bitmap is not null.

Enabling the drawing cache is similar to setting a layer when hardware acceleration is turned off. When hardware acceleration is turned on, enabling the drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitmap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and hardware layers.

This API can be used to manually generate a bitmap copy of this view, by setting the flag to true and calling getDrawingCache().

public Bitmap getDrawingCache ()    API Level1
Calling this method is equivalent to calling getDrawingCache(false)

public Bitmap getDrawingCache (boolean autoScale)   API Level4
Returns the bitmap in which this view drawing is cached. The returned bitmap is null when caching is disabled. If caching is enabled and the cache is not ready, this method will create it. Calling draw(android.graphics.Canvas) will not draw from the cache when the cache is enabled. To benefit from the cache, you must request the drawing cache by calling this method and draw it on screen if the returned bitmap is not null.

Note about auto scaling in compatibility mode: When auto scaling is not enabled, this method will create a bitmap of the same size as this view. Because this bitmap will be drawn scaled by the parent ViewGroup, the result on screen might show scaling artifacts. To avoid such artifacts, you should call this method by setting the auto scaling to true. Doing so, however, will generate a bitmap of a different size than the view. This implies that your application must be able to handle this size.

public void buildDrawingCache ()    API Level1
Calling this method is equivalent to calling buildDrawingCache(false).

public void buildDrawingCache (boolean autoScale)   API Level4
Forces the drawing cache to be built if the drawing cache is invalid.

If you call buildDrawingCache() manually without calling setDrawingCacheEnabled(true), you should cleanup the cache by calling destroyDrawingCache() afterwards.

Note about auto scaling in compatibility mode: When auto scaling is not enabled, this method will create a bitmap of the same size as this view. Because this bitmap will be drawn scaled by the parent ViewGroup, the result on screen might show scaling artifacts. To avoid such artifacts, you should call this method by setting the auto scaling to true. Doing so, however, will generate a bitmap of a different size than the view. This implies that your application must be able to handle this size.

You should avoid calling this method when hardware acceleration is enabled. If you do not need the drawing cache bitmap, calling this method will increase memory usage and cause the view to be rendered in software once, thus negatively impacting performance.

public void destroyDrawingCache ()  API Level1
Frees the resources used by the drawing cache. If you call buildDrawingCache() manually without calling setDrawingCacheEnabled(true), you should cleanup the cache with this method afterwards.

首先要获得view的cache, 通过setDrawingCacheEnabled(true)方法开启cache, 然后调用getDrawingCache()方法就可以获得view的cache 图片了。如果调用setDrawingCacheEnabled(false),系统也会自动把原来的cache销毁掉;

注意:buildDrawingCache()方法可以不用调用,原因在于getDrawingCache方法时,如果view的cache没有建立,会自动调用buildDrawingCache方法以生成cache.

如果要更新cache, 必须要调用destroyDrawingCache方法把旧的cache销毁,这样后面才能建立新cache,具体的方法:
1)destoryDrawingCache()
2)setDrawingCacheEnabled(false)
都可以。

获取view 的cache demo:
View view = view.setDrawingCacheEnabled(true)
Bitmap bp = view.getDrawingCache();

2 ViewGroup 涉及到cache的两个方法
protected void setChildrenDrawingCacheEnabled (boolean enabled)
Enables or disables the drawing cache for each child of this view group.

protected void setChildrenDrawnWithCacheEnabled (boolean enabled)
Tells the ViewGroup to draw its children using their drawing cache. This property is ignored when isAlwaysDrawnWithCacheEnabled() is true. A child's drawing cache will be used only if it has been enabled. Subclasses should call this method to start and stop using the drawing cache when they perform performance sensitive operations, like scrolling or animating.

setChildrenDrawingCacheEnabled方法,可以使viewgroup里的所有子view开启cache;而setChildrenDrawWithCacheEnabled使在绘制子view时,如果子view开启了cache, 则使用它的cache进行绘制,从而节省了绘制时间;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值