public class MultiTouch extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(new MultiTouchView(this)); } } class MultiTouchView extends SurfaceView implements Callback,Runnable{ private SurfaceHolder mSurfaceHolder; private Thread mThread; private Canvas mCanvas; private Paint mPaint; private boolean mFlag; private float mOldScaleRate=1; private float mScaleRate=1; private float mNewDistance; private float mOldDistance; private Bitmap mBitmap; private int mScreenW; private int mScreenH; public MultiTouchView(Context context) { super(context); mSurfaceHolder=this.getHolder(); mSurfaceHolder.addCallback(this); mPaint=new Paint(); mPaint.setColor(Color.WHITE); mPaint.setAntiAlias(true); mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon); setFocusable(true); setFocusableInTouchMode(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { mScreenW=this.getWidth(); mScreenH=this.getHeight(); mFlag=true; mThread=new Thread(this,"My Thread"); mThread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { mFlag=false; } @Override public void run() { while (mFlag) { long start=System.currentTimeMillis(); myDraw(); logic(); long end=System.currentTimeMillis(); if ((end-start)<50) { try { Thread.sleep(50-(end-start)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private void logic() { // TODO Auto-generated method stub } private void myDraw() { try { mCanvas=mSurfaceHolder.lockCanvas(); if (mCanvas != null) { mCanvas.drawColor(Color.WHITE); mCanvas.save(); mCanvas.scale(mScaleRate, mScaleRate, mScreenW/2, mScreenH/2); mCanvas.drawBitmap(mBitmap, mScreenW/2-mBitmap.getWidth()/2, mScreenH/2-mBitmap.getHeight()/2, mPaint); mCanvas.restore(); } } catch (Exception e) { // TODO: handle exception }finally{ if(mCanvas != null){ mSurfaceHolder.unlockCanvasAndPost(mCanvas); } } } @Override public boolean onTouchEvent(MotionEvent event) { requestFocus(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_UP: mOldScaleRate=mScaleRate; break; case MotionEvent.ACTION_POINTER_DOWN: mOldDistance = getEventSpace(event); break; case MotionEvent.ACTION_MOVE: if (event.getPointerCount()==2) { mNewDistance = getEventSpace(event); if (mOldDistance != 0) { mScaleRate = mOldScaleRate * mNewDistance / mOldDistance; } } return true; default: break; } return true; //return super.onTouchEvent(event); } private float getEventSpace(MotionEvent event) { float disX=event.getX(1)-event.getX(0); float disY=event.getY(1)-event.getY(0); return FloatMath.sqrt(disX*disX+disY*disY); } }
问题一:
当手指进行多点触摸的时候,无法进入相应的case语句进行执行,刚开始以为是没有加setFocusableInTouchMode(true);,但是发现即使加了这句话还是不能正常的执行,经过几次尝试发现是因为
@Override public boolean onTouchEvent(MotionEvent event) { requestFocus(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_UP: mOldScaleRate=mScaleRate; break; case MotionEvent.ACTION_POINTER_DOWN: mOldDistance = getEventSpace(event); break; case MotionEvent.ACTION_MOVE: if (event.getPointerCount()==2) { mNewDistance = getEventSpace(event); if (mOldDistance != 0) { mScaleRate = mOldScaleRate * mNewDistance / mOldDistance; } } return true; default: break; } return true; //return super.onTouchEvent(event); }应该返回return true;而不是return super.onTouchEvent(event);API解释:
case MotionEvent.ACTION_MOVE: mNewDistance = getEventSpace(event); if (mOldDistance != 0) { mScaleRate = mNewDistance / mOldDistance; } return true;之前是按照上面的方法做的,发现图片是可以进行放缩,但是放所的不正常
case MotionEvent.ACTION_MOVE: if (event.getPointerCount()==2) { mNewDistance = getEventSpace(event); if (mOldDistance != 0) { mScaleRate = mOldScaleRate * mNewDistance / mOldDistance; } } return true;改成上面的做法后正常了,我每次在抬起手指也就是触发MotionEvent.ACTION_POINTER_UP的时候应该将上一次的放缩比例保存起来,下一次计算放缩比例的时候应该在这个的基础上进行放缩。