图像旋转无法正常工作

时间:2013-09-24 10:21:04

标签: android matrix imageview

我正在使用矩阵显示图像。但是它围绕圆圈旋转,似乎(显然)每次都会改变坐标。我想在中心旋转它。这是我的代码。

        Matrix m = new Matrix();
        RectF r = new RectF(0, 0, im.getWidth(), im.getHeight());
        RectF rf = new RectF(0, 0, circleWidth, circleHeight);

        m.setRectToRect(r, rf, Matrix.ScaleToFit.CENTER);
         //185 is the half of imagesize.

        m.postRotate(angle, 185, 185);
        im.setImageMatrix(m);

        im.setScaleType(ScaleType.MATRIX);
        im.invalidate();

2 个答案:

答案 0 :(得分:0)

你是想让图像围绕它的中心旋转吗?我有一些旧代码可以做到这一点,但我对细节有点生疏。

我在工作代码与您的代码之间可以看到的唯一区别是,我使用方法ImageView.getImageMatrix()而不是创建new Matrix()

所以我的代码看起来更像是这样:

    // avgAngle is the angle of rotation
    // imageView is my ImageView containing the image to rotate

    int halfHeight = imageView.getHeight() / 2;
    int halfWidth = imageView.getWidth() / 2;

    // rotate image
    Matrix m = imageView.getImageMatrix();
    m.postRotate(avgAngle, halfWidth, halfHeight);
    imageView.postInvalidate();

我从动画线程中调用此代码 - 因此使用postInvalidate()强制它在主线程上发生。


它的价值,这里是完整的代码(它不是很好) - 也许它会有所帮助:

private Runnable Spinnamation = new Runnable()
{
    public void run()
    {
        // animating is a global boolean flag for the whole Activity
        // card is my ImageView
        // getAverageAngle() method gives the rotation per second

        animating = true;
        float avgAngle = getAverageAngle();
        int halfHeight = card.getHeight() / 2;
        int halfWidth = card.getWidth() / 2;

        long now = new Date().getTime();
        while ((now + 3000) > (new Date().getTime()))
        {
            // rotate image
            Matrix rotateMatrix = card.getImageMatrix();
            rotateMatrix.postRotate(avgAngle, halfWidth, halfHeight);
            card.postInvalidate();

            try
            {
                Thread.sleep(30);
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        animating = false;
    }
};

我只是通过这种方法来称呼它:

/**
 * Applies a spinning animation to the whole view.
 */
private void startSpinnamation()
{
    if (!animating)
    {
        new Thread(Spinnamation).start();
    }
}

答案 1 :(得分:0)

试试这个。它对你有用

int angle = 0;

            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                angle = 90;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                angle = 180;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                angle = 270;
            }

            Matrix mat = new Matrix();
            mat.postRotate(angle);