开发者

Android wait for UI thread to invalidate

I am having problems redrawing the view in a painting app. When the touch ends, I call touch_up() to make the path, invalidate() to draw it, and storeView() to push the current view on the stack. However, the view is not redrawn at all (until the next touch, giving it a twitchy effect). If I delete the call to storeView() it does redraw right away.

I understand from here that the issue is invalidate() does not necessarily happen immediately and I think I need to create a new thread or handler to wait for it. Despite reading the dev pages, I don't fully understand how to implement that.

Is that really what I need to do? How can I make storeView() wait until the view has been redrawn? I've fought with this a while, an answer would make my day. Thanks!

Here's what I currently do.

Action_up:

case MotionEven开发者_StackOverflow中文版t.ACTION_UP:
    touch_up();
    invalidate();
    storeView();
    break;

Touch_up:

private void touch_up() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    mPath.reset();
}

storeView:

public void storeView(){
    historyCount++;
    if(historyCount > historySize) historyCount = 6;    //We don't want more than 6
    history.add(Bitmap.createBitmap(myView.getDrawingCache()),historyCount);
}


When the touch ends, I call touch_up() to make the path, invalidate() to draw it, and storeView() to push the current view on the stack.

Why is storeView() holding an entire bitmap? Why aren't you using storing commands that you can replay (and possibly reverse)? You are taking up gobs and gobs of RAM this way.

I understand from here that the issue is invalidate() does not necessarily happen immediately

Correct.

and I think I need to create a new thread or handler to wait for it.

No, if you are going to stick with your save-the-whole-bitmap implementation, you need to make sure that storeView() is called after invalidate() completes. Wrapping the call to storeView() in a Runnable and scheduling that Runnable via a call to post() on myView should suffice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜