Android Bitmap 旋轉 (Picture旋轉)

Android 透過 Matrix 物件, 來設定 Bitmap 旋轉.

如下範例 :
 程式碼
public class helloWorld extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( new drawCanvas(this) );
}

class drawCanvas extends View {
public drawCanvas(Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

// 取得 Resource 圖片的 Bitmap
Bitmap vBitmap = BitmapFactory.decodeResource( this.getResources()
, R.drawable.icon
);

// Bitmap 旋轉
Matrix vMatrix = new Matrix();
vMatrix.setRotate( 45 );

Bitmap vB2 = Bitmap.createBitmap( vBitmap
, 0
, 0
, vBitmap.getWidth()
// 寬度
, vBitmap.getHeight() // 高度
, vMatrix
, true
);


canvas.drawBitmap( vBitmap, 50, 100, null ); // 無旋轉
canvas.drawBitmap( vB2, 50, 200, null ); // 有旋轉
}
}
}

範例結果 :


Related Posts Plugin for WordPress, Blogger...