1

我有这段代码可以在屏幕中放置我触摸的视图。但是视图并不完全遵循我手指触摸的点。

我可以改善这种行为吗?

像拖放 API 一样工作,但使用 onTouchEvent?

谢谢大家!

 @Override
 public boolean onTouchEvent(final MotionEvent event) {

            final float x =   event.getX(); 
            final float y =   event.getY();
            moveView(x, y);
            return super.onTouchEvent(event);

        }

        private void moveView(float x, float y) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
            params.leftMargin = Math.round(x);
            params.topMargin = Math.round(y);   
            selectedView.setLayoutParams(params); 

        }
4

3 回答 3

1
  // These matrices will be used to scale points of the image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();


// The 3 states (events) which the user is trying to perform   
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
//private static final float MAX_ZOOM = 5f;
int mode = NONE;

// these PointF objects are used to record the point(s) the user is touching
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
float[] lastEvent = null; 
float degrees = 0f;
float newRot = 0f;



OnTouchListener touchAction = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        //perfrom zoom & drag & rotate operations on touch of imageview
        dragZoomRotate((ImageView)v, event);
        return true; 

    }
};

private void dragZoomRotate(ImageView view, MotionEvent event){ 

    view.setScaleType(ImageView.ScaleType.MATRIX);  

    // Handle touch events here...

    switch (event.getAction() & MotionEvent.ACTION_MASK)   
    {
    case MotionEvent.ACTION_DOWN:
        savedMatrix.set(matrix);
        start.set(event.getX() , event.getY() );
        mode = DRAG;
        lastEvent = null;
        break;
    case MotionEvent.ACTION_POINTER_DOWN:   // first and second finger down
        oldDist = spacing(event);         
        savedMatrix.set(matrix);
        midPoint(mid, event);
        mode = ZOOM;

        lastEvent = new float[4];
        lastEvent[0] = event.getX(0);
        lastEvent[1] = event.getX(1);
        lastEvent[2] = event.getY(0);
        lastEvent[3] = event.getY(1);
        degrees = rotation(event);
        break;
    case MotionEvent.ACTION_UP: // first finger lifted
    case MotionEvent.ACTION_POINTER_UP:   // second finger lifted
        mode = NONE;
        lastEvent = null;

        break;
    case MotionEvent.ACTION_MOVE:    
        if (mode == DRAG) { 
            // ...
            matrix.set(savedMatrix);

            matrix.postTranslate(event.getX() - start.x , event.getY() - start.y );

        }
        else if (mode == ZOOM && event.getPointerCount()==2) {
            float newDist = spacing(event);
            matrix.set(savedMatrix);
            if (newDist > 10f) {              
                float scale = newDist / oldDist;
                matrix.postScale(scale, scale, mid.x, mid.y);
            } 
            if (lastEvent!=null){
                newRot = rotation(event); 
                float r = newRot-degrees;
                matrix.postRotate(r, view.getMeasuredWidth()/2, view.getMeasuredHeight()/2);  
            }
        }
        // 
        break;
    }   

    view.setImageMatrix(matrix);
}

private float rotation(MotionEvent event) 
{
    double delta_x = (event.getX(0) - event.getX(1));
    double delta_y = (event.getY(0) - event.getY(1)); 
    double radians = Math.atan2(delta_y, delta_x);
    return (float) Math.toDegrees(radians);         
} 




/*     
 * --------------------------------------------------------------------------
 * Method: spacing Parameters: MotionEvent Returns: float Description:
 * 
 * checks the spacing between the two fingers on touch
 * ----------------------------------------------------
 */

private float spacing(MotionEvent event) 
{
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    return FloatMath.sqrt(x * x + y * y);
}

/*
 * --------------------------------------------------------------------------
 * Method: midPoint Parameters: PointF object, MotionEvent Returns: void
 * Description: calculates the midpoint between the two fingers
 * ------------------------------------------------------------
 */   

private void midPoint(PointF point, MotionEvent event) 
{
    float x = event.getX(0) + event.getX(1);
    float y = event.getY(0) + event.getY(1);
    point.set(x / 2, y / 2);
}

现在将 touchAction 侦听器添加到您要移动的视图中...

   myImageView.setOnTouchListener(touchAction);
于 2012-09-20T04:45:51.480 回答
1

尝试对您的 x 和 y 坐标进行一些偏移。

//should be on the order of magnitude of the size of the view
float x_offset= 10;  //Play with this.
float y_offset= 10;  //Play with this.

final float x =   event.getX() + x_offset; 
final float y =   event.getY() + y_offset;

moveView(x, y);
于 2012-09-20T05:24:22.763 回答
0

尝试将您的布局从 RelativeLayout 更改为 AbsoluteLayout

于 2012-09-20T02:09:56.300 回答