布局:
复制代码 代码如下:
<?xml version="1.0" enCoding="utf-8"?>
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:ID="@+ID/relativeLayout1"
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent" >
<button
androID:ID="@+ID/zoom_in"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_alignParentleft="true"
androID:layout_alignParenttop="true"
androID:text="zoom_in" />
<button
androID:ID="@+ID/zoom_out"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_alignParentRight="true"
androID:layout_alignParenttop="true"
androID:text="zoom_out" />
<ScrollVIEw
androID:ID="@+ID/imageContainer"
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
androID:layout_below="@+ID/zoom_in"
androID:fadingEdge="none"
androID:scrollbars="none" >
<horizontalscrollview
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
androID:layout_centerHorizontal="true"
androID:fadingEdge="none"
androID:scrollbars="none" >
<ImageVIEw
androID:ID="@+ID/imageVIEw"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_alignParenttop="true"
androID:layout_centerHorizontal="true"
androID:scaleType="matrix" />
</horizontalscrollview>
</ScrollVIEw>
</relativeLayout>
程序代码:
复制代码 代码如下:
package taokun.demo.MutiltouchDemo;
import androID.app.Activity;
import androID.graphics.Bitmap;
import androID.graphics.BitmapFactory;
import androID.graphics.Matrix;
import androID.graphics.PointF;
import androID.os.Bundle;
import androID.util.floatMath;
import androID.util.Log;
import androID.vIEw.MotionEvent;
import androID.vIEw.VIEw;
import androID.vIEw.VIEw.OnClickListener;
import androID.vIEw.VIEw.OntouchListener;
import androID.Widget.button;
import androID.Widget.ImageVIEw;
public class MutiltouchDemoActivity extends Activity implements OntouchListener,OnClickListener {
private static final String TAG = "touch" ;
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF start = new PointF();
PointF mID = new PointF();
float olddist;
private ImageVIEw vIEw;
private button zoomIn,zoomOut;
//button zoom
private float scaleWIDth = 1;
private float scaleHeight = 1;
private Bitmap bmp,zoomedBMP;
private int zoom_level = 0;
private static final double ZOOM_IN_SCALE = 1.25;//放大系数
private static final double ZOOM_OUT_SCALE = 0.8;//缩小系数
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
@OverrIDe
public voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
//放大按钮
zoomIn = (button) findVIEwByID(R.ID.zoom_in);
//缩小按钮
zoomOut = (button) findVIEwByID(R.ID.zoom_out);
zoomIn.setonClickListener(this);
zoomOut.setonClickListener(this);
vIEw = (ImageVIEw) findVIEwByID(R.ID.imageVIEw);
vIEw.setontouchListener(this);
//取得drawable中图片,放大,缩小,多点触摸的作用对象
bmp = BitmapFactory.decodeResource(MutiltouchDemoActivity.this.getResources(),R.drawable.splash);
}
public boolean ontouch(VIEw v,MotionEvent event) {
// Handle touch events here...
ImageVIEw vIEw = (ImageVIEw) v;
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
//设置拖拉模式
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(),event.getY());
Log.d(TAG,"mode=DRAG" );
mode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG,"mode=NONE" );
break;
//设置多点触摸模式
case MotionEvent.ACTION_POINTER_DOWN:
olddist = spacing(event);
Log.d(TAG,"olddist=" + olddist);
if (olddist > 10f) {
savedMatrix.set(matrix);
mIDPoint(mID,event);
mode = ZOOM;
Log.d(TAG,"mode=ZOOM" );
}
break;
//若为DRAG模式,则点击移动图片
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
// 设置位移
matrix.postTranslate(event.getX() - start.x,
event.getX() - start.x);
}
//若为ZOOM模式,则多点触摸缩放
else if (mode == ZOOM) {
float newdist = spacing(event);
Log.d(TAG,"newdist=" + newdist);
if (newdist > 10f) {
matrix.set(savedMatrix);
float scale = newdist / olddist;
//设置缩放比例和图片中点位置
matrix.postscale(scale,scale,mID.x,mID.y);
}
}
break;
}
// Perform the transformation
vIEw.setimageMatrix(matrix);
return true; // indicate event was handled
}
//计算移动距离
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);
}
// 计算中点位置
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);
}
//放大,缩小按钮点击事件
@OverrIDe
public voID onClick(VIEw v) {
if(v == zoomIn){
enlarge();
}else if (v == zoomOut) {
small();
}
}
//按钮点击缩小函数
private voID small() {
int bmpWIDth = bmp.getWIDth();
int bmpHeight = bmp.getHeight();
scaleWIDth = (float) (scaleWIDth * ZOOM_OUT_SCALE);
scaleHeight = (float) (scaleHeight * ZOOM_OUT_SCALE);
Matrix matrix = new Matrix();
matrix.postscale(scaleWIDth,scaleHeight);
zoomedBMP = Bitmap.createBitmap(bmp,bmpWIDth,bmpHeight,matrix,
true);
vIEw.setimageBitmap(zoomedBMP);
}
//按钮点击放大函数
private voID enlarge() {
try {
int bmpWIDth = bmp.getWIDth();
int bmpHeight = bmp.getHeight();
scaleWIDth = (float) (scaleWIDth * ZOOM_IN_SCALE);
scaleHeight = (float) (scaleHeight * ZOOM_IN_SCALE);
Matrix matrix = new Matrix();
matrix.postscale(scaleWIDth,scaleHeight);
zoomedBMP = Bitmap.createBitmap(bmp,
true);
vIEw.setimageBitmap(zoomedBMP);
} catch (Exception e) {
}
}
}
以上是内存溢出为你收集整理的android 多点触摸图片缩放的具体实现方法全部内容,希望文章能够帮你解决android 多点触摸图片缩放的具体实现方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)