android:通过xml按下动画按钮

时间:2014-02-28 09:43:17

标签: android animation

在Android应用中,我试图动画一个按钮。我提到了这个: In Android, Display pressed button animation in OnClick?

这是按钮图像:

enter image description here

触摸时,我想要显示旋转动画。为此,我创建了相同的旋转角度,如下所示:

enter image description here enter image description here enter image description here

我创建了一个这样的自定义button.xml:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@anim/anim_refresh_button">    </item>
    <item android:state_focused="true" android:drawable="@drawable/icon_refresh">    </item>
    <item android:drawable="@drawable/icon_refresh"></item>
</selector>

然后,对于动画,我创建了anim_refresh_button.xml(我在上面'state_pressed'中提到过):

 <?xml version="1.0" encoding="utf-8"?>
 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  android:oneshot="true">

   <item android:drawable="@drawable/icon_refresh_1" android:duration="500"/>
    <item android:drawable="@drawable/icon_refresh_2" android:duration="500" />
    <item android:drawable="@drawable/icon_refresh_3" android:duration="500" />
    <item android:drawable="@drawable/icon_refresh_4" android:duration="500" />

 </animation-list>

当我按下按钮时,它会有点动画。意思是它转到上面动画列表中的一个图像,然后返回到它的状态。我尝试将持续时间增加到1000,怀疑它很快就无法注意到动画。但是,徒劳无功。我希望按下时箭头旋转。并且应该清楚地向用户显示该旋转效果。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

试试这个:

RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(3000);
iv.setAnimation(anim);
iv.startAnimation(anim);

答案 1 :(得分:1)

只需在(anim_refresh_button.xml)的动画列表中更改此内容:

android:oneshot="false"

相反

android:oneshot="true"

答案 2 :(得分:0)

使这个方法成为全局,并在您的活动中进行如下操作: -

private void Animation() {
            ImageView img = (ImageView)findViewById(R.id.anim_image);
            anim = (AnimationDrawable)img.getDrawable();
            img.post(run);
        }

        Runnable run = new Runnable() {
            @Override
            public void run() {
                anim.start();
             }
        };

现在只需致电

 Animation();

在活动的oncreate()中

这是xml中的图像视图代码

  <ImageView
            android:id="@+id/anim_image"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/rel"
            android:layout_below="@+id/cmnttext"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="40dp"
            android:src="@anim/animationofdownloading" />

这是src animationofdownloading of image

<item
    android:drawable="@drawable/cloud_first"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_sec"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_thi"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_four"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_fiv"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_six"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_sev"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_eig"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_nin"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_ten"
    android:duration="40"/>
<item
    android:drawable="@drawable/cloud_ele"
    android:duration="40"/>

希望它会对你有所帮助 祝你好运dude:)

相关问题