Amit
Amit

Reputation: 11

Redraw view on MotionEvent.ACTION_MOVE

I have created on basic View which has Red color in background.I am using that view in my activity.I have setOnTouch listner to same view. on touch events i am moving that view.

Before touch event it looks like this

After MotionEvent.ACTION_MOVE it look like this

After motion event i want view to be redrawn.I don't want white portion to be shown.

it should redraw it like google map does on moving map in any direction.

following code is of my basic custom view

public class CustomVIew extends View {
    public CustomVIew(Context context) {
        super(context);
    }

    public CustomVIew(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomVIew(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.RED);
    }
}

This is my OnTouch method implementation

@Override
    public boolean onTouch(View v, MotionEvent event) {

 switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                _xDelta = v.getX() - event.getRawX();
                _yDelta = v.getY() - event.getRawY();
                Log.d(TAG, "onTouch: X:" + _xDelta + " Y:" + _yDelta);
                break;

            case MotionEvent.ACTION_MOVE:
                v.animate()
                        .x(event.getRawX() + _xDelta)
                        .y(event.getRawY() + _yDelta)
                        .setDuration(10)
                        .start();
                break;
            default:
                return false;
        }

        return true;
}

Thanx in advance

Upvotes: 1

Views: 378

Answers (1)

mehd azizi
mehd azizi

Reputation: 594

you may need to invalidate the view .

this helps: When it's necessary to execute invalidate() on a View?

@Override
public boolean onTouch(View v, MotionEvent event) {

 switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:

            _xDelta = v.getX() - event.getRawX();
            _yDelta = v.getY() - event.getRawY();
            Log.d(TAG, "onTouch: X:" + _xDelta + " Y:" + _yDelta);
            break;

        case MotionEvent.ACTION_MOVE:
             //do something like change the picture
            v.invalidate();
            break;
        default:
            return false;
    }

    return true;
}

Upvotes: 1

Related Questions