1

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

Amit
  • 11
  • 2
  • why you don't use `v.invalidate()` method to redraw? – Rahmat Waisi Jul 03 '16 at 10:48
  • @mehd azizi i tryed ivalidate on view which i get in ontouch method after animation,but its not working .It just draging whole view to new position,it is not redrawing like map – Amit Jul 03 '16 at 10:51

1 Answers1

1

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;
}
Community
  • 1
  • 1
mehd azizi
  • 594
  • 1
  • 5
  • 16
  • do you change the background picture or somethings like that in ACTION_MOVE ? – mehd azizi Jul 03 '16 at 10:56
  • no nothing I am changing its position only by animate – Amit Jul 03 '16 at 11:02
  • if you want to show a big picture and move around you have to add an image view in scroll View . like this : http://stackoverflow.com/questions/4990682/images-in-scrollview-in-android – mehd azizi Jul 03 '16 at 11:07