android 多点触摸图片缩放的具体实现方法
布局:
<?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, 0, 0, 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, 0, 0, bmpwidth, bmpheight, matrix,
true);
view.setimagebitmap(zoomedbmp);
} catch (exception e) {
}
}
}