在Android中,在OnClick中显示按钮动画吗? [英] In Android, Display pressed button animation in OnClick?

查看:78
本文介绍了在Android中,在OnClick中显示按钮动画吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用onClick时,如何在按钮中制作动画我没有找到直接的答案.我有一个如下的custom_btn.xml:

I haven't found a straight answer to how to make an animation in a button when onClick is called. I've got a custom_btn.xml like so:

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

动画位于btn_pressed.xml中,如下所示:

and the animation is in btn_pressed.xml like so:

    <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:drawable="@drawable/btn_on_1" android:duration="30" />
        <item android:drawable="@drawable/btn_on_2" android:duration="30" />
        <item android:drawable="@drawable/btn_on_3" android:duration="30" />
        <item android:drawable="@drawable/btn_on_4" android:duration="30" />
        <item android:drawable="@drawable/btn_on_5" android:duration="30" />

</animation-list>

我的问题是我似乎无法在此处的onClickListener中找到正确的代码:

my problem is that I can't seem to find the right code to go in the onClickListener here:

        button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 

            //-- what is the proper animation call that would go here
         to make btn_pressed.xml cycle only once when pressed?

        } 
    });

谢谢!

推荐答案

来自Android文档: http://developer.android.com/guide/topics/resources/animation -resource.html

From the Android documentation: http://developer.android.com/guide/topics/resources/animation-resource.html

示例: XML文件保存在res/anim/rocket.xml:

example: XML file saved at res/anim/rocket.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

此应用程序代码会将动画设置为View的背景,然后播放动画:

This application code will set the animation as the background for a View, then play the animation:

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);

rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();

这篇关于在Android中,在OnClick中显示按钮动画吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆