我使用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);
}
}
答案 0 :(得分:0)
ImageView类继承自View类,它具有getWidth()和getHeight()。这些方法为您返回像素的测量值。所以你需要做的就是调用mImageView.getWidth()
并返回一个int。