0

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

        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();
4

2 回答 2

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();
    }
}
于 2013-09-24T10:38:04.950 回答
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);
于 2013-09-24T10:56:12.300 回答