您好,登錄后才能下訂單哦!
前幾天做項目用到相機拍照,之后能對圖片進行縮放,拖拽,在此我將其單獨抽取出來,后面用到時直接拿來用就行了!
效果圖:
注:這里不僅能按鈕縮放,還能多點觸摸縮放和拖拽功能!
1.布局:
<?xml version="1.0" encoding="UTF-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/flayout_img_display" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout_img_display" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" > <ImageView android:id="@+id/img_display" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="5.0dip" android:paddingTop="5.0dip" android:scaleType="matrix" /> </LinearLayout> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_min" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:enabled="false" android:text="縮小" /> <Button android:id="@+id/btn_out" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="放大" /> </RelativeLayout> </FrameLayout>
2.就一個類:
import android.app.Activity; import android.content.Intent; 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.OnClickListener; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; /** * 縮放圖片界面 * @author ZHF * */ public class MainActivity extends Activity { public static final String TAG = "ImgDisplayActivity"; //控件聲明 private Button btnZoomin, btnZoomout; private ImageView imgDisPlay; private LinearLayout lLayoutDisplay; private FrameLayout fLayoutDisplay; private Bitmap bitmap; private int imgId = 0; private double scale_in = 0.8;//縮小比例 private double scale_out = 1.25;//放大比例 private float scaleWidth = 1; private float scaleHeight = 1; //模式:0:什么都不干;1:拖拽; 2:縮放 public static final int NONE = 0; public static final int DRAG = 1; public static final int ZOOM = 2; //聲明觸發的事件模式 private int mode = NONE; private Matrix matrix; //矩陣 private Matrix currMatrix; //當前矩陣 private PointF starPoint; private PointF midPoint; private float startDistance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 fLayoutDisplay = (FrameLayout) findViewById(R.id.flayout_img_display); lLayoutDisplay = (LinearLayout) findViewById(R.id.linearLayout_img_display); imgDisPlay = (ImageView) findViewById(R.id.img_display); btnZoomin = (Button) findViewById(R.id.btn_min); btnZoomout = (Button) findViewById(R.id.btn_out); matrix = new Matrix(); //保存拖拽變化 currMatrix = new Matrix();// 當前的 starPoint = new PointF();//開始點的位置 btnZoomin.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { zoomIn(); } }); btnZoomout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { zoomOut();//放大 } }); imgDisPlay.setImageResource(R.drawable.img); //給圖片綁定監聽器哦 imgDisPlay.setOnTouchListener(new ImageViewOnTouchListener()); } /**放大操作**/ private void zoomOut() { reSizeBmp(scale_out); btnZoomin.setEnabled(true); } /**縮小操作**/ private void zoomIn() { reSizeBmp(scale_in); } /**接收傳入的縮放比例實現縮放**/ private void reSizeBmp(double scale) { //縮放比例 scaleWidth = (float) (scaleWidth * scale); scaleHeight = (float) (scaleHeight * scale); Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); //設計縮放比例 imgDisPlay.setImageMatrix(matrix); } /**計算觸摸實現縮放**/ final class ImageViewOnTouchListener implements OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: //一只手指按下 Log.i(TAG,"一只手指按下"); currMatrix.set(matrix); starPoint.set(event.getX(), event.getY()); mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: //如果有一只手指按下屏幕,后續又有一個手指按下 // 兩只手指按下 Log.i(TAG,"又有一只手指按下"); startDistance = distance(event);//記下兩點的距離 Log.i(TAG, startDistance+""); if(startDistance > 5f) { //兩個手指之間的最小距離像素大于5,認為是多點觸摸 mode = ZOOM; currMatrix.set(matrix); midPoint = getMidPoint(event); //記下兩個點之間的中心點 } break; case MotionEvent.ACTION_MOVE: if(mode == DRAG) { //拖拽模式 Log.i(TAG,"一只手指在拖拽"); //開始--》結束點的距離 float dx = event.getX() - starPoint.x; float dy = event.getY() - starPoint.y; matrix.set(currMatrix); matrix.postTranslate(dx, dy);//移動到指定點:矩陣移動比例;eg:縮放有縮放比例 } else if(mode == ZOOM) { //縮放模式 Log.i(TAG,"正在縮放"); float distance = distance(event); //兩點之間的距離 if(distance > 5f) { matrix.set(currMatrix); float cale = distance / startDistance; matrix.preScale(cale, cale, midPoint.x, midPoint.y); //進行比例縮放 } } break; case MotionEvent.ACTION_UP: //最后一只手指離開屏幕后觸發此事件 case MotionEvent.ACTION_POINTER_UP: //一只手指離開屏幕,但還有一只手指在上面會觸此事件 //什么都沒做 mode = NONE; break; default: break; } imgDisPlay.setImageMatrix(matrix); //兩只手指的縮放 return true; } } /**計算兩點之間的距離像素**/ private float distance(MotionEvent e) { float eX = e.getX(1) - e.getX(0); //后面的點坐標 - 前面點的坐標 float eY = e.getY(1) - e.getY(0); return FloatMath.sqrt(eX * eX + eY * eY); } /**計算兩點之間的中心點**/ private PointF getMidPoint(MotionEvent event) { float x = (event.getX(1) - event.getX(0)) / 2; float y = (event.getY(1) - event.getY(0)) / 2; return new PointF(x,y); } }
ok!主要的算法就在ImageViewOnTouchListener監聽器當中,要考慮那三種情況(縮放、拖拽、什么都不干),另外需要注意的就是,單個手指和兩個以上手指的情況。
×××:http://down.51cto.com/data/876165
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。