2013-02-21 30 views
0

我目前正在繪製到我的應用程序中的屏幕。用戶可以繪製代表他們家的房間的矩形,但目前它們僅限於最初的空間。繪製到屏幕 - 需要能夠縮放和捏

什麼是使屏幕簡單地成爲一個更大的畫布上的「窗口」的最佳方式,用戶可以在繪圖時進行滾動,縮放等操作? (我很欣賞那裏必須是一個「引」觸摸模式和「移動」觸控模式)

此外,畫布是一個自定義視圖類:

class HomerView extends View { // the custom View for drawing on 
    // set up Bitmap, canvas, path and paint 
    private Bitmap myBitmap; // the initial image we turn into our canvas 
    private Canvas myCanvas; // the canvas we are drawing on 
    private Rect myRect; // the mathematical path of the lines we draw 
    private Paint myBitmapPaint; // the paint we use to draw the bitmap 

    // get the width of the entire tablet screen 
    private int screenWidth = getContext().getResources() 
      .getDisplayMetrics().widthPixels; 
    // get the height of the entire tablet screen 
    private int screenHeight = getContext().getResources() 
      .getDisplayMetrics().heightPixels; 

    public HomerView(Context context) { // constructor of HomerView 
     super(context); 
     myBitmap = Bitmap.createBitmap(screenWidth, screenHeight, 
       Bitmap.Config.ARGB_8888); // set our drawable space - the bitmap which becomes the canvas we draw on 
     myCanvas = new Canvas(myBitmap); // set our canvas to our bitmap which we just set up 
     myRect = new Rect(); // make a new rect 
     myBitmapPaint = new Paint(Paint.DITHER_FLAG); // set dither to ON in our saved drawing - gives better color interaction 
    } 

    protected void onDraw(Canvas canvas) { // method used when we want to draw something to our canvas 
     canvas.drawColor(Color.TRANSPARENT); // sets canvas colour 
     canvas.drawBitmap(myBitmap, 0, 0, myBitmapPaint); // save the canvas to bitmap - the numbers are the x, y coords we are drawing from 
     canvas.drawRect(myRect, myPaint); // draw the rectangle that the user has drawn using the paint we set up 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { // if screen size changes, alter the bitmap size 
     super.onSizeChanged(w, h, oldw, oldh); 
     /*myBitmap = Bitmap.createBitmap(screenWidth, screenHeight, 
       Bitmap.Config.ARGB_8888); 
     myCanvas = new Canvas(myBitmap);*/ 
    } 

      public void drawApplianceIcon() { 
     myCanvas.drawBitmap(bmp, iX - 50, iY - 50, myBitmapPaint); 
     makeToast("Lamp Drawn To Canvas"); 
    } 


    // these variables are needed in touch listening 
    private int mX, mY, iX, iY; // current x,y and initial x,y 
    private static final float TOUCH_TOLERANCE = 4; 

private void touch_start {} // touch methods 
private void touch_move {} 
private void touch_up {} 

這是宣佈的onCreate像這個:

View drawingAreaView = new HomerView(this); 
setContentView(drawingAreaView); 

我該如何調整我的代碼,使它成爲一個可滾動的窗口?

回答

1

視圖初始化期間試試這個?

setHorizontalScrollBarEnabled(true); 
setVerticalScrollBarEnabled(true); 

我想你還需要http://developer.android.com/reference/android/view/ScaleGestureDetector.html

而有些類型的視邏輯包含所有的位置。

還發現它。

Android: Enable Scrollbars on Canvas-Based View

+0

感謝您的幫助!什麼是視口?我會看看你發佈的東西。 – 2013-02-21 22:07:26

+0

查看區域。我想你可以使用canvas.translate。 – 2013-02-21 23:07:17