6

私のアプリケーションは、Canvasの周りにスプライトインスタンスを起動し、それが画面を横切ってax/y座標に向かって移動します。スプライトをその中心を中心に回転させて、目的の座標に向かわせることができるようにしたいと思います。スプライトシートを使用していますが、クリッピングに問題があります。私もたくさんの良い例を見つけましたが、私が探しているものを正確にカバーしているものは何もないようです。この例は非常に近いですが、効率を上げるためにImagePoolerクラスを使用しており、描画/回転のたびに画像を再読み込みすることはできません。ですから、誰かが私のスプライトシートをカットせずにプリロードされた画像を回転させる方法についてアイデアを持っていたら、私は非常に感謝するでしょう。

4

2 に答える 2

14

まず、キャンバスまたはマトリックスを使用できるスプライトを簡単に回転できます。

Matrix matrix = new Matrix();
matrix.postRotate(angle, (ballW / 2), (ballH / 2)); //rotate it
matrix.postTranslate(X, Y); //move it into x, y position
canvas.drawBitmap(ball, matrix, null); //draw the ball with the applied matrix

// method two 
canvas.save(); //save the position of the canvas
canvas.rotate(angle, X + (ballW / 2), Y + (ballH / 2)); //rotate the canvas' matrix
canvas.drawBitmap(ball, X, Y, null); //draw the ball on the "rotated" canvas
canvas.restore(); //rotate the canvas' matrix back
//in the second method only the ball was roteded not the entire canvas

それを目的地に向けるには、スプライトと目的地の間の角度を知る必要があります。

spriteToDestAngle =  Math.toDegrees(Math.atan2((spriteX - destX)/(spriteY - destY)));

今、あなたがする必要があるのは、スプライトの回転にこ​​の角度を使用し、さらにスプライトが最初に指す場所に依存するangleShiftのような定数でそれを調整することです。

これがうまくいくかどうかはわかりませんが、いくつかのアイデアが得られることを願っています...

于 2011-05-14T19:13:14.837 に答える
0

この参照を使用して 角度を計算します。

private double angleFromCoordinate(double lat1, double long1, double lat2,
        double long2) {

    double dLon = (long2 - long1);

    double y = Math.sin(dLon) * Math.cos(lat2);
    double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
            * Math.cos(lat2) * Math.cos(dLon);

    double brng = Math.atan2(y, x);

    brng = Math.toDegrees(brng);
    brng = (brng + 360) % 360;
    brng = 360 - brng;

    return brng;
}

次に、ImageViewをこの角度に回転させます

private void rotateImage(ImageView imageView, double angle) {

    Matrix matrix = new Matrix();
    imageView.setScaleType(ScaleType.MATRIX); // required
    matrix.postRotate((float) angle, imageView.getDrawable().getBounds()
            .width() / 2, imageView.getDrawable().getBounds().height() / 2);
    imageView.setImageMatrix(matrix);
}
于 2013-09-11T10:05:27.170 に答える