2012-03-15 58 views
0

我試圖以兩種方式旋轉黑莓位圖黑莓位圖的旋轉

1 -

public static Bitmap rotateImage(Bitmap oldB, int angle) { 
    int w = oldB.getWidth(); 
    int h = oldB.getHeight(); 
    double angRad = (angle % 360) * (Math.PI/180); 
    Bitmap newB = new Bitmap(w, h); 
    int[] oldD = new int[w * h]; 
    int[] newD = new int[w * h]; 
    oldB.getARGB(oldD, 0, w, 0, 0, w, h); 

    int axisX = w/2; 
    int axisY = h/2; 

    for (int x = 0; x < oldD.length; x++) { 
     int oldX = x % w; 
     int oldY = x/w; 
     int op = oldX - axisX; 
     int adj = oldY - axisY; 
     double oldT = MathUtilities.atan2(op, adj); 
     double rad = Math.sqrt((op * op) + (adj * adj)); 
     double newT = oldT + angRad; 
     int newX = (int) MathUtilities.round((rad * Math.sin(newT)) 
       + (double) axisX); 
     int newY = (int) MathUtilities.round((rad * Math.cos(newT)) 
       + (double) axisY); 
     if (newX < 0 || newY < 0 || newX >= w || newY >= h) { 
      newD[x] = 0x00000000; 
     } else { 
      newD[x] = oldD[(newY * w) + newX]; 
     } 
    } 

    newB.setARGB(newD, 0, w, 0, 0, w, h); 
    return newB; 
} 

2 - 使用drawTexturedPath

------功能

第二種方式
private void drawRotatedBitmap(Graphics graphics, Bitmap bm, int angle, 
     int x, int y) { 
    int w = bm.getWidth(); 
    int h = bm.getHeight(); 
    double a = Math.toRadians(angle); 
    int x1 = (int) (x - h * Math.sin(a)); 
    int y1 = (int) (y + h * Math.cos(a)); 
    int x2 = (int) (x1 + w * Math.cos(a)); 
    int y2 = (int) (y1 + w * Math.sin(a)); 
    int x3 = (int) (x + w * Math.cos(a)); 
    int y3 = (int) (y + w * Math.sin(a)); 
    int xPts[] = { x, x1, x2, x3 }; 
    int yPts[] = { y, y1, y2, y3 }; 
    int fAngle = Fixed32.toFP(angle); 
    int dvx = Fixed32.cosd(fAngle); 
    int dux = -Fixed32.sind(fAngle); 
    int dvy = Fixed32.sind(fAngle); 
    int duy = Fixed32.cosd(fAngle); 

    graphics.drawTexturedPath(xPts, yPts, null, null, 0, 0, dux, dvx, duy, 
      dvy, bm); 

} 

------如何調用

Graphics graphics = Graphics.create(circleBmp); 
drawRotatedBitmap(graphics, , 45, 0, 0); 
circleBitmapField.setBitmap(circleBmp); 

第一種方式是太慢了,第二種方式繪製位圖在錯誤的位置

任何一個可以幫助我調整它們的方法嗎?或者有另一種快速準確地旋轉位圖的方法。

感謝您的幫助.....

回答

1

你需要塗用ImageManipulator類。查找here「如何」文檔。

+1

我試過使用它。但我想圍繞中心旋轉位圖,而ImageManipulator不支持它。 – 2012-03-15 09:14:32

+0

試過了。位圖不圍繞中心旋轉。此外,它不會在旋轉後保持原始大小。 – mrvincenzo 2012-10-29 12:21:13