在android imageview中旋转变焦拖动图像

我想能够在android framelayout和imageview中旋转,缩放和移动图像。 用下面的代码,我能够做到这一点。 但是这个观点有时会从UI中走出来。 正如你可以c我处理ACTION_UP(每个ACTION_UP 90度旋转)motionevent旋转。 以下是我的main.xml:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/csf6a4_flipped" android:scaleType="matrix" > </ImageView> </FrameLayout> 

以下是我的java文件:

 package com.example.rotatezoommove; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.PointF; import android.os.Bundle; import android.util.FloatMath; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class RotateZoomMove extends Activity implements OnTouchListener { private static final String TAG = "Touch"; // These matrices will be used to move and zoom image Matrix matrix = new Matrix(); Matrix savedMatrix = new Matrix(); // We can be in one of these 3 states static final int NONE = 0; static final int DRAG = 1; static final int ZOOM = 2; int mode = NONE; // Remember some things for zooming PointF start = new PointF(); PointF mid = new PointF(); float oldDist = 1f; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView view = (ImageView) findViewById(R.id.imageView); view.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v; int rotation = 25; // Dump touch event to log dumpEvent(event); // Handle touch events here... switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: savedMatrix.set(matrix); start.set(event.getX(), event.getY()); Log.d(TAG, "mode=DRAG"); mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: oldDist = spacing(event); Log.d(TAG, "oldDist=" + oldDist); if (oldDist > 10f) { savedMatrix.set(matrix); midPoint(mid, event); mode = ZOOM; Log.d(TAG, "mode=ZOOM"); } break; case MotionEvent.ACTION_UP: mode = NONE; Log.d(TAG, "mode=NONE"); savedMatrix.set(matrix); matrix.postRotate(90); 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) { float newDist = spacing(event); Log.d(TAG, "newDist=" + newDist); if (newDist > 10f) { matrix.set(savedMatrix); float scale = newDist / oldDist; matrix.postScale(scale, scale, mid.x, mid.y); } } break; } view.setImageMatrix(matrix); return true; // indicate event was handled } /** Show an event in the LogCat view, for debugging */ 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, sb.toString()); } /** Determine the space between the first two fingers */ 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); } /** Calculate the mid point of the first 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); } } 

我想我快到了。 任何人都可以提出任何解决scheme,以便我可以旋转,拖动和缩放图像,并始终保持图像内的UI?

从里面调用这个方法:

 setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { .......................................................... fixing(); setImageMatrix(savedMatrix2); 

在回报结束之前

 public void fixing() { float[] value = new float[9]; matrix.getValues(value); float[] savedValue = new float[9]; savedMatrix2.getValues(savedValue); int width = getWidth(); int height = getHeight(); Drawable d = getDrawable(); if (d == null) return; int imageWidth = d.getIntrinsicWidth(); int imageHeight = d.getIntrinsicHeight(); int scaleWidth = (int) (imageWidth * value[0]); int scaleHeight = (int) (imageHeight * value[4]); // don't let the image go outside if (value[2] > width-1) value[2] = width-10; else if (value[5] > height - 1) value[5] = height - 10; else if (value[2] < -(scaleWidth-1)) value[2] = -(scaleWidth-10); else if (value[5] < -(scaleHeight-1)) value[5] = -(scaleHeight-10); // maximum zoom ratio: MAx if (value[0] > MAX_ZOOM || value[4] > MAX_ZOOM){ value[0] = MAX_ZOOM; value[4] = MAX_ZOOM; //value[2] = savedValue[2]; //value[5] = savedValue[5]; } matrix.setValues(value); savedMatrix2.set(matrix); } 

首先,你必须在你的.xml文件中使用ImageView

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout> 

然后,在Java中初始化Imageview ,并将onTouchListener设置为ImageView

 ImageView imageView=(ImageView) findViewById(R.id.imageView1); imageView.setOnTouchListener(this); 

onTouchListener实现到Activity,并将未实现的方法添加到Activity。 现在,最后把这个代码写在你的onTouch

 @Override public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v; view.setScaleType(ImageView.ScaleType.MATRIX); float scale; // Dump touch event to log dumpEvent(event); // 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()); Log.d(TAG, "mode=DRAG" ); mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: oldDist = spacing(event); if (oldDist > 10f) { 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); d = rotation(event); break; case MotionEvent.ACTION_UP: //first finger lifted case MotionEvent.ACTION_POINTER_UP: //second finger lifted mode = NONE; Log.d(TAG, "mode=NONE" ); 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) { scale = newDist / oldDist; matrix.postScale(scale, scale, mid.x, mid.y); } if (lastEvent != null) { newRot = rotation(event); float r = newRot - d; matrix.postRotate(r, view.getMeasuredWidth() / 2, view.getMeasuredHeight() / 2); } } break; } // Perform the transformation view.setImageMatrix(matrix); return true; // indicate event was handled } 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); } 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); } /** Show an event in the LogCat view, for debugging */ 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, sb.toString()); } 

使用Android StickerView轻松移动,缩放和旋转贴纸

请查看本教程以获取分步指南: 点击此处

在这里输入图像说明