代码之家  ›  专栏  ›  技术社区  ›  cakeforcerberus

Android ImageView动画

  •  57
  • cakeforcerberus  · 技术社区  · 15 年前

    我已经创建了一个带有图像视图和web视图的布局。web视图的默认可见性设置为gone。当活动启动时,它首先显示图像视图,当web视图加载完其url后,它将自身标记为可见,而imageview标记为隐藏。

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

    我以前从来没有在安卓系统上做过动画,我问互联网时发现的所有帖子都没有帮助;因此,我已返回SO寻求帮助。

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

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

    再次感谢!

    9 回复  |  直到 14 年前
        1
  •  106
  •   Christopher Orr    15 年前

    使用 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);
    
        2
  •  53
  •   sherpya    9 年前

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

    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%)。我在这里发表这篇文章是为了将来的读者,他们和我一样从谷歌来到这里,希望在不定义绝对像素的情况下围绕图像中心旋转图像。

        3
  •  26
  •   Steve Haley    15 年前

    也可以简单地使用“旋转动画”功能。在ImageView上以预定的时间量运行特定动画的。

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

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

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

        4
  •  11
  •   makvalti Alex Volovoy    12 年前

    单向-将图像分割为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);
    

    更多 here

        5
  •  7
  •   son nt    11 年前
    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);
    
        6
  •  5
  •   Andrew Mackenzie    13 年前

    android:repeatCount="infinite"
    
        7
  •  3
  •   Fahim Parkar    12 年前

    我发现,如果使用.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);
    

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

        8
  •  2
  •   Ahamadullah Saikat    6 年前

    验证代码:

    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>
    

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

    <?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>
    
        9
  •  0
  •   nick shp    9 年前

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

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