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. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. Android监听Home键和最近任务
  5. Android学习笔记(10)————Android的Listview详解1(ArrayAdapte
  6. android Gide加载webp图片方法
  7. Android(安卓)原生SQLite数据库操作实战
  8. Flutter 与 Android(安卓)相互调用、传递参数
  9. Android(安卓)Battery一些信息获取方法

随机推荐

  1. Android软件框架的搭建笔记
  2. 2010.11.16———android Camera 拍照的
  3. android下访问sd卡和网络的权限
  4. Android在开机时自动启动一个应用程序
  5. Android(安卓)mms 发送流程
  6. Basic4android:多功能的Android应用软件快
  7. Android获得当前系统时间、星期几、周几
  8. debug.keystore过期问题
  9. android studio3.6.3编译Telegram androi
  10. Android(安卓)Texting(2)Testing Fundament