实现伸缩的imageview

基本照抄的,原文地址:http://blog.csdn.net/lmj623565791/article/details/23441455

这是第一个版本

package com.example.win8imageview.customView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;

public class win8View extends ImageView{
	Matrix matrix;
	float tem=1;
	int mCenterWidth,mCenterHeight;
	float scale=0.85f;
	int count=0;
	public win8View(Context context, AttributeSet attrs) {
		this(context, attrs,0);
		// TODO Auto-generated constructor stub
	}
	public win8View(Context context) {
		this(context,null);
		// TODO Auto-generated constructor stub
	}
	public win8View(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		matrix=new Matrix();
		setScaleType(ScaleType.MATRIX);
	}
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		// TODO Auto-generated method stub
		super.onLayout(changed, left, top, right, bottom);
		mCenterWidth=getWidth()-getPaddingLeft()-getPaddingRight();
		mCenterHeight=getHeight()-getPaddingTop()-getPaddingBottom();
		mCenterHeight/=2;
		mCenterWidth/=2;
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
	
		switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        	count=0;
        	tem=(float) Math.sqrt(Math.sqrt(scale));
        	handler.sendEmptyMessage(1);
			break;
		default:
			break;
		}
		
		return super.onTouchEvent(event);
	}
	Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case 1:
				Log.i("ads", "1:"+count+":"+tem);
				if(count<5){
					matrix.postScale(tem, tem, mCenterWidth, mCenterHeight);
		            win8View.this.setImageMatrix(matrix);
					count++;
					handler.sendEmptyMessage(1);
				}
				else handler.sendEmptyMessage(2);
				break;
            case 2:
            	tem=(float) Math.sqrt(Math.sqrt(1.0f/scale));
            	Log.i("ads", "2:"+count+":"+tem);
            	
				if(count>0){
					matrix.postScale(tem, tem, mCenterWidth, mCenterHeight);
		            win8View.this.setImageMatrix(matrix);
		            count--;
		            handler.sendEmptyMessage(2);
				}
				break;
			default:
				break;
			}
		};
	};
}


以上是我看了一点自己想的,不过测试中发现快速点击会出现问题,因为handler.sendEmptyMessage并不是马上执行的,是发送到消息队列,如果第一次点击触发的缩小放大没完成,再点击会把count设为0,会减少第一次点击应该有的缩小放大,多出现几次就会放大图片。

package com.example.win8imageview.customView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;

public class win8View extends ImageView{
	Matrix matrix;
	float tem=1;
	int mCenterWidth,mCenterHeight;
	float scale=0.85f;
	int count=0;
	boolean finish=true;
	public win8View(Context context, AttributeSet attrs) {
		this(context, attrs,0);
		// TODO Auto-generated constructor stub
	}
	public win8View(Context context) {
		this(context,null);
		// TODO Auto-generated constructor stub
	}
	public win8View(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		matrix=new Matrix();
		setScaleType(ScaleType.MATRIX);
	}
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		// TODO Auto-generated method stub
		super.onLayout(changed, left, top, right, bottom);
		mCenterWidth=getWidth()-getPaddingLeft()-getPaddingRight();
		mCenterHeight=getHeight()-getPaddingTop()-getPaddingBottom();
		mCenterHeight/=2;
		mCenterWidth/=2;
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
	
		switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        	handler.sendEmptyMessage(1);
			break;
        case MotionEvent.ACTION_UP:
        	handler.sendEmptyMessage(2);
        	break;
		default:
			break;
		}
		
		return super.onTouchEvent(event);
	}
	Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case 1:
				if(!finish)handler.sendEmptyMessage(1);
				else {
					finish=false;
					tem=(float)Math.sqrt(Math.sqrt(scale));
					count=0;
					handler.sendEmptyMessage(3);
				}
				break;
            case 2:
            	if(!finish)handler.sendEmptyMessage(2);
				else {
					finish=false;
					tem=(float)Math.sqrt(Math.sqrt(1.0f/scale));
					count=0;
					handler.sendEmptyMessage(3);
				}
            	break;
            case 3:
            	if(count<5){
            		Log.i("ads", tem+":"+count);
            		matrix.postScale(tem, tem, mCenterWidth, mCenterHeight);
            		setImageMatrix(matrix);
            		count++;
            		handler.sendEmptyMessage(3);
            	}else finish=true;
            	break;
			default:
				break;
			}
		};
	};
}

好了,两次谢谢原作者: 鸿洋

猜你喜欢

转载自blog.csdn.net/ccc905341846/article/details/49999377
今日推荐