有人可以告訴我在Android應用程序中應用縮放,拖動和旋轉圖像的教程嗎?如何將縮放,拖動和旋轉應用於Android中的圖像
4
A
回答
1
6
嘗試用下面的代碼它爲我工作。
float[] lastEvent = null;
float d = 0f;
float newRot = 0f;
private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();
public static String fileNAME;
public static int framePos = 0;
private float scale = 0;
private float newDist = 0;
// Fields
private String TAG = this.getClass().getSimpleName();
// We can be in one of these 3 states
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private int mode = NONE;
// Remember some things for zooming
private PointF start = new PointF();
private PointF mid = new PointF();
float oldDist = 1f;
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
lastEvent = null;
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:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
lastEvent = null;
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) {
float 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;
}
view.setImageMatrix(matrix);
return true;
}
//用於在多點觸摸上旋轉圖像。
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);
}
2
這裏是
public class Touch implements OnTouchListener {
// These matrices will be used to move and zoom image
public static Matrix matrix = new Matrix();
public static 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;
private static final float MAX_ZOOM = (float) 3;
private static final float MIN_ZOOM = 1;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
int width,height;
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
Rect bounds = view.getDrawable().getBounds();
width = bounds.right - bounds.left;
height = bounds.bottom - bounds.top;
// 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());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
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) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist/oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
//----------------------------------------------------
limitZoom(matrix);
limitDrag(matrix);
//----------------------------------------------------
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("]");
}
/** 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);
}
private void limitZoom(Matrix m) {
float[] values = new float[9];
m.getValues(values);
float scaleX = values[Matrix.MSCALE_X];
float scaleY = values[Matrix.MSCALE_Y];
if(scaleX > MAX_ZOOM) {
scaleX = MAX_ZOOM;
} else if(scaleX < MIN_ZOOM) {
scaleX = MIN_ZOOM;
}
if(scaleY > MAX_ZOOM) {
scaleY = MAX_ZOOM;
} else if(scaleY < MIN_ZOOM) {
scaleY = MIN_ZOOM;
}
values[Matrix.MSCALE_X] = scaleX;
values[Matrix.MSCALE_Y] = scaleY;
m.setValues(values);
}
private void limitDrag(Matrix m) {
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];
//--- limit moving to left ---
float minX = (-width + 0) * (scaleX-1);
float minY = (-height + 0) * (scaleY-1);
//--- limit moving to right ---
float maxX=minX+width*(scaleX-1);
float maxY=minY+height*(scaleY-1);
if(transX>maxX){transX = maxX;}
if(transX<minX){transX = minX;}
if(transY>maxY){transY = maxY;}
if(transY<minY){transY = minY;}
values[Matrix.MTRANS_X] = transX;
values[Matrix.MTRANS_Y] = transY;
m.setValues(values);
}
}
0
我剛出版了一本新的複雜的手勢庫到位桶的捏縮放和平移的完整代碼(Touch.java有一些修改可實際使用)。
你將能夠操縱任何圖像。
您可以在這裏找到回購:https://bitbucket.org/warwick/hacergestov3
您可以查看Youtube的演示應用程序的位置:https://youtu.be/0CoX5WUL6_8
或者從谷歌下載演示應用程序在這裏Play商店:https://play.google.com/store/apps/details?id=com.WarwickWestonWright.HacerGestoV3Demo
圖書館有一個簡單的用法,並允許同時使用四個經典手勢:旋轉,捏/縮放,移動和扔。
相關問題
- 1. 圖像變換同時應用旋轉,拖動和縮放
- 2. 如何在android中放大,縮小,拖放和旋轉圖像視圖?
- 3. 自動縮放和旋轉圖像
- 4. 拖動,縮放和旋轉多個UIImageviews
- 5. 縮放,旋轉和拖動UIImageView
- 6. 在Android中縮放並拖動圖像
- 7. 我如何在Android中旋轉,縮放和移動圖片
- 8. 如何旋轉並縮放滾動視圖中的圖像?
- 9. 如何在Android中通過放大和縮小拖動圖像並拖動
- 10. 如何拖動旋轉和縮放的CALayer?
- 11. 使用android中的矩陣縮放和拖動圖像
- 12. Android - Webview - 旋轉圖像和縮放以適應頁面寬度
- 13. 旋轉後縮放圖像
- 14. 在Android中使用矩陣旋轉和縮放圖像
- 15. jQuery的旋轉,拖拽和縮放
- 16. 旋轉對應於iPhone中的觸摸拖動的圖像
- 17. CSS imageviewer:圖像旋轉(和縮放)
- 18. 旋轉和縮放圖像刷
- 19. 縮放和旋轉動態創建的圖像中的windows phone
- 20. 旋轉和縮放帶有錨點的圖像視圖android
- 21. 如何旋轉和縮放單應
- 22. 拉直圖像從圖像的中心旋轉和縮放
- 23. 在COCOS2D中旋轉和縮放雪碧圖像Android
- 24. 我如何可以旋轉,縮放和在Android的ImageView中的形象圖一起拖動,與邊框的Android
- 25. 角度圖像拖動和縮放
- 26. 如何正確拖放,旋轉和縮放UIView?
- 27. 旋轉和自動縮放圖片
- 28. 如何在Android OpenGL ES中應用拖放和縮放比例
- 29. 在Android上的畫布上縮放和旋轉圖像
- 30. 在ActionScript中拖動,旋轉和放鬆
出色的工作人!像任何事一樣工作 – Sri
下載上面的項目源代碼http://whats-online.info/science-and-tutorials/98/Android-rotate-zoom-drag-image-in-imageview-on-touch-example/ –
它的工作非常好..它也支持旋轉..哇...謝謝@kyogs –