2011-09-01 293 views
1

我的應用程序在3x3網格上有9個位圖圖像。所有這些都顯示在屏幕上,取決於用戶與它們的交互方式(點擊,雙擊,捏,縮放,拖動)「執行操作」的圖像需要執行不同的任務。Android縮放位圖圖像

因此,例如,如果我點擊一個圖像,它需要完全旋轉垂直訪問,然後停止。

目前我正在使用SurfaceView和onTouchEvent(..)方法。我想通過這種方式我可以對此進行動畫處理,方法是將圖像的寬度從1縮放到-1回到1.我一直在嘗試使用矩陣,但無法讓圖像保持居中。請幫我解決這個問題,或者建議一個替代/更好的方法來完成這個。

我現在的代碼只是一個測試。如果b爲真,則該方法將嘗試縮放位圖。如果b是假的,然後它會得出正常位圖:

public void doDraw(Canvas canvas, boolean b) 
{ 
    Matrix m = new Matrix(); 

    float scale = .5f; 
    m.setScale(scale, 1f); 
    m.postTranslate(mX,mY); 

    if(!b) 
     canvas.drawBitmap(mBitmap, mX, mY, null); 
    else 
     canvas.drawBitmap(mBitmap, m, null); 
} 

回答

1

嘗試,而不是這個postTranslate

matrix.preTranslate(-centerX, -centerY); 
matrix.postTranslate(centerX, centerY); 

其中的centerX,Y是位圖的中心

UPDATE

你可以在android上使用graphics.camera來轉換矩陣。將相機訪問到主視圖(網格)並覆蓋getChildStaticTransformation。獲取轉換矩陣。 t.getMatrix()並改變那一個。你可以將它移動到任何你想要的地方,因爲這是來自主視圖的矩陣,但是僅針對呈現該特定的孩子。

所以做這樣的事情

@Override 
    protected boolean getChildStaticTransformation(View child, Transformation t) { 
     Matrix matrix = t.getMatrix(); 
      int centerX = (child.getWidth()/2); 
      int centerY = (child.getHeight()/2); 
     t.clear(); 
     t.setTransformationType(Transformation.TYPE_MATRIX); 
     mCamera.save(); 

     if (child == getChildAt(0)) { 
      mCamera.translate(pixels to the right,pixels to the bottom, 
        to the z axis); 
      mCamera.getMatrix(matrix); 
      mCamera.restore(); 
      matrix.preTranslate(-centerX, -centerY); 
      matrix.postTranslate(centerX, centerY); 

     } 

     return true; 
    } 

而且不要忘記設置setStaticTransformationsEnabled(true);網格的構造函數的某處

+0

我假設你的意思是這樣的: 'float scale = -1f; \t m.preTranslate(-mX,-mY); \t m.setScale(scale,1f); \t m.postTranslate(mX,mY);' 但這並不奏效。我也嘗試過訂購量表,預翻譯,翻譯後。沒有任何內容 – Sababado

+0

它使圖像與左邊緣對齊。我需要它被集中。 – Sababado

+0

發佈更新了看一看 – weakwire