準備一張名爲picture的圖片。
佈局文件main.xml不用編寫
在MyMultitouchDemo.java中:
package com.li.multitouch;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.widget.ImageView;
public class MyMultitouchDemo extends Activity {
private static final int SCALEBASIC = 3 ;// 調整的比率
private int imageX = 0 ; // 計算圖片的X軸
private int imageY = 0 ; // 計算圖片的Y軸
private SurfaceHolder holder = null ;
private int screenWidth = 0 ; //屏幕寬度
private int screenHeight = 0 ; //屏幕高度
private int imageWidth = 0 ; //圖片寬度
private int imageHeight = 0 ; //圖片高度
private Bitmap bitmap = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.requestWindowFeature(Window.FEATURE_NO_TITLE) ;
this.screenWidth = super.getWindowManager()
.getDefaultDisplay().getWidth() ; //獲得屏幕的寬度
this.screenHeight = super.getWindowManager()
.getDefaultDisplay().getHeight() ; //獲得屏幕的高度
this.bitmap = BitmapFactory.decodeResource(super.getResources(),
R.drawable.picture);
this.imageWidth = this.bitmap.getWidth() ; //獲得圖片的寬度
this.imageHeight = this.bitmap.getHeight() ; //獲得圖片的高度
this.imageX = (this.screenWidth - this.imageWidth) / 2 ;
this.imageY = (this.screenHeight - this.imageHeight) / 2 ;
super.setContentView(new MySurfaceView(this));
}
private class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public MySurfaceView(Context context) {
super(context);
MyMultitouchDemo.this.holder = super.getHolder() ;
MyMultitouchDemo.this.holder.addCallback(this) ;
super.setFocusable(true) ; // 獲得焦點,進行觸摸事件
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
MyMultitouchDemo.this.setImage(1.0f, 300, 450);
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
private void setImage(float scale, int width, int height) { // 改變之後修改圖片
Canvas canvas = MyMultitouchDemo.this.holder.lockCanvas() ; // 獲取畫布
Paint paint = new Paint() ;
canvas.drawRect(0, 0, MyMultitouchDemo.this.screenWidth,
MyMultitouchDemo.this.screenHeight, paint); //畫底色消除從影
Matrix matrix = new Matrix() ;
matrix.postScale(scale, scale) ; // 等量縮放
Bitmap target = Bitmap.createBitmap(MyMultitouchDemo.this.bitmap, 0, 0,
width, height, matrix, true);
this.imageWidth = target.getWidth() ;
this.imageHeight = target.getHeight() ;
this.imageX = (this.screenWidth - this.imageWidth) / 2 ;
this.imageY = (this.screenHeight - this.imageHeight) / 2 ;
canvas.translate(this.imageX, this.imageY) ; // 平移到指定的位置
canvas.drawBitmap(this.bitmap, matrix, paint) ;
MyMultitouchDemo.this.holder.unlockCanvasAndPost(canvas) ;// 解鎖,並提交圖象
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int pointCount = event.getPointerCount() ;
if (pointCount == 2) {
float pointA = event.getY(0) ;
float pointB = event.getY(1) ;
if(pointA < pointB) {
float temp = pointA ;
pointA = pointB ;
pointB = temp ;
}
if(!(event.getAction() == MotionEvent.ACTION_UP)) {
float scale = this.getScale(pointA, pointB) / SCALEBASIC;
MyMultitouchDemo.this.setImage(scale, 300,450) ;
}
}
return super.onTouchEvent(event);
}
private float getScale(float pointA,float pointB) {
float scale = pointA / pointB ;
return scale ;
}
}