How can i drag a custom view inside the SurfaceView. Touch events are not dispatched to child custom view from SurfaceView or child view's onTouchEvent is not called.
How to invoke drag event of a custom view inside SurfaceView?
Try this code
public class MoveViewTouchListener implements View.OnTouchListener
{
private GestureDetector mGestureDetector;
private View mView;
public MoveViewTouchListener(View view)
{
mGestureDetector = new GestureDetector(view.getContext(), mGestureListener);
mView = view;
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
return mGestureDetector.onTouchEvent(event);
}
private GestureDetector.OnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener()
{
private float mMotionDownX, mMotionDownY;
@Override
public boolean onDown(MotionEvent e)
{
mMotionDownX = e.getRawX() - mView.getTranslationX();
mMotionDownY = e.getRawY() - mView.getTranslationY();
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
mView.setTranslationX(e2.getRawX() - mMotionDownX);
mView.setTranslationY(e2.getRawY() - mMotionDownY);
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
if (g) {
if (groupselecteditems.contains(mView)) {
mView.setBackgroundColor(Color.TRANSPARENT);
groupselecteditems.remove(mView);
} else {
mView.setBackgroundResource(border);
groupselecteditems.add((MyTextView) mView);
}
} else {
toucheditem = mView;
xcountry = mView.getX();
ycountry = mView.getY();
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
if (g) {
if (groupselecteditems.size() > 0) {
editGroup();
}
} else {
changtextfeatures((TextView) mView);
}
}
};
}
Call this class with your view
view.setOnTouchListener(new MoveViewTouchListener(view));
Please reserve the use of quote blocks (the yellow rectangle triggered by
>
Markdown) for things that are actually quotes. A quote is something said by somebody or something else, e.g. in a speech, a manual excerpt, etc. Use ordinary paragraph text instead. –
Cottingham © 2022 - 2024 — McMap. All rights reserved.
public boolean dispatchTouchEvent(MotionEvent event) { dragCircle.dispatchTouchEvent(event); return true; }
DragCircleView instance gets the event but it get's wherever the SurfaceView is touched, i tried to match event.getX and getY with circle view's x and y's, but it didn't work. – Responsiveness