Android 绘图缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3046766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:28:10  来源:igfitidea点击:

Android drawing cache

androidview

提问by Seva Alekseyev

Please explain how does the drawing cache work in Android. I'm implementing a custom View subclass. I want my drawing to be cached by the system. In the View constructor, I call

请解释绘图缓存在 Android 中是如何工作的。我正在实现一个自定义 View 子类。我希望我的绘图被系统缓存。在视图构造函数中,我调用

setDrawingCacheEnabled(true);

Then in the draw(Canvas c), I do:

然后在抽奖(Canvas c)中,我这样做:

    Bitmap cac = getDrawingCache();
    if(cac != null)
    {
        c.drawBitmap(cac, 0, 0, new Paint());
        return;
    }

Yet the getDrawingCache()returns null to me. My draw()is not called neither from setDrawingCacheEnabled(), nor from getDrawingCache(). Please, what am I doing wrong?

然而,getDrawingCache()返回 null 给我。Mydraw()既不是 fromsetDrawingCacheEnabled()也不是 from getDrawingCache()。请问,我做错了什么?

采纳答案by Seva Alekseyev

There's a hard limit on drawing cache size, available via the ViewConfiguration class.. My view is larger than allowed for caching.

绘图缓存大小有硬性限制,可通过 ViewConfiguration 类获得。我的视图大于缓存允许的大小。

FYI, the sources of the View class are available via the SDK Manager for some (not all) Android versions.

仅供参考,View 类的源可通过某些(并非所有)Android 版本的 SDK 管理器获得。

回答by Gavin Saunders

Hopefully this explains it.

希望这能解释它。

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) {
        ...
    }

}

Example code explained.

示例代码解释。

  1. In the setSomeProperty() method call setDrawingCacheEnabled(false) to clear the cache and force a redraw by calling invalidate().
  2. Call setDrawingCacheEnabled(true) in the onDraw method after drawing to the canvas.
  3. Optionally place a log statement in the onDraw method to confirm it is only called once each time you call the setSomeProperty() method. Be sure to remove the log call once confirmed as this will become a performance issue.
  1. 在 setSomeProperty() 方法中调用 setDrawingCacheEnabled(false) 以清除缓存并通过调用 invalidate() 强制重绘。
  2. 绘制到画布后,在 onDraw 方法中调用 setDrawingCacheEnabled(true)。
  3. 可以选择在 onDraw 方法中放置一条日志语句,以确认每次调用 setSomeProperty() 方法时它只调用一次。请务必在确认后删除日志调用,因为这将成为性能问题。