64

我创建了一个带有图像视图和 Web 视图的布局。Web 视图设置为具有默认可见性消失。当 Activity 启动时,它首先显示图像视图,当 Web 视图完成加载其 url 时,它会将自身标记为可见,而图像视图则标记为隐藏。

当显示图像视图时,我希望它反复旋转以增加一点活力。

我以前从未在 Android 中做过动画,当我在网上询问时发现的所有帖子都没有帮助;因此,我已经返回 SO 寻求帮助。

所以如果我从这个开始......

    final ImageView splash = (ImageView)findViewById(R.id.splash);

如何创建重复旋转动画并将其应用于 ImageView?

再次感谢!

4

9 回答 9

109

使用 a RotateAnimation,将轴心点设置为图像的中心。

RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

// Start animating the image
final ImageView splash = (ImageView) findViewById(R.id.splash);
splash.startAnimation(anim);

// Later.. stop the animation
splash.setAnimation(null);
于 2010-01-09T13:20:55.980 回答
53

如何围绕其中心旋转图像:

ImageView view = ... //Initialize ImageView via FindViewById or programatically

RotateAnimation anim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

//Setup anim with desired properties
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE); //Repeat animation indefinitely
anim.setDuration(700); //Put desired duration per anim cycle here, in milliseconds

//Start animation
view.startAnimation(anim); 
//Later on, use view.setAnimation(null) to stop it.

这将导致图像围绕其中心旋转(其宽度/高度的 0.5 或 50%)。我将这篇文章发布给未来的读者,他们像我一样从谷歌来到这里,并且希望围绕其中心旋转图像而不用绝对像素定义所述中心。

于 2012-08-17T13:58:51.973 回答
26

您也可以简单地使用旋转动画功能。它在 ImageView 上运行特定的动画,持续预定的时间。

Animation rotate = AnimationUtils.loadAnimation([context], R.anim.rotate_picture);
splash.startAnimation(rotate);

然后在 res/anim 中创建一个名为 rotate_picture 的动画 XML 文件,其内容为:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false">

    <rotate 
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="5000"
    android:pivotX="50%"
    android:pivotY="50%">
</rotate>
</set>

现在不幸的是,这只会运行一次。您需要在某处循环以使其在等待时重复动画。我做了一些实验,让我的程序陷入无限循环,所以我不确定最好的方法。编辑:克里斯托弗的回答提供了有关如何使其正确循环的信息,因此请删除我对单独线程的坏建议!

于 2010-01-09T13:08:00.143 回答
11

一种方法 - 将你的图像分成 N 每次稍微旋转它。我说5个就够了。然后在drawable中创建类似的东西

<animation-list   android:id="@+id/handimation" android:oneshot="false" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/progress1" android:duration="150" />
    <item android:drawable="@drawable/progress2" android:duration="150" />
    <item android:drawable="@drawable/progress3" android:duration="150" />
 </animation-list> 

代码开始

progress.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.setCallback(progress);
frameAnimation.setVisible(true, true);

代码停止

AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.stop();
frameAnimation.setCallback(null);
frameAnimation = null;
progress.setVisibility(View.GONE);

更多在这里

于 2010-01-09T04:28:53.320 回答
7
imgDics = (ImageView) v.findViewById(R.id.img_player_tab2_dics);
    imgDics.setOnClickListener(onPlayer2Click);
    anim = new RotateAnimation(0f, 360f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                            0.5f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(4000);

    // Start animating the image
    imgDics.startAnimation(anim);
于 2013-09-19T06:31:48.193 回答
5

在元素里面放:

android:repeatCount="infinite"
于 2011-04-09T14:32:25.447 回答
3

我发现,如果您使用 .getWidth/2 等...它不起作用,您需要获取图像的像素数并将其除以 2,然后只需输入数字最后 2 个参数。

所以说你的图像是一个 120 像素 x 120 像素的正方形,你的 x 和 y 将等于 60 像素。所以在你的代码中,你是对的:

RotateAnimation anim = new RotateAnimation(0f, 350f, 60f, 60f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

现在您的图像将围绕其中心旋转。

于 2012-04-07T02:12:31.143 回答
2

验证码:

imageView.setImageResource(R.drawable.ic_arrow_up);

boolean up = true;

if (!up) { 
    up = true; 
    imageView.startAnimation(animate(up)); 
} else { 
    up = false; 
    imageView.startAnimation(animate(up)); 
}

private Animation animate(boolean up) {
    Animation anim = AnimationUtils.loadAnimation(this, up ? R.anim.rotate_up : R.anim.rotate_down);
    anim.setInterpolator(new LinearInterpolator()); // for smooth animation
    return anim;
}

可绘制/ic_arrow_up.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#3d3d3d"
        android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
</vector>

动画/rotate_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <rotate
        android:duration="200"
        android:fromDegrees="-180"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="0" />
</set>

动画/rotate_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <rotate
        android:duration="200"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="180" />
</set>
于 2018-03-12T14:39:46.773 回答
0

不要硬编码图像边界。只需使用:

RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
于 2015-03-11T12:28:06.773 回答