画像を中心に回転させようとしていますか? それを行う古いコードがいくつかありますが、詳細については少しさびています。
私の作業コードとあなたのコードの唯一の違いは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();
}
}