您是否试图让图像围绕其中心旋转?我有一些旧代码可以做到这一点,但我对细节有点生疏。
我可以看到我的工作代码和你的代码之间的唯一区别是我使用该方法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();
}
}