Android ImageView动画

cak*_*rus 60 android android-animation

我创建了一个带有图像视图和Web视图的布局.Web视图设置为默认可见性已消失.当活动启动时,它首先显示图像视图,当Web视图完成加载其URL时,它将自身标记为可见,并将imageview标记为隐藏.

当显示图像视图时,我希望它反复旋转只是为了添加一点pizazz.

我之前从未在Android中做过动画,而且当我问互联网时我发现的所有帖子都没有帮助; 因此,我已经回到SO寻求帮助了.

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

    final ImageView splash = (ImageView)findViewById(R.id.splash);
Run Code Online (Sandbox Code Playgroud)

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

再次感谢!

Chr*_*Orr 103

使用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);
Run Code Online (Sandbox Code Playgroud)

  • 如何在这里设置相对轴点:new RotateAnimation(0f,350f,15f,15f); 或者我必须检查,如果屏幕是ldpi,mdpi或hdpi之前,并调整枢轴值的乘数?我也注意到,我的照片没有回到原来的位置.与原生的android微调器不同.(我想在图像视图中使用默认的android微调器,使其看起来就像原始的一样.) (5认同)

小智 50

如何围绕中心旋转图像:

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.
Run Code Online (Sandbox Code Playgroud)

这将导致图像围绕其中心旋转(宽度/高度的0.5%或50%).我正在为未来的读者发布此信息,这些读者来自谷歌,就像我一样,并希望围绕其中心旋转图像而不用绝对像素定义所述中心.

  • 这完全符合我的要求.只需要更改我需要的是指定Animation.RELATIVE_TO_SELF而不是'Dimension.RelativeToSelf'. (3认同)

Ste*_*ley 24

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

Animation rotate = AnimationUtils.loadAnimation([context], R.anim.rotate_picture);
splash.startAnimation(rotate);
Run Code Online (Sandbox Code Playgroud)

然后在你的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>
Run Code Online (Sandbox Code Playgroud)

现在不幸的是,这只会运行一次.你需要一个循环来让它在等待时重复动画.我试验了一下,让我的程序卡在无限循环中,所以我不确定最好的方法.编辑:克里斯托弗的答案提供了如何正确循环的信息,所以删除我关于单独的线程的坏建议!


Ale*_*voy 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> 
Run Code Online (Sandbox Code Playgroud)

代码开始

progress.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.setCallback(progress);
frameAnimation.setVisible(true, true);
Run Code Online (Sandbox Code Playgroud)

代码停止

AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.stop();
frameAnimation.setCallback(null);
frameAnimation = null;
progress.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

更多在这里


小智 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);
Run Code Online (Sandbox Code Playgroud)


And*_*zie 5

在元素内放:

android:repeatCount="infinite"
Run Code Online (Sandbox Code Playgroud)