2012-09-06 80 views
2

我正在繪製位圖,並嘗試使用Canvas.for繪圖做一些繪製我添加 路徑,每當用戶在它上面繪製的東西。我已經實現了縮放縮放並將特徵拖放到位圖上,但當用戶縮放位圖並在其上繪製某些內容時,Path無法正確繪製。我正在使用矩陣捏縮放和拖動功能。 我的問題是與矩陣一起使用路徑。安卓繪圖路徑,縮放和捏

+0

發佈您的代碼請 – kinghomer

回答

1

這是一個工作的例子,你可以看到如何實現拖動和縮放路徑:

MainActivity

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.WindowManager; 

public class DrawActivity extends Activity { 

     DrawView drawView; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // Set full screen view 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
           WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 

      drawView = new DrawView(DrawActivity.this); 

      setContentView(drawView); 
     }  
} 

drawView函數

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.util.DisplayMetrics; 
import android.view.Display; 
import android.view.MotionEvent; 
import android.view.ScaleGestureDetector; 
import android.view.View; 
import android.view.WindowManager; 

public class DrawView extends View { 

    Context ctx; 

    static final String TAG = "DrawView"; 

    Paint paint = new Paint(); 

    //These two constants specify the minimum and maximum zoom 
    private static float MIN_ZOOM = 3f; 
    private static float MAX_ZOOM = 10f; 

    private float scaleFactor = 3.f; 
    private ScaleGestureDetector detector; 

    //These two variables keep track of the X and Y coordinate of the finger when it first 
    //touches the screen 
    private float startX = 0f; 
    private float startY = 0f; 

    //These two variables keep track of the amount we need to translate the canvas along the X 
    //and the Y coordinate 
    private float translateX = 0f; 
    private float translateY = 0f; 

    //These two variables keep track of the amount we translated the X and Y coordinates, the last time we 
    //panned. 
    private float previousTranslateX = 0f; 
    private float previousTranslateY = 0f; 

    private boolean dragged = false; 

// Used for set first translate to a quarter of screen 
    private float displayWidth; 
    private float displayHeight; 

    public DrawView(Context context) 
    { 
     super(context); 

     ctx = context; 

     WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
     Display display = wm.getDefaultDisplay(); 

     DisplayMetrics metrics = new DisplayMetrics(); 

     display.getMetrics(metrics); 

     displayWidth = metrics.widthPixels; 
     displayHeight = metrics.heightPixels; 

     translateX = displayWidth/4; 
     translateY = displayHeight/4; 

     previousTranslateX = displayWidth/4; 
     previousTranslateY = displayHeight/4; 

     detector = new ScaleGestureDetector(context, new ScaleListener()); 

     setFocusable(true); 
     setFocusableInTouchMode(true);  

// Path's color 
     paint.setColor(Color.GRAY); 
     paint.setAntiAlias(false); 
    } 

    @Override 
    public void onDraw(Canvas canvas) {  
     super.onDraw(canvas); 

     canvas.save(); 

     //We're going to scale the X and Y coordinates by the same amount 
     canvas.scale(scaleFactor, scaleFactor, 0, 0); 

     //We need to divide by the scale factor here, otherwise we end up with excessive panning based on our zoom level 
     //because the translation amount also gets scaled according to how much we've zoomed into the canvas. 
     canvas.translate((translateX)/scaleFactor, (translateY)/scaleFactor); 

     canvas.drawRect(0, 0, 100, 100, paint); 

     canvas.restore(); 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     switch (event.getAction() & MotionEvent.ACTION_MASK) { 

     case MotionEvent.ACTION_DOWN: 

// First finger is on screen 

      //We assign the current X and Y coordinate of the finger to startX and startY minus the previously translated 
      //amount for each coordinates This works even when we are translating the first time because the initial 
      //values for these two variables is zero. 
      startX = event.getX() - previousTranslateX; 
      startY = event.getY() - previousTranslateY; 
      break; 

     case MotionEvent.ACTION_MOVE: 

// first finger is moving on screen 
// update translation value to apply on Path 

      translateX = event.getX() - startX; 
      translateY = event.getY() - startY;    

      break; 

     case MotionEvent.ACTION_UP: 

// No more fingers on screen 

      // All fingers went up, so let's save the value of translateX and translateY into previousTranslateX and 
      //previousTranslate 
      previousTranslateX = translateX; 
      previousTranslateY = translateY; 
      break; 

// All touch events are sended to ScaleListener 
     detector.onTouchEvent(event); 

     return true; 
    } 

    class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { 

     @Override 
     public boolean onScale(ScaleGestureDetector detector) { 


      scaleFactor *= detector.getScaleFactor(); 
      scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM)); 

      invalidate(); 
      return true; 
     } 
    } 
} 
+0

我有一個相同的問題 – jack