Android:围绕中心旋转图像 [英] Android: Rotation of image around the center

查看:63
本文介绍了Android:围绕中心旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试围绕中心旋转图像.这通常使用 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.

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

This is code, which draws the bitmap correctly (depending on the outer "heading")

航向 = 以度为单位的角度,位图 = 位图,w = 位图的宽度,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);

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

Drawback: The image is a circle and the code above produces visible aliasing effects...

下面的代码也在旋转图像,但在旋转时(比如顺时针从 0 度到 45 度),新图像的中心会向下/向右移动.我想,偏心效果是由于新图像的扩大宽度/高度?但是,如果设置了 filter=true,则此代码不会产生别名.有没有办法使用代码 #1 但有抗锯齿或使用代码 #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);

更新:根据该线程中的讨论,代码 #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);

这篇关于Android:围绕中心旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆