2016-03-25 44 views
1

我創建繪圖應用程序,並且我有一個使用畫布繪製的視圖。我使用矩陣縮放圖像,當圖像放大並出現觸摸事件時,我嘗試獲取矩陣的逆矩陣,然後使用逆矩陣映射點以獲取畫布上的點motionevent點,但它不匹配它應該如何,並想知道我做錯了什麼,從屏幕觸摸位置獲取畫布位置。當使用矩陣縮放時,從運動事件位置獲取畫布上的位置

public class CustomDrawableView extends View { 
    private Paint mBitmapPaint; 
    private ShapeDrawable mDrawable; 
    private ScaleGestureDetector detector; 
    Matrix drawMatrix; 
    Bitmap bm; 

    @Override 
    protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      canvas.save();   
      canvas.drawBitmap(bm, drawMatrix, mBitmapPaint);   
      mDrawable.draw(canvas); 
      canvas.restore();  
    } 
    public Pair<Float,Float> GetRealValues(float eventX, float eventY){   
     Matrix inverse = new Matrix(); 
     drawMatrix.invert(inverse); 
     float[] point = {eventX,eventY}; 
     inverse.mapPoints(point);  
     return new Pair<>(point[0],point[1]); 
    } 

    @Override 
    //reads motions and calls methods to set starting and ending points and to 
    //draw canvas depending on the motion 
    public boolean onTouchEvent(MotionEvent event) { 
     //get the canvas location from the MotionEvent location  
     Pair<Float,Float> realvals = GetRealValues(event.getX(),event.getY()); 
     //drawing mode where you can draw or zoom in on canvas 
      if(drawing) { 
       //not zooming mode where you draw on canvas 
       if (!zoom) { 
        switch (event.getAction() & MotionEvent.ACTION_MASK) {        
         case MotionEvent.ACTION_DOWN: 
          //do stuff 
          break;        
         case MotionEvent.ACTION_MOVE: 
          //do stuff 
          break; 
         case MotionEvent.ACTION_UP: 
          //do stuff 
          break; 
         case MotionEvent.ACTION_POINTER_UP: 
          break; 
        } 
     } 
     //zoom mode where you can scale the canvas 
      if (zoom) { 
       detector.onTouchEvent(event);       
       invalidate(); 
      } 
     }   
    return true; 
} 



    public class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { 
     private float MIN_ZOOM = 1f; 
     private float MAX_ZOOM = 10f;  

     @Override 
     public boolean onScale(ScaleGestureDetector detector) { 
      Matrix transformationMatrix = new Matrix(); 
      scaleFactor *= detector.getScaleFactor(); 
      float tscale = detector.getScaleFactor(); 
      if(scaleFactor < MIN_ZOOM || scaleFactor > MAX_ZOOM){ 
       float prescaleFactor = scaleFactor/tscale; 
       scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM)); 
       tscale = scaleFactor/prescaleFactor; 
      } 
      transformationMatrix.postScale(tscale, tscale);    
      drawMatrix.postConcat(transformationMatrix); 
      return true; 
    } 
} 

回答

0

找到了答案。我的OnDraw方法需要是這樣

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
     canvas.save();   
     float[] mvals = new float[9]; 
     drawMatrix.getValues(mvals); 
     canvas.translate(mvals[Matrix.MTRANS_X], mvals[Matrix.MTRANS_Y]); 
     canvas.scale(mvals[Matrix.MSCALE_X], mvals[Matrix.MSCALE_Y]); 
     canvas.drawBitmap(bm, 0, 0, mBitmapPaint); 
     mDrawable.draw(canvas); 
     canvas.restore(); 
} 

我不知道爲什麼我需要翻譯,爲了單獨縮放畫布逆矩陣給我從觸摸點正確的畫布點,我米猜

canvas.drawBitmap(bm, drawMatrix, mBitmapPaint); 

輪矩陣值以整數因爲畫布點i。從所述逆矩陣的點映射得到繪製畫布時方式只是稍微偏離。

相關問題