图片缩放

2023-05-31

要实现图片的缩放,首先要判断是否有两个触摸点,

然后要获得两个触摸点的当前的距离,还有设置上一次两触摸点的距离。

如果当前距离减去上一次的距离大于5,图片则是放大,

如果上一次的距离减去当前距离大于5,图片则是缩小,

case MotionEvent.ACTION_MOVE:

//移动图片

/*FrameLayout.LayoutParams lp=(android.widget.FrameLayout.LayoutParams) v1.getLayoutParams();

lp.leftMargin=(int) e.getX();

lp.topMargin=(int) e.getY();

v1.setLayoutParams(lp);*/

//缩放图片

if(e.getPointerCount()>=2){//如果两个点的时候才求距离

float offsetx=e.getX(0)-e.getX(1);

float offsety=e.getY(0)-e.getY(1);

current=(float) Math.sqrt(offsetx*offsetx+offsety*offsety);

if(lastdistance<0){

lastdistance=current;

}else{

if(lastdistance-current>5){//缩小

FrameLayout.LayoutParams lp=(android.widget.FrameLayout.LayoutParams) v1.getLayoutParams();

lp.width=(int) (v1.getWidth()*0.9);

lp.height=(int) (v1.getHeight()*0.9);

v1.setLayoutParams(lp);

lastdistance=current;

}else if(current-lastdistance>5){//放大

FrameLayout.LayoutParams lp=(android.widget.FrameLayout.LayoutParams) v1.getLayoutParams();

lp.width=(int) (v1.getWidth()*1.1);

lp.height=(int) (v1.getHeight()*1.1);

v1.setLayoutParams(lp);

lastdistance=current;

}

}

}

break;

《图片缩放.doc》

下载本文的Word格式文档,以方便收藏与打印。