我已经创建与图像视图和web视图的布局。 Web视图设置为具有的走了一个默认的知名度。当活动触发了它首先显示图像视图,并当纸幅视图已完成加载其URL,它本身标记为可见,并且为隐藏ImageView的标记。

当示出了ImageView的,我想它反复转动只是一点点加入潇洒。

我从来没有在Android和所有我发现,当我问互联网是没有帮助的帖子做动画之前;因此,我已经回到SO寻求帮助。

所以,如果我开始与这个...

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

如何创建一个反复旋转动画,并将其应用到ImageView的?

再次感谢!

有帮助吗?

解决方案

使用一个 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);

其他提示

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

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%)旋转。我张贴这种对未来的读者谁拿到这里从谷歌,因为我有,谁愿意绕其中心的图像没有绝对的定义像素上述中心。

您也可以简单地使用旋转动画功能。运行特定的动画,对于时间的预先确定的量,上ImageView的。

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

然后在你的资源创建一个动画的XML文件/阿尼姆称为与内容rotate_picture:

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

现在不幸的是,这将只运行一次。你需要一个循环的地方,使其重复动画,同时它的等待。我尝试一点点,陷入了无限循环我的计划,所以我不知道来的最好方法。编辑:克里斯托弗的回答提供了有关如何使循环正常,所以删除有关独立的线程我不好建议的信息

的一种方式 - 分割你图像分成N个每一次稍微旋转它。我会说5就足够了。 然后创建在抽拉像这样

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

此处

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

在元件放置:

android:repeatCount="infinite"

我发现,如果你使用.getWidth / 2等等,它不会工作,你需要得到像素的图像是数量和2分自己,然后只需在输入在过去2个参数数量。

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

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

和现在您的图像将枢转轮它的中心。

<强>验证码:

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>

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

RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top