日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

我的Android进阶之旅------android Matrix图片随意的放大缩小,拖动(转)

發(fā)布時間:2025/4/16 Android 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 我的Android进阶之旅------android Matrix图片随意的放大缩小,拖动(转) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

step1:新建一個項目DragAndZoom,并準(zhǔn)備一張照片放在res/drawable-hdpi目錄下,如下圖所示:

? ? ? ?

step2: 設(shè)置應(yīng)用的UI界面,在main.xml中設(shè)置:

?

[html]?view plain?copy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:orientation="vertical"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????>??
  • <ImageView????
  • ????android:layout_width="fill_parent"???
  • ????android:layout_height="wrap_content"???
  • ????android:src="@drawable/wall"??
  • ????android:id="@+id/imageView"??
  • ????android:scaleType="matrix"?????
  • ????/>??<!--?指定為matrix類型?-->??
  • </LinearLayout>??

  • step3:MainActivity.java中實(shí)現(xiàn)具體的需求

    ?

    ?

    [java]?view plain?copy
  • package?cn.roco.drag;??
  • ??
  • import?android.app.Activity;??
  • import?android.graphics.Matrix;??
  • import?android.graphics.PointF;??
  • import?android.os.Bundle;??
  • import?android.util.FloatMath;??
  • import?android.view.MotionEvent;??
  • import?android.view.View;??
  • import?android.view.View.OnTouchListener;??
  • import?android.widget.ImageView;??
  • ??
  • public?class?MainActivity?extends?Activity?{??
  • ??
  • ????private?ImageView?imageView;??
  • ??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ??
  • ????????imageView?=?(ImageView)?this.findViewById(R.id.imageView);??
  • ????????imageView.setOnTouchListener(new?TouchListener());??
  • ????}??
  • ??
  • ????private?final?class?TouchListener?implements?OnTouchListener?{??
  • ??????????
  • ????????/**?記錄是拖拉照片模式還是放大縮小照片模式?*/??
  • ????????private?int?mode?=?0;//?初始狀態(tài)????
  • ????????/**?拖拉照片模式?*/??
  • ????????private?static?final?int?MODE_DRAG?=?1;??
  • ????????/**?放大縮小照片模式?*/??
  • ????????private?static?final?int?MODE_ZOOM?=?2;??
  • ??????????
  • ????????/**?用于記錄開始時候的坐標(biāo)位置?*/??
  • ????????private?PointF?startPoint?=?new?PointF();??
  • ????????/**?用于記錄拖拉圖片移動的坐標(biāo)位置?*/??
  • ????????private?Matrix?matrix?=?new?Matrix();??
  • ????????/**?用于記錄圖片要進(jìn)行拖拉時候的坐標(biāo)位置?*/??
  • ????????private?Matrix?currentMatrix?=?new?Matrix();??
  • ??????
  • ????????/**?兩個手指的開始距離?*/??
  • ????????private?float?startDis;??
  • ????????/**?兩個手指的中間點(diǎn)?*/??
  • ????????private?PointF?midPoint;??
  • ??
  • ????????@Override??
  • ????????public?boolean?onTouch(View?v,?MotionEvent?event)?{??
  • ????????????/**?通過與運(yùn)算保留最后八位?MotionEvent.ACTION_MASK?=?255?*/??
  • ????????????switch?(event.getAction()?&?MotionEvent.ACTION_MASK)?{??
  • ????????????//?手指壓下屏幕??
  • ????????????case?MotionEvent.ACTION_DOWN:??
  • ????????????????mode?=?MODE_DRAG;??
  • ????????????????//?記錄ImageView當(dāng)前的移動位置??
  • ????????????????currentMatrix.set(imageView.getImageMatrix());??
  • ????????????????startPoint.set(event.getX(),?event.getY());??
  • ????????????????break;??
  • ????????????//?手指在屏幕上移動,改事件會被不斷觸發(fā)??
  • ????????????case?MotionEvent.ACTION_MOVE:??
  • ????????????????//?拖拉圖片??
  • ????????????????if?(mode?==?MODE_DRAG)?{??
  • ????????????????????float?dx?=?event.getX()?-?startPoint.x;?//?得到x軸的移動距離??
  • ????????????????????float?dy?=?event.getY()?-?startPoint.y;?//?得到x軸的移動距離??
  • ????????????????????//?在沒有移動之前的位置上進(jìn)行移動??
  • ????????????????????matrix.set(currentMatrix);??
  • ????????????????????matrix.postTranslate(dx,?dy);??
  • ????????????????}??
  • ????????????????//?放大縮小圖片??
  • ????????????????else?if?(mode?==?MODE_ZOOM)?{??
  • ????????????????????float?endDis?=?distance(event);//?結(jié)束距離??
  • ????????????????????if?(endDis?>?10f)?{?//?兩個手指并攏在一起的時候像素大于10??
  • ????????????????????????float?scale?=?endDis?/?startDis;//?得到縮放倍數(shù)??
  • ????????????????????????matrix.set(currentMatrix);??
  • ????????????????????????matrix.postScale(scale,?scale,midPoint.x,midPoint.y);??
  • ????????????????????}??
  • ????????????????}??
  • ????????????????break;??
  • ????????????//?手指離開屏幕??
  • ????????????case?MotionEvent.ACTION_UP:??
  • ????????????????//?當(dāng)觸點(diǎn)離開屏幕,但是屏幕上還有觸點(diǎn)(手指)??
  • ????????????case?MotionEvent.ACTION_POINTER_UP:??
  • ????????????????mode?=?0;??
  • ????????????????break;??
  • ????????????//?當(dāng)屏幕上已經(jīng)有觸點(diǎn)(手指),再有一個觸點(diǎn)壓下屏幕??
  • ????????????case?MotionEvent.ACTION_POINTER_DOWN:??
  • ????????????????mode?=?MODE_ZOOM;??
  • ????????????????/**?計算兩個手指間的距離?*/??
  • ????????????????startDis?=?distance(event);??
  • ????????????????/**?計算兩個手指間的中間點(diǎn)?*/??
  • ????????????????if?(startDis?>?10f)?{?//?兩個手指并攏在一起的時候像素大于10??
  • ????????????????????midPoint?=?mid(event);??
  • ????????????????????//記錄當(dāng)前ImageView的縮放倍數(shù)??
  • ????????????????????currentMatrix.set(imageView.getImageMatrix());??
  • ????????????????}??
  • ????????????????break;??
  • ????????????}??
  • ????????????imageView.setImageMatrix(matrix);??
  • ????????????return?true;??
  • ????????}??
  • ??
  • ????????/**?計算兩個手指間的距離?*/??
  • ????????private?float?distance(MotionEvent?event)?{??
  • ????????????float?dx?=?event.getX(1)?-?event.getX(0);??
  • ????????????float?dy?=?event.getY(1)?-?event.getY(0);??
  • ????????????/**?使用勾股定理返回兩點(diǎn)之間的距離?*/??
  • ????????????return?FloatMath.sqrt(dx?*?dx?+?dy?*?dy);??
  • ????????}??
  • ??
  • ????????/**?計算兩個手指間的中間點(diǎn)?*/??
  • ????????private?PointF?mid(MotionEvent?event)?{??
  • ????????????float?midX?=?(event.getX(1)?+?event.getX(0))?/?2;??
  • ????????????float?midY?=?(event.getY(1)?+?event.getY(0))?/?2;??
  • ????????????return?new?PointF(midX,?midY);??
  • ????????}??
  • ??
  • ????}??
  • ??
  • }??

  • step4:AndroidMainfest.xml

    ?

    ?

    [html]?view plain?copy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ??????package="cn.roco.drag"??
  • ??????android:versionCode="1"??
  • ??????android:versionName="1.0">??
  • ????<uses-sdk?android:minSdkVersion="8"?/>??
  • ??
  • ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">??
  • ????????<activity?android:name=".MainActivity"??
  • ??????????????????android:label="@string/app_name">??
  • ????????????<intent-filter>??
  • ????????????????<action?android:name="android.intent.action.MAIN"?/>??
  • ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
  • ????????????</intent-filter>??
  • ????????</activity>??
  • ??
  • ????</application>??
  • </manifest>??

  • step5:具體的效果圖

    ?

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

    上面兩個是圖片拖拽的效果,而圖片的縮放效果要在真機(jī)中才能夠看得到,請讀者自己在真機(jī)環(huán)境中測試。

    ?

    附注:具體的程序源碼在:http://pan.baidu.com/share/link?shareid=397888&uk=805959799

    ?

    ?

    ?

    ?

    其實(shí) 通過通過手勢也可以縮放圖片 ? ?左--->右 放大 右 --->左 縮小 速度越快,縮放比例越大

    zoom.xml

    ?

    [html]?view plain?copy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:orientation="vertical"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????>??
  • <ImageView????
  • ????android:layout_width="fill_parent"???
  • ????android:layout_height="wrap_content"???
  • ????android:src="@drawable/wall"??
  • ????android:id="@+id/show"??
  • ????android:scaleType="matrix"?????
  • ????/>??<!--?指定為matrix類型?-->??
  • </LinearLayout>??
  • ?

    ?

    GestureZoom.java

    ?

    [java]?view plain?copy
  • package?cn.roco.gesture;??
  • ??
  • import?android.app.Activity;??
  • import?android.graphics.Bitmap;??
  • import?android.graphics.BitmapFactory;??
  • import?android.graphics.Matrix;??
  • import?android.graphics.drawable.BitmapDrawable;??
  • import?android.os.Bundle;??
  • import?android.view.GestureDetector;??
  • import?android.view.GestureDetector.OnGestureListener;??
  • import?android.view.MotionEvent;??
  • import?android.widget.ImageView;??
  • ??
  • /**?
  • ?*?通過手勢?縮放圖片?左--->右?放大?右?--->左?縮小?速度越快,縮放比例越大?
  • ?*/??
  • public?class?GestureZoom?extends?Activity?implements?OnGestureListener?{??
  • ????//?定義手勢檢測器實(shí)例??
  • ????GestureDetector?detector;??
  • ????ImageView?imageView;??
  • ????//?初始化圖片資源??
  • ????Bitmap?bitmap;??
  • ????//?定義圖片的高和寬??
  • ????int?width,?height;??
  • ????//?記錄當(dāng)前的縮放比??
  • ????float?currentScale?=?1;??
  • ????//?控制圖片縮放的Matrix對象??
  • ????Matrix?matrix;??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.zoom);??
  • ????????//?創(chuàng)建手勢檢測器??
  • ????????detector?=?new?GestureDetector(this);??
  • ????????imageView?=?(ImageView)?findViewById(R.id.show);??
  • ????????matrix?=?new?Matrix();??
  • ????????//?獲取被縮放的源圖片??
  • ????????bitmap?=?BitmapFactory.decodeResource(this.getResources(),??
  • ????????????????R.drawable.wall);??
  • ????????//?獲得位圖的寬??
  • ????????width?=?bitmap.getWidth();??
  • ????????//?獲得位圖的高??
  • ????????height?=?bitmap.getHeight();??
  • ????????//?設(shè)置?ImageView初始化顯示的圖片??
  • ????????imageView.setImageBitmap(BitmapFactory.decodeResource(??
  • ????????????????this.getResources(),?R.drawable.wall));??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onTouchEvent(MotionEvent?event)?{??
  • ????????//?將該Activity上的觸碰時間交個?GestureDetector處理??
  • ????????return?detector.onTouchEvent(event);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onDown(MotionEvent?e)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????return?false;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onShowPress(MotionEvent?e)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onSingleTapUp(MotionEvent?e)?{??
  • ????????return?false;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onScroll(MotionEvent?e1,?MotionEvent?e2,?float?distanceX,??
  • ????????????float?distanceY)?{??
  • ????????return?false;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onLongPress(MotionEvent?e)?{??
  • ??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onFling(MotionEvent?e1,?MotionEvent?e2,?float?velocityX,??
  • ????????????float?velocityY)?{??
  • ????????velocityX?=?velocityX?>?4000???4000?:?velocityX;??
  • ????????velocityY?=?velocityY?<?-4000???-4000?:?velocityY;??
  • ????????//?感覺手勢的速度來計算縮放比,如果?velocityX>0,放大圖像,否則縮小圖像??
  • ????????currentScale?+=?currentScale?*?velocityX?/?4000.0f;??
  • ????????//?保證?currentScale?不會等于0??
  • ????????currentScale?=?currentScale?>?0.01???currentScale?:?0.01f;??
  • ????????//?重置?Matrix??
  • ????????matrix.setScale(currentScale,?currentScale,?160,?200);??
  • ????????BitmapDrawable?tmp?=?(BitmapDrawable)?imageView.getDrawable();??
  • ????????//?如果圖片還未回收,先強(qiáng)制收回該圖片??
  • ????????if?(!tmp.getBitmap().isRecycled())?{??
  • ????????????tmp.getBitmap().recycle();??
  • ????????}??
  • ????????//?根據(jù)原始位圖和?Matrix創(chuàng)建新圖片??
  • ????????Bitmap?bitmap2?=?Bitmap.createBitmap(bitmap,?0,?0,?width,?height,??
  • ????????????????matrix,?true);??
  • ????????//顯示新的位圖??
  • ????????imageView.setImageBitmap(bitmap2);??
  • ????????return?true;??
  • ????}??
  • ??????
  • }??
  • ?

    查看一下運(yùn)行的效果

    ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ?? ? ?

    轉(zhuǎn)載于:https://www.cnblogs.com/gzhnan/articles/5286893.html

    總結(jié)

    以上是生活随笔為你收集整理的我的Android进阶之旅------android Matrix图片随意的放大缩小,拖动(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。