安卓:旋转图像围绕中心 [英] Android: Rotation of image around the center
问题描述
我想绕中心的图像。这个工程一般采用RotateAnimation,但我想把它快一点。我现在使用SurfaceView模式有独立的绘画主题。
I'm trying to rotate an image around the center. This works generally using RotateAnimation, but I want to have it a bit faster. I'm now using the SurfaceView pattern with a separate drawing thread.
这是code,它正确绘制位图(取决于外部标题)
This is code, which draws the bitmap correctly (depending on the outer "heading")
标题=度角, 位图=位图, 瓦特=宽度的位图, 位图的H =身高
heading = angle in degrees, bitmap = the bitmap, w = width of the bitmap, h = height of the bitmap.
Matrix m = new Matrix();
m.preRotate(heading, w/2, h/2);
m.setTranslate(50,50);
canvas.drawBitmap(bitmap, m, null);
缺点:该图像是一个圆,在code以上会产生明显的锯齿现象......
Drawback: The image is a circle and the code above produces visible aliasing effects...
在code以下也旋转图像,但同时转动(从0到45度顺时针说)的新形象举措底部/右侧的中心。我想,偏心的效果是由于新的图像的放大宽/高?然而,这code不会产生混淆,如果过滤器= true的设置。有没有办法使用code#1,但有某种抗锯齿或使用code#2,但摆脱中心运动?
The code below is also rotating the image, but while rotating (say from 0 to 45 degrees clockwise) the center of the new image moves bottom/right. I suppose, the eccentric effect is due to the enlarged width/height of the new image ?? However, this code doesn't produce aliasing, if filter=true is set. Is there a way to use code #1 but have sort of anti-aliasing or use code #2 but getting rid of the center movement?
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);
更新:由于正确版本的code#在此线程的讨论2(抗锯齿的和的正确旋转)的结果是这样(的50,50省略偏移)
UPDATE: As result of the discussion in this thread the correct version of code #2 (anti-aliasing and correct rotation) would look like this (offset of 50,50 omitted):
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);
感谢。
推荐答案
查找原始图像的中心和新的形象和中心的使用:
Find the center of the original image and for the new image and center using that:
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);
这篇关于安卓:旋转图像围绕中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!