6

我需要拖动和缩放图像。这是我的代码...

//instance variables

Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;

//图像视图的OnTouchListener

OnTouchListener touchAction = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView i = (ImageView)v;

        dragAndZoom(i, event);
        return true; 

    }
};

//执行拖拽操作

private void dragAndZoom(View v, MotionEvent event) 
{
    ImageView view = (ImageView) v;
    view.setScaleType(ImageView.ScaleType.MATRIX);
    float scale;

    // Handle touch events here...

    switch (event.getAction() & MotionEvent.ACTION_MASK) 
    {
    case MotionEvent.ACTION_DOWN:  

        // first finger down only
        savedMatrix.set(matrix);
        start.set(event.getX(), event.getY());


         mode = DRAG;
        break;

    case MotionEvent.ACTION_UP: // first finger lifted

    case MotionEvent.ACTION_POINTER_UP: // second finger lifted

        mode = NONE;   

        break;

    case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down

        oldDist = spacing(event);

        if (oldDist > 5f)
        {
            savedMatrix.set(matrix);
            midPoint(mid, event);
            mode = ZOOM;

        }
        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) 
        { 
            // pinch zooming
            float newDist = spacing(event);

            if (newDist > 5f) 
            {
                matrix.set(savedMatrix);
                scale = newDist / oldDist; 
                matrix.postScale(scale, scale, mid.x, mid.y);
            }
        }
        break;  
    }

    view.setImageMatrix(matrix); // display the transformation on screen


}

  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);
}



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);
}

它工作正常。但有时图像会在触摸时超出屏幕边界。如何始终将图像保留在 UI 中?请帮我。提前致谢。

4

1 回答 1

2
private void limitDrag(Matrix m, ImageView view) {


    float[] values = new float[9];
    m.getValues(values);
    float transX = values[Matrix.MTRANS_X];
    float transY = values[Matrix.MTRANS_Y];
    float scaleX = values[Matrix.MSCALE_X];
    float scaleY = values[Matrix.MSCALE_Y];

    Rect bounds = view.getDrawable().getBounds();
    int viewWidth = getResources().getDisplayMetrics().widthPixels;
    int viewHeight = getResources().getDisplayMetrics().heightPixels;



    if(viewHeight<=480)
    {

        _y_up=0;
    }
    if(viewHeight>480&&viewHeight<980)
    {

        _y_up=140;
    }


    int width = bounds.right - bounds.left;
    int height = bounds.bottom - bounds.top;
    int __width=width;
    int __height=height;
    width = viewWidth / 2;
    height = viewHeight / 2;


    //height = 200 ;
    float minX = (-width) ;//* scaleX;
    float minY = (-height) ;//* scaleY;



    if ((transX) > (viewWidth)) {

        //_x_left

        transX = viewWidth;
    } else if (transX < minX) {


        transX = minX;
    }


    if ((-transX) > (viewWidth)) {
             // _x_right
        transX = -(viewWidth);
    } else if (-transX < minX) {

        transX = -(minX+30);
    }



    if ((transY) > (viewHeight)) {
    //  _y_up
        transY =( viewHeight);


    } else if (transY < minY) {

        transY = (minY+_y_up);
    }

    if ((-transY) > (viewHeight)) {
    //  _y_down
        transY = -(viewHeight);

    } else if (-transY < minY) {

        transY = -(minY+170);
    }

    values[Matrix.MTRANS_X] = transX;
    values[Matrix.MTRANS_Y] = transY;
    m.setValues(values);
}

在您的 view.setImageMatrix(matrix) 上方调用此函数 limitDrag(matrix,view) 以限制屏幕内的移动 注意:- IM 在其中进行一些丑陋的硬编码计算。您可以根据需要更改它。只需运行它并检查,记住它不是最好的解决方案,但总比没有好。快乐编码!

于 2012-11-26T16:11:22.007 回答