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

发布于 2024-10-20 23:34:46 字数 382 浏览 5 评论 0原文

我想围绕一个固定点连续旋转图像 360 度。我已经看到了一些例子,例如:

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

这确实会旋转图像,但它是在弧形/圆形路径上旋转的。 IE。图像以圆周运动移动/旋转,但并未固定在其起始位置。

我基本上想要的是模仿风车手臂的旋转。

有什么想法吗?

I'd like to rotate an image 360 degrees continuously around a fixed point. I've seen a few examples already such as:

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

This does rotate the image, but it does so on an arc/circular path. Ie. the image is moving/rotating in a circular motion but isn't staying fixed at it's starting location.

What I basically want is to mimic the rotation of a WindMill's arms.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有伤那来痛 2024-10-27 23:34:46

使用此代码,

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);

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

Use this code

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);

this will rotate your image on its fixed position, i.e. around itself

鹤仙姿 2024-10-27 23:34:46

好的,经过一些调整后我完美地工作了。正如 Macarse 所说,它确实涉及 ImageView 周围的填充。

要解决此问题,您所需要做的就是将 ImageView 放入 RelativeLayout 中:

<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>

Ok so I got this working perfectly after some tweaking. Like Macarse said, it did involve the padding around the ImageView.

To fix this issue, all you have to do is put your ImageView inside a RelativeLayout:

<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>
夏见 2024-10-27 23:34:46

我会说将 imageview.. 的枢轴点设置为 x = imgView.getWidth()/2 和 y = imgView.getHeight()/2

i will say set the pivot point of imageview.. to the x = imgView.getWidth()/2, and y = imgView.getHeight()/2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文