E
E
Eugene2017-05-03 20:10:56
Android
Eugene, 2017-05-03 20:10:56

Why doesn't invalidate(rect) work?

Why does the following code redraw the entire canvas and not the Rect specified?

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(rect != null) {
            canvas.drawRect(rect, paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                float posX = event.getX();
                float posY = event.getY();

                int size = 50;
                int x = (int) Math.floor(posX / size) * size;
                int y = (int) Math.floor(posY / size) * size;

                rect = new Rect(x, y, x + size, y + size);
                invalidate(rect);
                break;
        }
        return true;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-05-04
@zagayevskiy

In my opinion, you need to add canvas.clipRect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question