开发者

Scaling and rotating images on a Canvas on Android

How do you scale and rotate im开发者_运维技巧ages on a Canvas on Android?


This might help you do what you want to do.


To draw scaled and rotated images on an Android Canvas you need something like this:

Bitmap bitmap = BitmapFactory.decodeResource(
    getResources(), R.drawable.myimg );

Matrix matrix = new Matrix();
matrix.setRotate( 90.0f );     // Degrees
matrix.preScale( 1.5f, 1.5f ); // 1.0f would be no scaling

Canvas canvas = surfaceHolder.lockCanvas();

canvas.drawBitmap( bitmap, matrix, paint );

surfaceHolder.unlockCanvasAndPost( canvas );

Or, to keep hold of a scaled and rotated bitmap for later use, create one like this:

Bitmap newBitmap = Bitmap.createBitmap(
    oldBitmap, 0, 0, oldBitmap.getWidth(),
    oldBitmap.getHeight(), matrix, true );

More details: Canvas.drawBitmap, Matrix, Bitmap.createBitmap

Tutorial here: Canvas and Drawables


you can use sample code at developer site .in which particularly api demo will help u abt this topic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜