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 に答える