0

I have created the system overlay from the example here.

In the SampleOverlayView.java there is a method for move event like this:

@Override
protected void onTouchEvent_Move(MotionEvent event) {
    info.setText("MOVE\nPOINTERS: " + event.getPointerCount());
}

I wish I could use this to move my overlay. I found the solution for moving my overlay (when I move the view, it does follow my finger movement). The solution is over here

This solution uses, onTouch method, which gives the View and MotionEvent params. But in my case I dont have view object. View can be anything ( as its an overlay).Have only event param.

Any idea of how I can move overlay within my screen?

Thanks in advance

Community
  • 1
  • 1
batman
  • 4,728
  • 8
  • 39
  • 45

1 Answers1

1

Of course you have a View you are already in it. I copied the relevant parts of the guide you are linking to:

public class SampleOverlayView extends OverlayView {

    private TextView info;

    private float x;
    private float y;

    public SampleOverlayView(OverlayService service) {
        super(service, R.layout.overlay, 1);
    }

    public int getGravity() {
        return Gravity.TOP + Gravity.RIGHT;
    }

    @Override
    protected void onInflateView() {
        info = (TextView) this.findViewById(R.id.textview_info);
    }

    @Override
    protected void refreshViews() {
        info.setText("WAITING\nWAITING");
    }

    @Override
    protected void onTouchEvent_Up(MotionEvent event) {w
        info.setText("UP\nPOINTERS: " + event.getPointerCount());
    }

    @Override
    protected void onTouchEvent_Move(MotionEvent event) {
        float newX = event.getX();
        float newY = event.getY();

        float deltaX = newX - this.x;
        float deltaY = newY - this.y;

        // Move this View

        this.x = newX;
        this.y = newY;
    }

    @Override
    protected void onTouchEvent_Press(MotionEvent event) {
        this.x = event.getX();
        this.y = event.getY();
    }

    @Override
    public boolean onTouchEvent_LongPress() {
        info.setText("LONG\nPRESS");
        return true;
    }
}

So you don't need a separate View object because that class IS the View. Just replace any occurrence of view in the other answer with this.

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86