拖拽删除较旧的Android不准确

时间:2012-07-12 18:31:05

标签: android button view drag-and-drop gesturedetector

所以我试图制作一个“磁性诗歌”型应用程序。用户将在屏幕上移动各种Button小部件。我使用的是Button小部件,因为它最接近磁铁的外观,但我对其他选项持开放态度。

物体没有用手指正确移动。它们很接近,但它们与我的手指并不完全一致。我的手机上的X坐标看起来很好,但Y corrdinate已关闭。这可能是由标题栏引起的吗?

private final static int DRAGGING_OFF = 0;
private final static int DRAGGING_ON = 1;

private int dragStatus;

private GestureDetector mGestureDetector; 
private int mOffsetX;
private int mOffsetY;
private LayoutParams mOldParams;
private RelativeLayout.LayoutParams lp;

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(!mGestureDetector.onTouchEvent(event)) {
        if(event.getAction() == MotionEvent.ACTION_MOVE) {
            if(dragStatus == DRAGGING_ON) {
                Log.e(VIEW_LOG_TAG, "Dragging! RAW -- " + event.getRawX() + " : " + event.getRawY() + " NOT RAW -- " + event.getX() + " : " + event.getY());
                int rawX, rawY;
                int finalX, finalY;

                rawX = (int) event.getRawX();
                rawY = (int) event.getRawY();

                finalX = rawX - mOffsetX;
                finalY = rawY - mOffsetY;

                lp.setMargins(finalX, finalY, 0, 0);
                this.setLayoutParams(lp);
            }

            return true;
        } else if(event.getAction() == MotionEvent.ACTION_UP) {
            if(dragStatus == DRAGGING_ON) {
                dragStatus = DRAGGING_OFF;
                Log.e(VIEW_LOG_TAG, "Stopped dragging!");
            }

            return true;
        } else {
            return super.onTouchEvent(event);
        }
    } else {
        return true;
    }
}

private final GestureDetector.SimpleOnGestureListener mListener = new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onDown(MotionEvent e) {          
        int[] location = new int[2];
        Log.e(VIEW_LOG_TAG, "Down! RAW -- " + e.getRawX() + " : " + e.getRawY() + " NOT RAW -- " + e.getX() + " : " + e.getY());
        dragStatus = DRAGGING_ON;

        // Sets the current location of the View in the int[] passed to it; x then y.
        getLocationInWindow(location);
        Log.e(VIEW_LOG_TAG, "Down location: " + location[0] + " " + location[1]);

        mOffsetX = (int) e.getRawX() - location[0];
        mOffsetY = (int) e.getRawY() - location[1];
        mOldParams = getLayoutParams();

        return true;
    }
};

1 个答案:

答案 0 :(得分:0)

我认为问题在于你没有获得当前触摸点Y的DELTA和缓存的最后一个触点y。你需要这样的东西:

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final TextView textView = (TextView) findViewById(R.id.text);
        final ImageView image = (ImageView) findViewById(R.id.image);
        matrix.setTranslate(1f, 1f);
        image.setImageMatrix(matrix);
        image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                matrix.getValues(m); //copy matrix values into array

                PointF currentXYTouchPoint = new PointF(event.getX(), event.getY());

                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: //start of pressed gesture
                        lastXYTouchPoint.set(event.getX(), event.getY()); //save the last touchpoint
                        mode = DRAG;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        //calculate the change in finger position
                        float deltaX = currentXYTouchPoint.x - lastXYTouchPoint.x;
                        float deltaY = currentXYTouchPoint.y - lastXYTouchPoint.y;
                        matrix.postTranslate(deltaX, deltaY); //move the entire image by the deltas
                        image.setImageMatrix(matrix);

                        // save this last starting touchpoint
                        lastXYTouchPoint.set(currentXYTouchPoint.x, currentXYTouchPoint.y);
                        break;
                }

                textView.setText("TouchPoint started at " + currentXYTouchPoint.x + ", " + currentXYTouchPoint.y + " & the matrix is now " + Arrays.toString(m));
                return true;
            }
        });

    }

根据自己的目的进行调整,但基本的想法就在那里。