3 回答
TA贡献1854条经验 获得超8个赞
希望以下代码序列对您有所帮助:
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config);
Canvas canvas = new Canvas(targetBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
canvas.drawBitmap(source, matrix, new Paint());
如果从以下方法检查以下方法 ~frameworks\base\graphics\java\android\graphics\Bitmap.java
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
Matrix m, boolean filter)
这将解释旋转和平移的作用。
TA贡献1876条经验 获得超7个赞
优化代码。
public static Bitmap RotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
要从资源获取位图:
Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);
TA贡献1853条经验 获得超6个赞
现在我们要完成游戏的设计,我回到了这个问题,我只是想发布对我有用的东西。
这是旋转矩阵的方法:
this.matrix.reset();
this.matrix.setTranslate(this.floatXpos, this.floatYpos);
this.matrix.postRotate((float)this.direction, this.getCenterX(), this.getCenterY());
(this.getCenterX()基本上是位图X位置+位图宽度/ 2)
以及绘制位图的方法(通过RenderManager类调用):
canvas.drawBitmap(this.bitmap, this.matrix, null);
因此直截了当是棘手的,但我感到有点奇怪,我无法让它setRotate紧随其后postTranslate。也许有人知道为什么这行不通?现在所有的位图都可以正确旋转,但是在位图质量上并没有降低一些:/
无论如何,谢谢您的帮助!
- 3 回答
- 0 关注
- 838 浏览
添加回答
举报