在Android中如何使用RotateAnimation围绕固定点旋转ImageView?

4

我希望能够让一张图片在固定点周围连续旋转360度。我已经看到一些示例,例如:

RotateAnimation anim = new RotateAnimation(0, 360,150,150);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(2000);
[imageview].startAnimation(anim);

这确实可以旋转图像,但是它是在一个弧形/圆形路径上旋转。也就是说,图像正在以圆形运动的方式移动/旋转,但并没有停留在其起始位置。

我想要的基本上是模仿风车叶片的旋转。

有什么想法吗?

3个回答

7
请使用这段代码。
RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation1.setInterpolator(new LinearInterpolator());
rotateAnimation1.setDuration(duration);
rotateAnimation1.setRepeatCount(0);
img.startAnimation(rotateAnimation1);

这会将您的图像在其固定位置上旋转,即围绕自身旋转。

我需要旋转处理程序,就像我将处理程序向下和向上拖动一样。 - Dhara Patel
最后想说什么? - Shirish Herwade

0

好的,经过一些调整,我已经完美地解决了这个问题。就像Macarse说的那样,它确实涉及到ImageView周围的填充。

要解决这个问题,你只需要在RelativeLayout中放置你的ImageView

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    >

    <ImageView
        android:id="@+id/imageview"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/image"
        />

</RelativeLayout>

-1
我会说设置ImageView的中心点为 x = imgView.getWidth()/2,y = imgView.getHeight()/2。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接