关于Android手势缩放圆球

jopen 12年前

这个主要是根据手势缩放图片功能改装的一个简易Demo.熟悉一下图形操作..

代码如下:
package hfut.gmm;  import android.app.Activity;  import android.content.Context;  import android.graphics.Canvas;  import android.graphics.Color;  import android.graphics.Matrix;  import android.graphics.Paint;  import android.graphics.PointF;  import android.os.Bundle;  import android.util.FloatMath;  import android.util.Log;  import android.view.MotionEvent;  import android.view.View;    public class MultiTouch extends Activity {       PointF start = new PointF();           private Matrix matrix=new Matrix();       private Matrix savedMatrix=new Matrix();         @Override   protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    MyView mv=new MyView(this);    this.setContentView(mv);   }    }   class MyView extends View{    static final int NONE = 0;         static final int DRAG = 1;         static final int ZOOM = 2;         int mode = NONE;        float oldDist = 1f;       float mr=0;       float mx;       float my;   public MyView(Context context) {    super(context);    // TODO Auto-generated constructor stub   }     @Override   public boolean onTouchEvent(MotionEvent event) {    // TODO Auto-generated method stub    Log.d("Infor", "触摸监听到了...");    switch(event.getActionMasked()){    case MotionEvent.ACTION_DOWN://单点触碰    mx=event.getX();    mr=50;    Log.d("Infor", "mx"+mx);     my=event.getY();    Log.d("Infor", "my"+my);    mode=this.DRAG;     break;         case MotionEvent.ACTION_POINTER_DOWN://多点触碰     oldDist=this.spacing(event);     if(oldDist>10f){          Log.d("Infor", "oldDist"+oldDist);       }    mode=ZOOM;    break;       case MotionEvent.ACTION_MOVE://移动     if(mode==DRAG){      Log.d("Infor", "单点移动。。。");      mx=event.getX();      my=event.getY();        }     else if(mode==ZOOM){      float newDist=spacing(event);         if(newDist>10){       mr=newDist/oldDist*100;       Log.d("Infor", "多点移动。。。");       Log.d("Infor", "newDist:"+newDist);      }     }    break;    case MotionEvent.ACTION_UP:     mode=this.NONE;    break;        case MotionEvent.ACTION_POINTER_UP:     mode=this.NONE;    break;    }    invalidate();     return true;   }   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);    }      @Override   protected void onDraw(Canvas canvas) {    // TODO Auto-generated method stub    super.onDraw(canvas);    Paint paint=new Paint();    paint.setColor(Color.BLUE);    canvas.drawCircle(mx, my, mr, paint);                                         }        }
展示结果如下:
关于Android手势缩放圆球 关于Android手势缩放圆球