gpt4 book ai didi

Android:图像围绕中心旋转

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:42 25 4
gpt4 key购买 nike

我正在尝试围绕中心旋转图像。这通常使用 RotateAnimation 工作,但我想让它快一点。我现在使用带有单独绘图线程的 SurfaceView 模式。

这是正确绘制位图的代码(取决于外部“标题”)

航向 = 以度为单位的角度,位图 = 位图,w = 位图的宽度,h = 位图的高度。

    Matrix m = new Matrix();
m.preRotate(heading, w/2, h/2);
m.setTranslate(50,50);

canvas.drawBitmap(bitmap, m, null);

缺点:图像是一个圆,上面的代码会产生可见的锯齿效果......

下面的代码也在旋转图像,但是在旋转时(比如顺时针从 0 度到 45 度),新图像的中心移动到底部/右侧。我想,偏心效应是由于新图像的宽度/高度扩大所致??但是,如果设置了 filter=true,此代码不会产生别名。有没有办法使用代码 #1 但有某种抗锯齿或使用代码 #2 但摆脱中心移动?

    Matrix m = new Matrix();
m.preRotate(heading, w/2, h/2);
m.setTranslate(50,50);

Bitmap rbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, m, true);
canvas.drawBitmap(rbmp, 50, 50, null);

更新:作为此线程中讨论的结果,代码 #2 的正确版本(抗锯齿正确旋转)将如下所示(省略 50,50 的偏移):

        Matrix m = new Matrix();

m.setRotate(heading, w/2, h/2);
Bitmap rbpm = Bitmap.createBitmap(bitmap, 0, 0, w, h, m, true);
canvas.drawBitmap(rbpm, (w - rbpm.getWidth())/2, (h - rbpm.getHeight())/2, null);

谢谢。

最佳答案

找到原始图像的中心以及使用它的新图像和中心:

Matrix minMatrix = new Matrix();
//height and width are set earlier.
Bitmap minBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas minCanvas = new Canvas(minBitmap);

int minwidth = bitmapMin.getWidth();
int minheight = bitmapMin.getHeight();
int centrex = minwidth/2;
int centrey = minheight/2;

minMatrix.setRotate(mindegrees, centrex, centrey);
Bitmap newmin = Bitmap.createBitmap(minBitmap, 0, 0, (int) minwidth, (int) minheight, minMatrix, true);

minCanvas.drawBitmap(newmin, (centrex - newmin.getWidth()/2), (centrey - newmin.getHeight()/2), null);
minCanvas.setBitmap(minBitmap);

关于Android:图像围绕中心旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5182861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com