如何只在边缘拖动任何视图?

时间:2017-05-19 12:23:15

标签: android

现在我正试图在边缘上拖动ImageButton我已经使用了教程但是实现了这个:

想要在Edges中拖动

拖拽Everywhere - 我的输出

这是我的代码:

imageButton.setOnTouchListener(new View.OnTouchListener() {
        PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
        PointF StartPT = new PointF(); // Record Start Position of 'img'

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            int eid = event.getAction();
            switch (eid)
            {
                case MotionEvent.ACTION_MOVE :
                    PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
                    imageButton.setX((int)(StartPT.x+mv.x));
                    imageButton.setY((int)(StartPT.y+mv.y));
                    StartPT = new PointF( imageButton.getX(), imageButton.getY() );
                    break;
                case MotionEvent.ACTION_DOWN :
                    DownPT.x = event.getX();
                    DownPT.y = event.getY();
                    StartPT = new PointF( imageButton.getX(), imageButton.getY() );
                    break;
                case MotionEvent.ACTION_UP :
                    // Nothing have to do
                    break;
                default :
                    break;
            }
            return true;
        }
    });

1 个答案:

答案 0 :(得分:0)

我一直在工作,因为我看到了你的问题,最后得到答案...... 我是这样实现的: 首先设置DisplayMetrics,将您的图像按钮粘贴在屏幕上,否则它会离开它:

DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    final int height = displayMetrics.heightPixels-106;

并在您的图片视图中为仅在边缘拖动的Touchlistner:

mMapView.setOnTouchListener(new View.OnTouchListener(){
        PointF DownPT = new PointF();
        PointF StartPT = new PointF();
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int eid = event.getAction();
            switch (eid)
            {
                case MotionEvent.ACTION_MOVE :
                    PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
                    if((int)(StartPT.y+mv.y)<=0){
                        mMapView.setY(0);
                        StartPT = new PointF(mMapView.getX(), mMapView.getY());
                    }else if((int)(StartPT.y+mv.y)>=height){
                        mMapView.setY(height);
                        StartPT = new PointF(mMapView.getX(), mMapView.getY());
                    }
                    else {
                        mMapView.setY((int) (StartPT.y + mv.y));
                        StartPT = new PointF(mMapView.getX(), mMapView.getY());
                    }
                    break;
                case MotionEvent.ACTION_DOWN :
                    DownPT.y = event.getY();
                    StartPT = new PointF(0, mMapView.getY());
                    break;
                case MotionEvent.ACTION_UP :
                    DownPT.y = event.getY();
                    StartPT = new PointF(0, mMapView.getY());
                    break;
                default :
                    break;
            }
            return true;
        }
    });

此外,这种方法用于记录float x和float y,但首先导入:

import android.graphics.PointF;

 public class PointF
{
    public float x = 0;
    public float y = 0;
    public PointF(){};
    public PointF( float _x, float _y ){ x = _x; y = _y; }
}