多点触控
package com.zcp.ddck;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
public class MainActivity extends Activity {
//声明holder
SurfaceHolder holder;
//获取屏幕的宽度和高度
int width;
int height;
//图片的x,y周
int imageX;
int imageY;
//图片的宽度和高度
int imageW;
int imageH;
//bitmap对象
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//屏幕宽度和高度
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
width = metric.widthPixels; // 屏幕宽度(像素)
height = metric.heightPixels; // 屏幕高度(像素)
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.i);
imageW = bitmap.getWidth();
imageH = bitmap.getHeight();
//图片的位置
imageX = (width - imageW)/2;
imageY = (height - imageH)/2;
setContentView(new MySurface(this));
}
//surface
class MySurface extends SurfaceView implements SurfaceHolder.Callback{
public MySurface(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
holder.addCallback(this);
setFocusable(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
SetImage(1.0f, bitmap.getWidth(), bitmap.getHeight());
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
//绘制图片
public void SetImage(float scalse,int width,int height){
//得到画布
Canvas canvas = holder.lockCanvas();
//画布重绘
canvas.drawColor(Color.BLACK);
//设置画笔
Paint paint = new Paint();
//画矩形
canvas.drawRect(0, 0, width, height, paint);
//得到marix 类
Matrix matrix = new Matrix();
//设置缩放比例
matrix.postScale(scalse, scalse);
//得到缩放后的bitmap
Bitmap bafter = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
//得到新的bitmap的宽度和高度并且重新计算位置
imageW = bafter.getWidth();
imageH = bafter.getHeight();
imageX = (width - imageW)/2;
imageY = (height - imageH)/2;
//平移过去
canvas.translate(imageX, imageY);
//重新绘制bitmap
canvas.drawBitmap(bitmap, matrix, paint);
//解锁
holder.unlockCanvasAndPost(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
//获取触控点个数
int pointcount = event.getPointerCount();
//两个点代表双点触控
if(pointcount == 2){
//获取两个点的距离
float pointA = event.getX(0);
float PointB = event.getX(1);
//判断那个比较大
//如果前面小于后面换
if(pointA<PointB){
float t = pointA;
pointA = PointB;
PointB = t;
}
//只要手指不抬起来,就进行缩放
if(!(event.getAction()==MotionEvent.ACTION_UP)){
float scale = getScale(pointA, PointB)/3;
SetImage(scale,bitmap.getWidth(), bitmap.getHeight());
}
}
return super.onTouchEvent(event);
}
//缩放比
public float getScale(float pointA,float PointB){
float scale = pointA/PointB;
return scale;
}
}