2010-10-11 84 views
5

所以我的目標是水平翻轉圖像然後在畫布上繪製它。目前我使用的是canvas.scale(-1,1),它可以有效地工作並水平繪製圖像,但它也會與x軸值一起旋轉,在此位置之前x的位置將爲150,並且在我必須切換之後它以-150來呈現在同一個地方。Android canvas.scale(-1,1)

我的問題是,我怎樣才能使它在兩種情況下的x值都是150,而不必在縮放後調整x位置?有沒有一種更有效的方式來做到這一點,而不會影響性能?

回答

1

我用它像這樣不斷前應用轉換爲位圖定這樣的:

public void applyMatrix(Matrix matrix) { 
    mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, 
     mBitmap.getWidth(), mBitmap.getHeight(), matrix, true); 
} 
... 
Matrix matrix = new Matrix(); 
matrix.preScale(-1, 1); 
mSprite.applyMatrix(matrix); 
0

您是否試過重複canvas.scale(-1, 1)?它將有效地消除這種轉變,因爲兩個消極因素會產生積極的影響。

8

我知道這個問題是老了,但我偶然碰到了同樣的問題。在我的情況下,當繪製擴展ImageButton的類時,我不得不翻動畫布。幸運的是,這個特定案例的解決方案比我想象的更加優雅。簡單覆蓋onDraw(Canvas)方法如下:

@Override 
protected void onDraw(final Canvas canvas) { 

    // Scale the canvas, offset by its center. 
    canvas.scale(-1f, 1f, 
     super.getWidth() * 0.5f, super.getHeight() * 0.5f); 

    // Draw the button! 
    super.onDraw(canvas); 
} 
相關問題