Android實現手指滑動控制元件縮放
ImageView設定手指滑動縮放
本來就是我平時的兩篇練習,但是昨天看了一下感覺其實還挺有用的.就把他拿出了獻醜了,還有另外一篇關於控制元件抖動效果的部落格大家可以關注.
實現步驟
1, imageview設定scaletype為 android:scaleType=”matrix”
2, 設定imageview的setOnTouchListener,重寫裡面的程式碼
3, 新建一個matrix,
matrix.postScale(scale,scale,縮放中心,縮放中心);
image.setImageMatrix(matrix);
這樣image的大小就會改變了.
需要注意的幾點是 imageview一定要設定scaletype為matrix,否則圖片不能進行縮放.而且Bitmap也可以通過Matrix改變大小.
也可以用自定義控制元件imageview,重寫裡面的onTouchEvent,本文就是用的自定義控制元件得到方式實現的.
程式碼
自定義控制元件的程式碼,主要是重寫的onTouchEvent的程式碼
package com.test.administrator.imageviewscale; import android.content.Context; import android.graphics.Matrix; import android.graphics.PointF; import android.renderscript.Float2; import android.renderscript.Float4; import android.util.AttributeSet; import android.util.FloatMath; import android.view.MotionEvent; import android.widget.ImageView; /** * Created by Administrator on 2016/3/21. */ public class MyImageView extends ImageView { private PointF centerPoint; private float firstdistance; private Matrix matrix; public MyImageView(Context context) { super(context); initParams(); } private void initParams() { matrix = new Matrix(); setImageMatrix(matrix); } public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); initParams(); } public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initParams(); } @Override public boolean onTouchEvent(MotionEvent event) { int count = event.getPointerCount(); if(count == 1){ switch (event.getAction()){ case MotionEvent.ACTION_DOWN: System.out.println("actiondown 1----"); break; case MotionEvent.ACTION_MOVE: System.out.println("actionmove 1----"); break; case MotionEvent.ACTION_UP: System.out.println("actionup 1----"); break; default: break; } }else if(count == 2){ switch (event.getAction()&MotionEvent.ACTION_MASK){ case MotionEvent.ACTION_POINTER_DOWN: firstdistance = getDistance(event); centerPoint = getCenterPoint(event); System.out.println("actiondown 2==="); break; case MotionEvent.ACTION_MOVE: float distance = getDistance(event); float scale = distance / firstdistance; matrix.postScale(scale,scale,centerPoint.x,centerPoint.y); setImageMatrix(matrix); System.out.println(scale + "===="); firstdistance = distance; System.out.println("actionmove 2==="); break; case MotionEvent.ACTION_UP: break; default: break; } } return true; } /** * 獲取兩個點的中心點座標 * @param event */ private PointF getCenterPoint(MotionEvent event) { PointF point = new PointF(); point.x = (event.getX()+event.getX(1))/2; point.y = (event.getY()+event.getY(1))/2; return point; } /** * 兩根手指時手指間的距離 * @param event */ private float getDistance(MotionEvent event) { float x1 = event.getX(); float y1 = event.getY(); float x2 = event.getX(1); float y2 = event.getY(1); float distance = (float) Math.sqrt((x1 - x2)*(x1 - x2)+(y1 - y2)*(y1- y2)); return distance; } }
imageview的程式碼
<com.test.administrator.imageviewscale.MyImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:scaleType="matrix"
android:src="@drawable/qq" />