daftpuppy
daftpuppy

Reputation: 41

getDrawingCache() returned Bitmap not being updated

I have a custom View that will manage hundreds of discrete user-defined sequential drawing events. Rather than maintaining a collection of all of the individual text, line, shape updates and then redrawing them all during each onDraw, I grab a Bitmap of the canvas at the end of each onDraw and then start the next onDraw with that Bitmap. A description of my problem follows this snippet:

public class TestView extends View implements OnTouchListener {
    private Paint mPaint;
    private Bitmap mPrevCanvas;
    private int mTouchCount = 0;

    float mX = 50f;
    float mY = 50f;

    public TestView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);      
        this.setOnTouchListener(this);
        this.setDrawingCacheEnabled(true);

        mPaint = new Paint();
        mPaint.setTextSize(30f);
    }

    @Override
    protected void onDraw(Canvas canvas) {   

        if (mTouchCount == 0) {
            canvas.drawText("Touch screen to begin", 50f, 100f, mPaint);
        } else {
            if (mPrevCanvas != null) {
                canvas.drawBitmap(mPrevCanvas, 0, 0, mPaint);
            }

            canvas.drawText(Integer.toString(mTouchCount), mX, mY, mPaint);
            mPrevCanvas = getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
        }
    }

    public boolean onTouch(View arg0, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            mX = event.getX();
            mY = event.getY();
            mTouchCount += 1;
            invalidate();
        }
        return true;
    }
}

It seems to be working okay for the first touch event, but then mPrevCanvas never gets updated again. In reviewing the previous questions related to getDrawingCache(), only getDrawingCache is not updated seemed relevant, but based on that user's self-discovered answer, that apparently wasn't the same problem.

When running this custom view, touching the screen displays a "1" at the position you touch. It then captures a Bitmap of the canvas with that "1" in it. Subsequent touches redisplay that Bitmap stored in mPrevCanvas (showing the "1" again at the same position), and then the new number representing the current touch event (e.g., "2", "3", etc.) Since I'm refreshing mPrevCanvas at the end of each onDraw, I expect each onDraw to begin by displaying a Bitmap containing the results of all of the previous touch events... but for some reason the mPrevCanvas Bitmap is never updated to include that anything except that initial event ("1").

I've (a) verified that isDrawingCacheEnabled() is still true during each pass through onDraw; (b) tried throwing in destroyDrawingCache() and buildDrawingCache() to no avail; (c) forced mPrevCanvas to null before the call to getDrawingCache() to make sure it's really getting updated; and (d) searched through my copy of O'Reilly's Java in a Nutshell on the hunch that maybe I have a java headspace problem rather than an Android API problem.

Q1: Why will getDrawingCache() only return a Bitmap containing that first call to canvas.drawText, but never with the results of any of the subsequent drawText calls?

Q2: Given that I'm doing it this way for resource efficiency, should I be using some other design pattern anyway?

Upvotes: 4

Views: 1597

Answers (1)

Mazhar
Mazhar

Reputation: 376

Even its too late to reply for this, I thought it could help somebody searching for this to correct answer.

same question here (with reply)

Upvotes: 2

Related Questions