簡體   English   中英

在Android畫布上旋轉位圖

[英]Rotate Bitmap on Android Canvas

我有一些對象作為SurfaceView的一部分繪制到了Canvas上。 我希望能夠以編程方式旋轉這些,例如myParticle.setRotation(90); 這是我目前(簡化)的繪制粒子的代碼:

public class Particle {

  public void draw(Canvas canvas){
    image.setBounds((int)(xPos), (int)(yPos), (int)(xPos+radius), (int)(yPos+radius));
    image.draw(canvas);
  }

}

在我看來,這樣做比較干凈:

Matrix rotator = new Matrix();

// rotate around (0,0)
rotator.postRotate(90);

// or, rotate around x,y
// NOTE: coords in bitmap-space!
int xRotate = ...
int yRotate = ...
rotator.postRotate(90, xRotate, yRotate);

// to set the position in canvas where the bitmap should be drawn to;
// NOTE: coords in canvas-space!
int xTranslate = ...
int yTranslate = ...
rotator.postTranslate(xTranslate, yTranslate);

canvas.drawBitmap(bitmap, rotator, paint);

這樣,畫布就可以像以前一樣保持定向,並且您可以對矩陣進行更多處理,例如平移,縮放等,並且矩陣的內容封裝了操作的真正含義。

編輯 :埃迪想知道旋轉發生在哪一點。

編輯 :AndrewOrobator想知道如何設置畫布目標坐標

你只需要打電話

canvas.rotate(90) :) // 90 is degree.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM