2

Matrix用来缩放图像,但是如何以简单的方式知道缩放后的图像大小?

我的代码如下:

public class MainActivity extends Activity {
    private final String TAG = "MainActivity";

    private ImageView mImageView;
    private Button btnin, btnout;
    int imageid = 0;
    LinearLayout mlayout;
    private float scaleWidth = 1, scaleHeight = 1;
    private final static int NONE = 0;
    private final static int DRAG = 1;
    private final static int ZOOM = 2;

    private int mode = NONE;
    private Matrix matrix;
    private Matrix currentMatrix;
    private PointF startPoint;
    private PointF midPoint;
    private float startDistance;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mlayout = (LinearLayout) findViewById(R.id.mlayout);

        mImageView = (ImageView) findViewById(R.id.myImageView);
        matrix = new Matrix();
        currentMatrix = new Matrix();
        startPoint = new PointF();

        btnin = (Button) findViewById(R.id.myButton1);
        btnout = (Button) findViewById(R.id.myButton2);
        btnout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                zoomOut();

            }
        });
        btnin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                zoomIn();

            }
        });

        mImageView.setOnTouchListener(new ImageViewOnTouchListener());
    }

    private void reSetImage(double d) {

        Matrix matrix=new Matrix();
        scaleWidth *= d;
        scaleHeight *= d;
        matrix.postScale(scaleWidth, scaleHeight);
        mImageView.setImageMatrix(matrix);
    }

    private void zoomIn() {

        reSetImage(0.8);

    }

    private void zoomOut() {

        reSetImage(1.2);
    }

    @SuppressLint("NewApi")
    class ImageViewOnTouchListener implements OnTouchListener {

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

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

                 matrix.set(view.getImageMatrix());  
                 currentMatrix.set(matrix);
                 startPoint.set(event.getX(), event.getY());
                 mode = DRAG;


                break;

            case MotionEvent.ACTION_UP:

                mode = NONE;
                Log.i(TAG, "up");
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;

                Log.i(TAG, "pointer up");
                break;

            case MotionEvent.ACTION_POINTER_DOWN: {
                startDistance = distance(event);
                if (startDistance > 15f) {

                    currentMatrix.set(matrix);
                    midPoint = getMidPoint(event);
                    Log.i(TAG, "midpoint:" + "X:" + midPoint.x + " Y:"
                            + midPoint.y);

                    mode = ZOOM;
                }

            }

                break;

            case MotionEvent.ACTION_MOVE: {
                if (mode == DRAG) {
                    float dx = event.getX() - startPoint.x;
                    float dy = event.getY() - startPoint.y;
                    matrix.set(currentMatrix);
                    matrix.postTranslate(dx, dy);
                    Log.i(TAG, "action move");

                } else if (mode == ZOOM) {
                    float distance = distance(event);
                    if (distance > 15f) {
                        matrix.set(currentMatrix);
                        float scale = distance / startDistance;
                        matrix.postScale(scale, scale, midPoint.x, midPoint.y);

                    }
                }
            }



                break;

            default:
                break;
            }

            mImageView.setImageMatrix(matrix);


            mImageView.invalidate();
            return true;
        }

    }

    private float distance(MotionEvent event) {

        float ex = event.getX(1) - event.getX(0);
        float ey = event.getY(1) - event.getY(0);
        return FloatMath.sqrt(ex * ex + ey * ey);

    }

    private PointF getMidPoint(MotionEvent event) {
        float x = (event.getX(1) + event.getX(0)) / 2;
        float y = (event.getY(1) + event.getY(0)) / 2;
        return new PointF(x, y);
    }
}
4

1 回答 1

0

ImageView 类继承自 View 类,具有 getWidth() 和 getHeight()。这些方法会为您返回以像素为单位的测量值。所以你需要做的就是调用mImageView.getWidth()它,它会返回一个int。

于 2012-12-16T05:16:18.867 回答