Android ImageView 动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2032304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:16:38  来源:igfitidea点击:

Android ImageView Animation

androidandroid-animation

提问by cakeforcerberus

I've created a layout with an image view and a web view. The web view is set to have a default visibility of gone. When the activity fires up it displays the image view first and when the web view has finished loading its url, it marks itself as visible and the imageview is marked as hidden.

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

When the imageview is shown, I would like it to rotate repeatedly just for a little added pizazz.

当显示图像视图时,我希望它重复旋转只是为了增加一点点活力。

I have never done animations before in Android and all the posts I found when I asked the internet were not helpful; thus, I have returned to SO for help.

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

So if I start with this...

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

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

How do I create a repeated rotate animation and apply it to the ImageView?

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

Thanks again!

再次感谢!

回答by Christopher Orr

Use a RotateAnimation, setting the pivot point to the centre of your image.

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

回答by Matt

How to rotate an image around its center:

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

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.

This will cause the image to rotate around its center (0.5 or 50% of its width/height). I am posting this for future readers who get here from Google, as I have, and who wish to rotate the image around its center without defining said center in absolute pixels.

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

回答by Steve Haley

You can also simply use the Rotate animation feature. That runs a specific animation, for a pre-determined amount of time, on an ImageView.

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

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

Then create an animation XML file in your res/anim called rotate_picture with the content:

然后在您的 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>

Now unfortunately, this will only run it once. You'll need a loop somewhere to make it repeat the animation while it's waiting. I experimented a little bit and got my program stuck in infinite loops, so I'm not sure of the best way to that. EDIT: Christopher's answer provides the info on how to make it loop properly, so removing my bad suggestion about separate threads!

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

回答by Alex Volovoy

One way - split you image into N rotating it slightly every time. I'd say 5 is enough. then create something like this in drawable

一种方法 - 将您的图像分成 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> 

code start

代码开始

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

code stop

代码停止

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

more here

更多在这里

回答by son nt

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

回答by Andrew Mackenzie

Inside the element put:

在元素里面放:

android:repeatCount="infinite"

回答by simply_immense

I have found out, that if you use the .getWidth/2 etc... that it won't work you need to get the number of pixels the image is and divide it by 2 yourself and then just type in the number for the last 2 arguments.

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

so say your image was a 120 pixel by 120 pixel square, ur x and y would equal 60 pixels. so in your code, you would right:

所以说你的图像是一个 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);

and now your image will pivot round its center.

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

回答by Ahamadullah Saikat

Verified Code:

验证码:

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

drawable/ic_arrow_up.xml

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

anim/rotate_up.xml

动画/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>

anim/rotate_down.xml

动画/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>

回答by nick shp

Don't hard code image bounds. Just use:

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

RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);