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

﹥>﹥吖頭↗ 提交于 2019-12-24 06:47:16

问题


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.


回答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));


来源:https://stackoverflow.com/questions/40974471/how-to-invoke-drag-event-of-a-custom-view-inside-surfaceview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!