Ans*_*hul 7 android smooth image rotation
我想基于用户点击10度旋转位图图像.在众多stackoverflow和谷歌答案之后,我尝试了Matrix旋转的各种组合.
然而,图像并没有像预期的那样真正旋转,并且给出了关于画布中心的旋转+振荡的抖动视图.为了测试,每次调用对象的绘制方法时,我都会将旋转角度增加10度(而不是点击次数).图像是一个对称的圆形[64x64包围矩形],我希望它在屏幕中心围绕它自己的中心像一个轮子一样旋转,但它会旋转并沿着对角线向右下方移动并以振荡方式向后移动到屏幕中心.
public void draw(Canvas canvas) {
Matrix matrix = new Matrix();
rotation += 10;
float px = this.viewWidth/2;
float py = this.viewHeight/2;
matrix.setRotate(rotation, bitmap.getWidth()/2, bitmap.getHeight()/2);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, getImgWidth(), getImgHeight(), matrix, true);
canvas.drawBitmap(newbmp, px - (getImgWidth()/2), py - (getImgHeight()/2), null);
}
Run Code Online (Sandbox Code Playgroud)
yoa*_*oah 18
这是一个例子.我打破了它的3个步骤.第一个平移移动位图,使其中心位于0,0然后旋转,最后将位图中心移动到画布上所需的位置.您不需要第二个位图.
Matrix matrix = new Matrix();
rotation += 10;
float px = this.viewWidth/2;
float py = this.viewHeight/2;
matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2);
matrix.postRotate(rotation);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, null);
Run Code Online (Sandbox Code Playgroud)
作为优化,在此方法之外创建一次Matrix,并通过调用替换创建 matrix.reset()
| 归档时间: |
|
| 查看次数: |
13652 次 |
| 最近记录: |