Android 可平移,縮放,旋轉的ImageView
在網上搜到很多都是一樣的版本,只有平移和縮放的功能。我在搜到的原始碼基礎上新增了旋轉和邊界檢查的功能。
程式碼主要分兩部分,一個activity一個view,程式碼如下:
- package com.example.touch;
- import android.app.Activity;
- import android.os.Bundle;
- public class TouchImageViewActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TouchImageView img = new TouchImageView(this);
- setContentView(img);
- }
- }
- package com.example.touch;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Matrix;
- import android.graphics.PointF;
- import android.util.DisplayMetrics;
- import android.util.FloatMath;
- import android.view.MotionEvent;
- import android.widget.ImageView;
- public class TouchImageView extends ImageView {
- float x_down = 0;
- float y_down = 0;
- PointF start = new PointF();
- PointF mid = new PointF();
- float oldDist = 1f;
- float oldRotation = 0;
- Matrix matrix = new Matrix();
- Matrix matrix1 = new Matrix();
- Matrix savedMatrix = new Matrix();
- private static final int NONE = 0;
- private static final int DRAG = 1;
- private static final int ZOOM = 2;
- int mode = NONE;
- boolean matrixCheck = false;
- int widthScreen;
- int heightScreen;
- Bitmap gintama;
- public TouchImageView(TouchImageViewActivity activity) {
- super(activity);
- gintama = BitmapFactory.decodeResource(getResources(), R.drawable.gintama);
- DisplayMetrics dm = new DisplayMetrics();
- activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
- widthScreen = dm.widthPixels;
- heightScreen = dm.heightPixels;
- matrix = new Matrix();
- }
- protected void onDraw(Canvas canvas) {
- canvas.save();
- canvas.drawBitmap(gintama, matrix, null);
- canvas.restore();
- }
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction() & MotionEvent.ACTION_MASK) {
- case MotionEvent.ACTION_DOWN:
- mode = DRAG;
- x_down = event.getX();
- y_down = event.getY();
- savedMatrix.set(matrix);
- break;
- case MotionEvent.ACTION_POINTER_DOWN:
- mode = ZOOM;
- oldDist = spacing(event);
- oldRotation = rotation(event);
- savedMatrix.set(matrix);
- midPoint(mid, event);
- break;
- case MotionEvent.ACTION_MOVE:
- if (mode == ZOOM) {
- matrix1.set(savedMatrix);
- float rotation = rotation(event) - oldRotation;
- float newDist = spacing(event);
- float scale = newDist / oldDist;
- matrix1.postScale(scale, scale, mid.x, mid.y);// 縮放
- matrix1.postRotate(rotation, mid.x, mid.y);// 旋轉
- matrixCheck = matrixCheck();
- if (matrixCheck == false) {
- matrix.set(matrix1);
- invalidate();
- }
- } else if (mode == DRAG) {
- matrix1.set(savedMatrix);
- matrix1.postTranslate(event.getX() - x_down, event.getY()
- - y_down);// 平移
- matrixCheck = matrixCheck();
- matrixCheck = matrixCheck();
- if (matrixCheck == false) {
- matrix.set(matrix1);
- invalidate();
- }
- }
- break;
- case MotionEvent.ACTION_UP:
- case MotionEvent.ACTION_POINTER_UP:
- mode = NONE;
- break;
- }
- return true;
- }
- private boolean matrixCheck() {
- float[] f = new float[9];
- matrix1.getValues(f);
- // 圖片4個頂點的座標
- float x1 = f[0] * 0 + f[1] * 0 + f[2];
- float y1 = f[3] * 0 + f[4] * 0 + f[5];
- float x2 = f[0] * gintama.getWidth() + f[1] * 0 + f[2];
- float y2 = f[3] * gintama.getWidth() + f[4] * 0 + f[5];
- float x3 = f[0] * 0 + f[1] * gintama.getHeight() + f[2];
- float y3 = f[3] * 0 + f[4] * gintama.getHeight() + f[5];
- float x4 = f[0] * gintama.getWidth() + f[1] * gintama.getHeight() + f[2];
- float y4 = f[3] * gintama.getWidth() + f[4] * gintama.getHeight() + f[5];
- // 圖片現寬度
- double width = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
- // 縮放比率判斷
- if (width < widthScreen / 3 || width > widthScreen * 3) {
- return true;
- }
- // 出界判斷
- if ((x1 < widthScreen / 3 && x2 < widthScreen / 3
- && x3 < widthScreen / 3 && x4 < widthScreen / 3)
- || (x1 > widthScreen * 2 / 3 && x2 > widthScreen * 2 / 3
- && x3 > widthScreen * 2 / 3 && x4 > widthScreen * 2 / 3)
- || (y1 < heightScreen / 3 && y2 < heightScreen / 3
- && y3 < heightScreen / 3 && y4 < heightScreen / 3)
- || (y1 > heightScreen * 2 / 3 && y2 > heightScreen * 2 / 3
- && y3 > heightScreen * 2 / 3 && y4 > heightScreen * 2 / 3)) {
- return true;
- }
- return false;
- }
- // 觸碰兩點間距離
- 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);
- }
- // 取旋轉角度
- 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);
- }
- // 將移動,縮放以及旋轉後的圖層儲存為新圖片
- // 本例中沒有用到該方法,需要儲存圖片的可以參考
- public Bitmap CreatNewPhoto() {
- Bitmap bitmap = Bitmap.createBitmap(widthScreen, heightScreen,
- Config.ARGB_8888); // 背景圖片
- Canvas canvas = new Canvas(bitmap); // 新建畫布
- canvas.drawBitmap(gintama, matrix, null); // 畫圖片
- canvas.save(Canvas.ALL_SAVE_FLAG); // 儲存畫布
- canvas.restore();
- return bitmap;
- }
- }
相關文章
- ARFoundation - 實現物體旋轉, 平移,縮放
- Android 中實現圖片平移、縮放、旋轉同步進行Android
- 使用RxJava實現ImageView的拖動、旋轉和縮放RxJavaView
- Qt 從 QTransform 逆向解出 Translate/Scale/Rotate(平移/縮放/旋轉)分析QTORM
- CGAffineTransform二維檢視旋轉、縮放、平移變換詳解ORM
- Android:ImageView圖片縮放、居中AndroidView
- imageView圖片放大縮小及旋轉View
- [Python影象處理] 六.影象縮放、影象旋轉、影象翻轉與影象平移Python
- android imageview 縮放檢視圖片AndroidView
- 通過手勢實現Android ImageView 縮放AndroidView
- 【Android動畫】之Tween動畫 (漸變、縮放、位移、旋轉)Android動畫
- 將圖片旋轉(這裡不是旋轉imageView)View
- 在Delphi中實現圖片的旋轉、縮放 (轉)
- 【數字影象處理】六.MFC空間幾何變換之影象平移、映象、旋轉、縮放詳解
- macw的CAD教程:如何更快的縮放和平移Mac
- CSS3 滑鼠懸浮div旋轉縮放CSSS3
- Android基礎動畫之alpha透明度/translate平移/rotate旋轉Android動畫
- 視覺化學習:WebGL實現縮放平移視覺化Web
- 該演算法利用區域性的平移、縮放以及旋轉的方式來不失真的進行影像纖瘦化處理。演算法
- pixi.js 簡單互動事件(點選、縮放、平移)JS事件
- 學習OpenCV:濾鏡系列(5)——徑向模糊:縮放&旋轉OpenCV
- C# 簡易影像處理(包括平移,旋轉,翻轉, 裁切)C#
- 手遊UI互動動作設計研究:縮放、書寫、旋轉UI
- 滑鼠懸浮div實現旋轉縮放效果程式碼例項
- THREE 矩陣優先原則和平移旋轉矩陣矩陣
- OpenCV計算機視覺學習(11)——影像空間幾何變換(影像縮放,影像旋轉,影像翻轉,影像平移,仿射變換,映象變換)OpenCV計算機視覺
- web技術分享| WebRTC控制攝像機平移、傾斜和縮放Web
- QT 自定義QGraphicsItem 縮放後旋轉 圖形出現漂移問題QT
- 第四個OpenGL程式,vector 向量 (矩陣變換之 旋轉,縮放)矩陣
- android canvas.drawBitmap(bitmap, matrix, paint) 中 利用 matrix 實現平移到中心點及中心點縮放AndroidCanvasAI
- Android 圖片縮放Android
- android 拖拽與縮放Android
- CSS屬性transform之旋轉:rotate,縮放:scale,傾斜:skew,移動:translateCSSORM
- opencv中自定義的雙線性二次插值的影像旋轉及縮放OpenCV
- iOS動畫系列之五:基礎動畫之縮放篇&旋轉篇Swift+OCiOS動畫Swift
- blender python api -修改骨架中特定骨骼的變換,包括沿不同軸的旋轉、位置和縮放(旋轉為四元數運算WXYZ)PythonAPI
- 【開源】canvas影象裁剪、壓縮、旋轉Canvas
- 在ArcGIS Pro中對Revit的bim資料進行地理配準(平移、旋轉等)