0

In this example, I try to invalidate only a rectangle of my custom view, but the canvas passed to onDraw is not clipped. Output always shows that the clip bounds contain the whole canvas. What's the reason for this?

public class ClippingActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new ClippingView(this));
    }
}

class ClippingView extends View {

    Rect r = new Rect();

    public ClippingView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.getClipBounds(r);
        Log.d("ClippingView","onDraw: " + r);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        invalidate(0,0,400,400);
        return true;
    }
}
moonring
  • 5
  • 5
  • check if the canvas is hardware accelerated, if so read about clipping when hwd acc – pskink Apr 13 '14 at 11:31
  • @psking: but I can reproduce this behavior on my Nexus 5 where canvas.isHardwareAccelerated() returns true and on my emulator (API v19) where canvas.isHardwareAccelerated() returns false. – moonring Apr 13 '14 at 15:42
  • Try to touch the view and move a few times and you will see the proper clip bounds 400x400 – pskink Apr 13 '14 at 16:02

1 Answers1

0

I just found a closely related question: Partial invalidation in custom Android view with hardware acceleration

Romain Guy answered it:

Partial redraw works just fine, only the specified region of the screen will get redrawn. What it won't do however is change the clip bounds on the Canvas. All the drawing operations will be recorded but only the ones intersecting with the dirty region will actually be executed.

Community
  • 1
  • 1
moonring
  • 5
  • 5