How to invoke drag event of a custom view inside SurfaceView?
Asked Answered
R

1

1

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.

Responsiveness answered 5/12, 2016 at 12:37 Comment(3)
SurfaceView is not a ViewGroup, hence cannot hold any child views. You can only draw things on a surface view's canvas. For example, you can draw a rectangle on touch x,y.Odontology
Yes, you are right. I have a view that i'm drawing to screen above SurfaceView using it's canvas. But this view(white circle with border) is not receiving any events. Neither touch nor drag events are propagated to this custom view. I want to drag this view when i touch on it. How can i achieve that?Responsiveness
I have the view inside custom SurfaceView class. If i dispatch event to view from SurfaceView's 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
B
1

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));
Betteann answered 15/11, 2018 at 9:11 Comment(1)
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.