开发 Android 的时候,在许多情况下会使用到 View 的 getDrawingCache 方法来取得 View 目前显示出来的样子(DrawingCache),虽然算是一个还蛮方便的方法,但是这个方法却有着许多的缺陷,它不但性能极差,内部实作方式和回传的结果随着 Android API 版本不同还有很大的差异。最严重的一点是,getDrawingCache 常常会请你吃 null。在这篇文章中,将会探讨为什么 getDrawingCache 会回传 null,以及解决这个问题的方法。
setDrawingCacheEnabled、buildDrawingCache 和 getDrawingCache 彼此间的关系
在 Android SDK 上,所有的 View 都拥有 setDrawingCacheEnabled、buildDrawingCache 和 getDrawingCache 这三种方法,这三种看起来颇为相似的方法到底有什么样的情感纠葛呢?让我们继续看下去。
大部分的 View,如果没有使用 setDrawingCacheEnabled 方法来激活 View 的 DrawingCache 功能的话,那预设是不激活的。激活 DrawingCache 的话,使用到 getDrawingCache 方法时,会先自动去调用 buildDrawingCache 方法创建 DrawingCache,再将结果回传;不激活 DrawingCache 的话,使用 getDrawingCache 方法时,会回传上一次使用 buildDrawingCache 方法所产生出来的结果,如果在此之前都没有使用过 buildDrawingCache 来创建 DrawingCache 的话,那么 getDrawingCache 就会回传 null,当然,就算没有激活 DrawingCache,也还是可以事先使用 buildDrawingCache 来创建 DrawingCache,避免 getDrawingCache 传回 null。
以下两种方式都可以取得 View 最新的 DrawingCache:
...
view.setDrawingCacheEnabled(true);
...
Bitmap drawingCache = view.getDrawingCache();
...
...
view.buildDrawingCache();
Bitmap drawingCache = view.getDrawingCache();
...
激活 DrawingCache 之后,就不要再调用 buildDrawingCache 方法了,以下写法应该避免,会造成两次创建 DrawingCache:
...
view.setDrawingCacheEnabled(true);
...
view.buildDrawingCache();
Bitmap drawingCache = view.getDrawingCache();
...
在使用 buildDrawingCache 方法创建 DrawingCache 的同时,Android SDK 预设会将上次的 DrawingCache 给 recycle 掉,因此不必自作聪明在使用 buildDrawingCache 方法之前,或是在 DrawingCache 激活的状态下使用 getDrawingCache 方法之前,把前次的 DrawingCache 给手动 recycle,如果真的这样做的话,将会出现重复 recycle 的 RuntimeException。所以下面的写法也应该要避免:
...
if(view.getDrawingCache() != null){
view.getDrawingCache().recycle();;
}
view.buildDrawingCache();
Bitmap drawingCache = view.getDrawingCache();
...
为什么 getDrawingCache 性能会很差?
文章一开始便提到 getDrawingCache 的性能极差,这是为什么呢?就像上面提到的,一旦激活了 DrawingCache 之后,每次调用 getDrawingCache,都会自动重新调用 buildDrawingCache 方法来创建新的 DrawingCache,但是在大部分的情况下,一个 View 的状态是不会任意改变的,如果此时将 getDrawingCache 使用在 onDraw 之类的事件中,将会使性能非常地低落。再来就是随着 Android API 层级愈来愈高,DrawingCache 的品质也跟着愈设愈高,在绝大部分的情况下都是使用最占用内存且运算速度最慢的 ARGB_8888,过去 View 所提供的 setDrawingCacheQuality 方法已经没有实质作用了,不管设置哪种品质,都还是会使用 ARGB_8888。
为什么 getDrawingCache 常常传回 null?
如果遭遇 getDrawingCache 方法传回 null 的状况,请先确定 View 的 DrawingCache 有无激活,如果没有激活,再确定有没有调用过 buildDrawingCache 方法。如果以上都确认过没问题,再看看使用 getDrawingCache 时机是否有误,View 一定要经过 measure 和 layout 的过程才可以被绘制出来。以下面的例子为例,即使没有直接将 View 加入至 Activity 或是 Fragment 的 RootView 中,也可以取得 DrawingCache:
...
ViewGroup viewGroup = new FrameLayout(getContext());
ImageView image = new ImageView(getContext());
image.setImageResource(R.drawable.ic_launcher);
viewGroup.addView(image);
viewGroup.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
viewGroup.layout(0, 0, viewGroup.getMeasuredWidth(), viewGroup.getMeasuredHeight());
viewGroup.buildDrawingCache();
Bitmap bitmap = viewGroup.getDrawingCache();
...
如果很确定 View 已经有经过 measure 和 layout 且也有调用 buildDrawingCache(无论自动或手动)方法了,但是 getDrawingCache 却还是传回 null,那就是因为要绘制的 DrawingCache 太大张了,超过 Android 系统预设的 drawingCacheSize,所以系统就不给画啦!当遇到这种状况时,就只能放弃使用 DrawingCache 了,而事实上,这种状况还蛮常发生的。
Android 系统预设的 DrawingCache 大小上限,在不同的设备上有不同的设置,甚至有可能差了好几倍,如果要查看数值的话可以在 Android SDK 中使用以下方式来取得 drawingCacheSize:
ViewConfiguration.get(context).getScaledMaximumDrawingCacheSize();
不使用 getDrawingCache 的替代方法
文章看到这里大家应该都可以了解到 getDrawingCache 实在是异常难用,既然如此,那就完全放弃 Android 内置的 DrawingCache 机制吧!实际上,要自己实作出类似的功能并不会太难,大致上的概念是自行创建出 Bitmap,并且使用一个 Canvas 在这个 Bitmap 上作画,只要调用 View 的 draw 方法,将自己的 Canvas 作为参数传入,结果就会出现在 Bitmap 上了。可写成如以下的程序:
public Bitmap getMagicDrawingCache(View view) {
Bitmap bitmap = (Bitmap) view.getTag(cacheBitmapKey);
Boolean dirty = (Boolean) view.getTag(cacheBitmapDirtyKey);
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
if (bitmap == null || bitmap.getWidth() != viewWidth || bitmap.getHeight() != viewHeight) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
bitmap = Bitmap.createBitmap(viewWidth, viewHeight, bitmap_quality);
view.setTag(cacheBitmapKey, bitmap);
dirty = true;
}
if (dirty == true || !quick_cache) {
bitmap.eraseColor(color_background);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
view.setTag(cacheBitmapDirtyKey, false);
}
return bitmap;
}
其中,cacheBitmapKey 和 cacheBitmapDirtyKey 为相异的资源 ID(例如在项目资源中声明的 R.id 数值),分别用来指定 View 的 Tag ID。cacheBitmapKey 的位置会存放使用这个方法创建出来的 DrawingCache;cacheBitmapDirtyKey 的位置会存放这个 View 的 DrawingCache 是否已经脏掉了(dirty)而需要调用 View 的 draw 方法重新绘制。DrawingCache 所用的 Bitmap 只在没有 Bitmap 对象或是 Bitmap 对象的大小和 View 的大小不合的时候才重新创建,在创建新的 Bitmap 前会先将先前的 Bitmap 进行 recycle,新的 Bitmap 对象的参考会再被存入至 View 的 Tag 中。quick_cache 若设置为 false,则不论 DrawingCache 是否 dirty,都进行重绘,只有在 View 常常变化的时候才需要这样做。bitmap_quality 可以设置为 Bitmap.Config.RGB_565 或是 Bitmap.Config.ARGB_8888,Bitmap.Config.ARGB_4444 已经随着 Android API 层级愈来愈高而慢慢被禁用了,在实际应用上,RGB_565 虽然没有透明层,但是性能会比 ARGB_8888 还要好很多。
如果要加入 View 不在 Activity 或是 Fragment 的 RootView 中的判断的话,可以写成以下程序:
public Bitmap getMagicDrawingCache(View view) {
Bitmap bitmap = (Bitmap) view.getTag(cacheBitmapKey);
Boolean dirty = (Boolean) view.getTag(cacheBitmapDirtyKey);
if (view.getWidth() + view.getHeight() == 0) {
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
}
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
if (bitmap == null || bitmap.getWidth() != viewWidth || bitmap.getHeight() != viewHeight) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
bitmap = Bitmap.createBitmap(viewWidth, viewHeight, bitmap_quality);
view.setTag(cacheBitmapKey, bitmap);
dirty = true;
}
if (dirty == true || !quick_cache) {
bitmap.eraseColor(color_background);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
view.setTag(cacheBitmapDirtyKey, false);
}
return bitmap;
}
DrawingCache 实际应用
DrawingCache 的用途非常广,可以用来制作 Android SDK 所没有内置的 View,如以下时间轴样式的 View。

