通过自定义ImageView来实现多点触控
public class Main extends Activity {private MyImageView imageView;// 声明一个自定义的ImageViewprivate Bitmap bitmap;// 两点触屏后之间的长度private float beforeLenght;private float afterLenght;TextView mtxtNext;// 单点移动的前后坐标值private float afterX, afterY;private float beforeX, beforeY;Button btn1;@Overridepublic void onCreate(Bundle savedInstanceState) {requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);imageView = new MyImageView(this);bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.mm)).getBitmap();// 得到图片setContentView(R.layout.main);setContentView(imageView);// 设置imageView的显示图片imageView.setImageBitmap(bitmap);// 设置图片填充ImageViewimageView.setScaleType(ScaleType.FIT_XY);}// 触屏事件@Overridepublic boolean onTouchEvent(MotionEvent event) {if (event.getY() > imageView.getTop()&& event.getY() < imageView.getBottom()&& event.getX() > imageView.getLeft()&& event.getX() < imageView.getRight()) //{// 判定用户是否触摸到了图片if (event.getPointerCount() == 2) {// 2点触控imageView.scaleWithFinger(event);// 调用控制图片大小的方法} else if (event.getPointerCount() == 1) {// 单点触摸imageView.moveWithFinger(event);// 调用控制图片移动的方法}}return true;}/** * 自定义ImageView类 */class MyImageView extends ImageView {private float scale = 0.1f;public MyImageView(Context context) {super(context);}// 用来设置ImageView的位置private void setLocation(int x, int y) {this.setFrame(//this.getLeft() + x, //this.getTop() + y, //this.getRight() + x, //this.getBottom() + y);//}// 用来放大缩小ImageView 因为图片是填充ImageView的,所以也就有放大缩小图片的效果private void setScale(float temp, int flag) {if (flag == 0) {// 放大图片this.setFrame(//this.getLeft() - (int) (temp * this.getWidth()),//this.getTop() - (int) (temp * this.getHeight()), //this.getRight() + (int) (temp * this.getWidth()), //this.getBottom() + (int) (temp * this.getHeight()));//} else {// 缩小图片this.setFrame(//this.getLeft() + (int) (temp * this.getWidth()),//this.getTop() + (int) (temp * this.getHeight()),//this.getRight() - (int) (temp * this.getWidth()), //this.getBottom() - (int) (temp * this.getHeight()));//}}// 绘制边框@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Rect rect = canvas.getClipBounds();rect.bottom--;rect.right--;Paint paint = new Paint();paint.setColor(Color.BLACK);paint.setStyle(Paint.Style.STROKE);canvas.drawRect(rect, paint);}/* * 让图片跟随手指触屏的位置移动 beforeX、Y是用来保存前一位置的坐标 afterX、Y是用来保存当前位置的坐标 * 它们的差值就是ImageView各坐标的增加或减少值 */public void moveWithFinger(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:beforeX = event.getX();beforeY = event.getY();break;case MotionEvent.ACTION_MOVE:afterX = event.getX();afterY = event.getY();this.setLocation((int) (afterX - beforeX),(int) (afterY - beforeY));beforeX = afterX;beforeY = afterY;break;case MotionEvent.ACTION_UP:break;}}// 通过多点触屏放大或缩小图像 beforeLenght用来保存前一时间两点之间的距离 afterLenght用来保存当前时间两点之间的距离public void scaleWithFinger(MotionEvent event) {float X = event.getX(1) - event.getX(0);float Y = event.getY(1) - event.getY(0);switch (event.getAction()) {case MotionEvent.ACTION_DOWN:beforeLenght = (float) Math.sqrt((X * X) + (Y * Y));break;case MotionEvent.ACTION_MOVE:afterLenght = (float) Math.sqrt((X * X) + (Y * Y));// 得到两个点之间的长度float gapLenght = afterLenght - beforeLenght;if (gapLenght == 0) {break;}// 如果当前时间两点距离大于前一时间两点距离,则传0,否则传1if (gapLenght > 0) {this.setScale(scale, 0);} else {this.setScale(scale, 1);}beforeLenght = afterLenght;break;}}}}
?