2012-03-15 111 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