圖片縮放時java.lang.IllegalArgumentException: pointerIndex out of range解決方案
阿新 • • 發佈:2018-12-22
package com.example.webproject; import android.app.Activity; 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 ImageMarixActivity extends Activity{ private static final String TAG = "ImageMarixActivity"; private static final int RESULT_CODE_NOFOUND = 200; private ImageView matrixImageView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.imagemarix_activity); matrixImageView = (ImageView) findViewById(R.id.matriximageview); //監聽圖片的觸控事件 matrixImageView.setOnTouchListener(new TouchListener()); } public final class TouchListener implements OnTouchListener{ //記錄拖拽是放大還是縮小 private int mode = 0;//初始狀態 private int DRAG_MODE = 1;//拖拽模式 private int ZOOM_MODE = 2 ;//縮小模式 //記錄開始的位置 private PointF startPoint = new PointF(); //兩個手指中間的距離 private PointF midPont; //拖拉圖片開始移動的座標位置 private Matrix matrix = new Matrix(); //拖拉圖片當前移動的座標位置 private Matrix currentMatrix = new Matrix(); //開始兩個手指的距離 private float twopointstartDis; @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()& MotionEvent.ACTION_MASK) { //手指按下的時候 case MotionEvent.ACTION_DOWN: mode = DRAG_MODE; //記錄當前的位置 Log.i(TAG, "當前的位置"+matrixImageView.getImageMatrix()); currentMatrix.set(matrixImageView.getImageMatrix()); //記錄手指按下去的位置 startPoint.set(event.getX(), event.getY()); break; case MotionEvent.ACTION_MOVE: Log.i(TAG, "移動"+"模式"+mode); //如果是拖拉模式 if(mode == DRAG_MODE) { float dx = event.getX() - startPoint.x; float dy = event.getY() - startPoint.y; matrix.set(currentMatrix);//在原有的基礎上移動 matrix.postTranslate(dx, dy); } else if(mode == ZOOM_MODE) { Log.i(TAG, "ZOOM_MODE獲取第0個x"+event.getX(0)); Log.i(TAG, "獲取第一個x"+event.getX(1)); float endDis = distance(event); if(endDis >10f) { matrix.set(currentMatrix); float scale =endDis / twopointstartDis; matrix.postScale(scale, scale,midPont.x,midPont.y); } } break; case MotionEvent.ACTION_UP: Log.i(TAG, "一隻手指點選後彈起來"); // mode =0; break; case MotionEvent.ACTION_POINTER_DOWN: mode = ZOOM_MODE; twopointstartDis = distance(event); if(twopointstartDis>10f) { midPont = mid(event); currentMatrix.set(matrixImageView.getImageMatrix()); } break; case MotionEvent.ACTION_POINTER_UP: Log.i(TAG, "兩隻手指點選後彈起來"); // Log.i(TAG, "獲取第一個x"+event.getX(1)+"獲取的第一個x"+event.getY(0)); // Log.i(TAG, "獲取第一個x"+(event.getX(1)-event.getY(0))); mode = 0; break; default: break; } matrixImageView.setImageMatrix(matrix); return true; } } public float distance(MotionEvent event) { Log.i(TAG, "呼叫測距"); float dx = event.getX(1) - event.getX(0); float dy = event.getY(1) - event.getY(0); return FloatMath.sqrt(dx*dx+dy*dy); } public PointF mid(MotionEvent event) { float dx = (event.getX(1)+event.getX(0))/2; float dy = (event.getY(1)+event.getY(0))/2; return new PointF(dx,dy); } }