2010-06-03 48 views
0

我有一個矩形尺寸的圖像,例如30 x 60像素 我想旋轉此圖像圍繞圖像的底部中心,即 我想在上面設置樞軸例如(15,60)像素。旋轉圖像aroound點

我使用一個繪圖和矩陣來完成這個任務, 無論我嘗試我總是最終圍繞圖像中心旋轉。

代碼是:

Bitmap bitmapOrg = BitmapFactory.decodeFile("/sdcard/DCIM/2010-06-01_15-32-42_821.jpg"); 

//浮動角=(角度+ 10.0f)%360.0f; 如果(空= bitmapOrg!) {

   int width = bitmapOrg.getWidth(); 
      int height = bitmapOrg.getHeight(); 
      int newWidth = 15; 
      int newHeight = 15; 

      // calculate the scale - in this case = 0.4f 
      float scaleWidth = ((float) newWidth)/width; 
      float scaleHeight = ((float) newHeight)/height; 

/*帆布C =新畫布(bitmapOrg); float px =; float py; c.rotate(角度,PX,PY)*/

   // createa matrix for the manipulation 
      Matrix matrix = new Matrix(); 
      // resize the bit map 
      matrix.postScale(scaleWidth, scaleHeight); 
      // rotate the Bitmap 

// matrix.postRotate(45);

   // recreate the new Bitmap 
      Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
           width, height, matrix, true); 

      // make a Drawable from Bitmap to allow to set the BitMap 
      // to the ImageView, ImageButton or what ever 
      BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 

      ImageView imageView = new ImageView(this); 

      // set the Drawable on the ImageView 
      imageView.setImageDrawable(bmd); 

      // center the Image 
      imageView.setScaleType(ScaleType.CENTER); 

// imageView.layout(100,300,0,0); // linLayout.addView(imageView);

   // add ImageView to the Layout 
      linLayout.addView(imageView, 
       new AbsoluteLayout.LayoutParams(
          LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 10, 30 
        ) 
      ); 

任何人都可以讓我知道如何得到這個糾正?

回答

0

是與基體的幫助,您可以旋轉圖像。在調用RotateBitmap()後,您可以隨時使用getBitmap()獲取位圖。

public class RotateBitmap { 

public static final String TAG = "RotateBitmap"; 
private Bitmap     mBitmap; 
private int      mRotation; 
private int      mWidth; 
private int      mHeight; 
private int      mBitmapWidth; 
private int      mBitmapHeight; 

public RotateBitmap(Bitmap bitmap, int rotation) 
{ 
    mRotation = rotation % 360; 
    setBitmap(bitmap); 
} 

public void setRotation(int rotation) 
{ 
    mRotation = rotation; 
    invalidate(); 
} 

public int getRotation() 
{ 
    return mRotation % 360; 
} 

public Bitmap getBitmap() 
{ 
    return mBitmap; 
} 

public void setBitmap(Bitmap bitmap) 
{ 
    mBitmap = bitmap; 

    if (mBitmap != null) { 
     mBitmapWidth = bitmap.getWidth(); 
     mBitmapHeight = bitmap.getHeight(); 
     invalidate(); 
    } 
} 

private void invalidate() 
{ 
    Matrix matrix = new Matrix(); 
    int cx = mBitmapWidth/2; 
    int cy = mBitmapHeight/2; 
    matrix.preTranslate(-cx, -cy); 
    matrix.postRotate(mRotation); 
    matrix.postTranslate(cx, cx); 

    RectF rect = new RectF(0, 0, mBitmapWidth, mBitmapHeight); 
    matrix.mapRect(rect); 
    mWidth = (int)rect.width(); 
    mHeight = (int)rect.height(); 
} 

public Matrix getRotateMatrix() 
{ 
    Matrix matrix = new Matrix(); 
    if (mRotation != 0) { 
     int cx = mBitmapWidth/2; 
     int cy = mBitmapHeight/2; 
     matrix.preTranslate(-cx, -cy); 
     matrix.postRotate(mRotation); 
     matrix.postTranslate(mWidth/2, mHeight/2); 
    } 

    return matrix; 
} 

public int getHeight() 
{ 
    return mHeight; 
} 

public int getWidth() 
{ 
    return mWidth; 
} 

public void recycle() 
{ 
    if (mBitmap != null) { 
     mBitmap.recycle(); 
     mBitmap = null; 
    } 
} 
}