请解释一下绘图缓存在Android中是如何工作的.我正在实现一个自定义View子类.我希望系统缓存我的绘图.在View构造函数中,我调用
setDrawingCacheEnabled(true);
Run Code Online (Sandbox Code Playgroud)
然后在绘图(Canvas c)中,我做:
Bitmap cac = getDrawingCache();
if(cac != null)
{
c.drawBitmap(cac, 0, 0, new Paint());
return;
}
Run Code Online (Sandbox Code Playgroud)
然而,getDrawingCache()回报对我来说无效.我draw()既不是来自setDrawingCacheEnabled(),也不是来自getDrawingCache().拜托,我做错了什么?
绘制缓存大小有一个硬性限制,可通过ViewConfiguration类获得.我的视图大于允许缓存.
仅供参考,View类的来源可通过SDK Manager获取,用于某些(并非所有)Android版本.
小智 6
希望这能解释它.
public class YourCustomView extends View {
private String mSomeProperty;
public YourCustomView(Context context) {
super(context);
}
public YourCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public YourCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setSomeProperty(String value) {
mSomeProperty = value;
setDrawingCacheEnabled(false); // clear the cache here
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// specific draw logic here
setDrawingCacheEnabled(true); // cache
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
示例代码说明.
| 归档时间: |
|
| 查看次数: |
25726 次 |
| 最近记录: |