图片移动放大缩小功能集合

只要在xml布局文件里面,放进去就可以了,然后加载图片

package com.example.text;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;

import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.view.View.OnTouchListener;
public class MyImageView extends ImageView implements OnTouchListener{

	private Matrix matrix = new Matrix();
	private Matrix savedMatrix = new Matrix();
	PointF start = new PointF();
	PointF mid = new PointF();
	static final int NONE = 0;
	static final int DRAG = 1;
	static final int ZOOM = 2;
	int mode = NONE;
	float oldDist = 1f;
	
	public MyImageView(Context context) {
		super(context);
		setOnTouchListener(this);
		setLongClickable(true);
	}
	
	public MyImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnTouchListener(this);
		setLongClickable(true);
	}
	public MyImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setOnTouchListener(this);
		setLongClickable(true);
		
	}

	
	//使用BitmapFactory.Options的inSampleSize参数来缩放
    public static Drawable resizeImage2(String path,
			int width,int height) 
    {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;//不加载bitmap到内存中
		BitmapFactory.decodeFile(path,options); 
		int outWidth = options.outWidth;
		int outHeight = options.outHeight;
		options.inDither = false;
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		options.inSampleSize = 1;
		
		if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0) 
		{
			int sampleSize=(outWidth/width+outHeight/height)/2;
			options.inSampleSize = sampleSize;
		}
	
		options.inJustDecodeBounds = false;
		return new BitmapDrawable(BitmapFactory.decodeFile(path, options));		
	}

	//使用Bitmap加Matrix来缩放
    public static Drawable resizeImage(Bitmap bitmap, int w, int h) 
    {  
        Bitmap BitmapOrg = bitmap;  
        int width = BitmapOrg.getWidth();  
        int height = BitmapOrg.getHeight();  
        int newWidth = w;  
        int newHeight = h;  

        float scaleWidth = ((float) newWidth) / width;  
        float scaleHeight = ((float) newHeight) / height;  

        Matrix matrix = new Matrix();  
        matrix.postScale(scaleWidth, scaleHeight);  
        // if you want to rotate the Bitmap   
        // matrix.postRotate(45);   
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,  
                        height, matrix, true);  
        return new BitmapDrawable(resizedBitmap);  
    }
	
	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 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);
	}
	@SuppressLint("ClickableViewAccessibility") @Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				/*
				 * Log.d("Infor", "类别:"+event.getAction()); Log.d("Infor",
				 * "mask:"+event.getActionMasked()); Log.d("Infor",
				 * "index:"+event.getActionIndex()); Log.d("Infor",
				 * "points:"+event.getPointerCount());
				 */
				Log.d("Infor", "size:" + event.getSize());
				if (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP)
					Log.d("Infor", "多点操作");
				switch (event.getActionMasked()) {
				case MotionEvent.ACTION_DOWN:
					matrix.set(getImageMatrix());
					savedMatrix.set(matrix);
					start.set(event.getX(), event.getY());
					Log.d("Infor", "触摸了...");
					mode = DRAG;
					break;
				case MotionEvent.ACTION_POINTER_DOWN: // 多点触控
					oldDist = this.spacing(event);
					if (oldDist > 10f) {
						Log.d("Infor", "oldDist" + oldDist);
						savedMatrix.set(matrix);
						midPoint(mid, event);
						mode = ZOOM;
					}
					break;
				case MotionEvent.ACTION_POINTER_UP:
					mode = NONE;
					break;
				case MotionEvent.ACTION_MOVE:
					if (mode == DRAG) { // 此实现图片的拖动功能...
						matrix.set(savedMatrix);
						matrix.postTranslate(event.getX() - start.x, event.getY()
								- start.y);
					} else if (mode == ZOOM) {// 此实现图片的缩放功能...
						float newDist = spacing(event);
						if (newDist > 10) {
							matrix.set(savedMatrix);
							float scale = newDist / oldDist;
							matrix.postScale(scale, scale, mid.x, mid.y);
						}
					}
					break;
				}
				setImageMatrix(matrix);
		return false;
	}

}

布局文件

	<com.example.text.MyImageview  android:id="@+id/id_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:scaleType="matrix" 
                android:src="@drawable/placeholder1"/>

猜你喜欢

转载自274137570-qq-com.iteye.com/blog/2369849