View is not automatically being drawed when overriding dispatchDraw
I'm trying to mirror a LinearLayout. To get this work, I extended LinerLayout to create my own View component.
This is what it looks like:
public class FlipLayout extends LinearLayout implements Runnable {
private boolean isMirroring = true;
private Handler handler;
public FlipLayout(Context context) {
super(context);
this.setWillNotDraw(false);
}
public FlipLayout(Context context, AttributeSet attr) {
super(context, attr);
this.setWillNotDraw(false);
handler = new Handler();
handler.postDelayed(this, 30);
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.save();
if (isMirroring) {
canvas.rotate(180, getWidth() / 2, getHeight() / 2);
}
super.dispatchDraw(canvas);
canvas.restore();
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (isMirroring)
event.setLocation(getWidth() - event.getX(),
getHeight() - event.getY());
return super.dispatchTouchEvent(event);
}
@Override
public void run() {
invalidate();
handler.postDelayed(this, 30);
}
}
That class is working well, but only when implementing the Runnable interface and by calling invalidate() every few milliseconds.
If I only rotate the canvas without invalidating the view, the changes of the child views are not drawn. Now I'm wondering what's the reason for this behaviour and if theres a way to get it working without the Runnable/Handler implementation. If I remove the line canvas.rotate(...) the changes of the 开发者_StackOverflow社区child views are drawn correctly (this is for example a progressbar which is updating itself frequently.)
I hope someone can help! Thanks so much. Chris
It looks normal to me that changing a Gui dynamically would not update the screen. In any Gui framework, they require the programmer to manually request the redraw.
This is because if i change 10 items in my Gui, i don't want to redraw 10 times, but only once at the end. And the framework can't guess when i'm done refactoring the Gui. So i need to explicitely call a refresh method somehow.
Alternatively, you could invalidate you Gui directly when you're done rotating your stuff, and not in a separate thread.
精彩评论