package com.platomix;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
public class ZoomView extends ImageView implements OnTouchListener ,OnClickListener{
static final String TAG = "ZoomView";
private Bitmap bitmap;
private Bitmap resizeBitmap;
private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();
private float x0 = 0.0f;
private float y0 = 0.0f;
private float currScale = 1.0f;
private float currZoomScale ;
private float startX;
private float startY;
private float screenWidth;
private float screenHeight;
private float zoomoutScale = 1.0f;
private float zoominScale = 1.0f;
PointF point = new PointF();
/**
* 构造器
* */
public ZoomView(Context context) {
super(context);
this.setScaleType(ScaleType.MATRIX);
this.setOnTouchListener(this);
getScreen(context);
}
/**
* setter 和 getter
* */
public void setZoominScale(float scale){
this.zoominScale = scale;
}
public void setZoomoutScale(float scale){
this.zoomoutScale = scale;
}
public float getZoomoutScale() {
return zoomoutScale;
}
public float getZoominScale() {
return zoominScale;
}
public void setBitmap(Bitmap bmp){
this.bitmap = bmp;
this.setImageBitmap(bitmap);
}
public Bitmap getBitmap(){
return this.bitmap;
}
/**
* 获取屏幕的大小
* */
private PointF getScreen(Context context) {
WindowManager window = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = window.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
PointF point = new PointF();
point.set(screenWidth, screenHeight);
return point;
}
/**
* 监听ontuch事件,完成单手滑动操作
* */
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView imageView = (ImageView)v;
event.getDownTime();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - startX,event.getY() - startY);
break;
case MotionEvent.ACTION_UP:
/**
* 当手指离开屏幕时,校准图片的位置
* 1.当x坐标都小于0
* 2.当y坐标都小于0
* 3.当x坐标加图片的宽度大于屏幕的宽带
* 4.当y坐标加图片的高度大于屏幕的高度
* */
if(x0<=0){
matrix.postTranslate(Math.abs(x0), 0);
}
if(y0<=0){
matrix.postTranslate(0,Math.abs(y0));
}
if(resizeBitmap!=null){
if((x0+resizeBitmap.getWidth()*currZoomScale) >=screenWidth){
matrix.postTranslate(screenWidth-(x0+resizeBitmap.getWidth()*currZoomScale), 0);
}
if(y0+resizeBitmap.getHeight()*currZoomScale >=screenHeight){
matrix.postTranslate(0,screenHeight-(y0+resizeBitmap.getHeight()*currZoomScale));
}
}else{
if((x0+bitmap.getWidth())>=screenWidth){
matrix.postTranslate(screenWidth-(x0+bitmap.getWidth()), 0);
}
if((y0+bitmap.getHeight())>=screenHeight){
matrix.postTranslate(0,screenHeight-(y0+bitmap.getHeight()));
}
}
break;
}
imageView.setImageMatrix(matrix);
float[] location = new float[9];
matrix.getValues(location);
x0 = location[2];
y0 = location[5];
return true;
}
/**
* 图片的放大操作
* 当 ( 图片宽度*放大倍数 >= 屏幕宽度) 或 ( 图片 高度*放大倍数 >= 屏幕高度)
* 1.若图片的宽高比例大于屏幕的款宽高比例,此时图片的宽应先铺满屏幕,当前的放大比例应为 (屏幕的宽度/图片的宽度)
* 2.若图片的高宽比例大于屏幕的款高宽比例,此时图片的高应先铺满屏幕,当前的放大比例应为 (屏幕的高度/图片的高度)
* */
public void zoomOut(ImageView v){
System.out.println("++++++++++++++");
midPoint(point,true);
float tempScale = 1.0f;
float[] f= new float[9];
if(resizeBitmap.getWidth()* this.getZoomoutScale() >=screenWidth || resizeBitmap.getHeight()*this.getZoomoutScale() >=screenHeight){
if((bitmap.getWidth()/bitmap.getHeight())>=(screenWidth/screenHeight)){
tempScale = screenWidth/resizeBitmap.getWidth();
}else{
tempScale = screenHeight/resizeBitmap.getHeight();
}
float x2 = screenWidth - resizeBitmap.getWidth()*tempScale;
float y2 = screenHeight - resizeBitmap.getHeight()*tempScale;
f[0]=tempScale*currScale;
f[2]=x2/2;
f[4]=tempScale*currScale;
f[5]=y2/2;
currZoomScale = tempScale;
}else{
f[0]=this.getZoomoutScale()*currScale;
f[4]=this.getZoomoutScale()*currScale;
f[2]=point.x;
f[5]=point.y;
currZoomScale = this.getZoomoutScale();
}
f[1]=0.0f;
f[3]=0.0f;
f[6]=0.0f;
f[7]=0.0f;
f[8]=1.0f;
currScale = f[0];
matrix.setValues(f);
v.setImageMatrix(matrix);
}
/**
* 图片的缩小操作
* 当图片的宽或高小于指定的最小宽或高是,把当前的缩放比例设置为1
* */
public void zoomIn(ImageView v,float minWidth,float minHeight){
System.out.println("------------");
midPoint(point,false);
if(resizeBitmap.getWidth()< (minWidth/this.getZoominScale()) || resizeBitmap.getHeight()< (minHeight)/this.getZoominScale()){
currZoomScale = 1.0f;
return;
}
float[] f= new float[9];
f[0]=this.getZoominScale()*currScale;
f[1]=0.0f;
f[2]=point.x;
f[3]=0.0f;
f[4]=this.getZoominScale()*currScale;
f[5]=point.y;
f[6]=0.0f;
f[7]=0.0f;
f[8]=1.0f;
currScale= f[0];
currZoomScale = this.getZoominScale();
matrix.setValues(f);
v.setImageMatrix(matrix);
}
/**
* 设置图片居中显示
* */
private void midPoint(PointF point,boolean flag){
resizeBitmap = Bitmap.createBitmap(this.bitmap, 0, 0,
this.bitmap.getWidth(), this.bitmap.getHeight(), matrix, true);
float x2,y2;
if(flag){
x2 = screenWidth - resizeBitmap.getWidth()*this.getZoomoutScale();
y2 = screenHeight - resizeBitmap.getHeight()*this.getZoomoutScale();
}else{
x2 = screenWidth - resizeBitmap.getWidth()*this.getZoominScale();
y2 = screenHeight - resizeBitmap.getHeight()*this.getZoominScale();
}
point.set(x2 / 2, y2 / 2);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}