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 に答える