我正在尝试在android中实现缩放功能。一切正常,但问题是,第一次点击图像时,它将图像带到了左上角。
我希望图像可以随时居中。
我将图像视图比例类型用作矩阵和OnTouchListener。
public class ZoomInZoomOut extends Activity implements
View.OnTouchListener, ViewTreeObserver.OnGlobalLayoutListener {
private static final String TAG = "ZoomInZoomOut";
ImageView imgZoom;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zoom_in_zoom_out_image);
imgZoom = (ImageView) findViewById(R.id.imgZoom);
imgZoom.setOnTouchListener(this);
imgZoom.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= 16) {
imgZoom.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
imgZoom.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
centerImage();
}
private void centerImage() {
Drawable drawable = imgZoom.getDrawable();
RectF imageRectF = new RectF(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
RectF viewRectF = new RectF(0, 0, imgZoom.getWidth(), imgZoom.getHeight());
Matrix myMatrix = new Matrix();
myMatrix.setRectToRect(imageRectF, viewRectF, Matrix.ScaleToFit.CENTER);
imgZoom.setImageMatrix(myMatrix);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
float scale;
dumpEvent(event);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: // first finger down only
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG"); // write to LogCat
mode = DRAG;
centerImage();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 5f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "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);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 5f) {
matrix.set(savedMatrix);
scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
centerImage();
break;
}
imgZoom.setImageMatrix(matrix);
return true;
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.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);
}
private void dumpEvent(MotionEvent event) {
String names[] = {"DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?"};
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
Log.d(TAG, "Touch Events ---------" + sb.toString());
}
}
我的期望是实现类似缩放效果的whatsapp。图片应始终居中。
谢谢。