如何创建一个点击后闪烁的按钮?

发布于 2024-12-04 12:30:50 字数 643 浏览 2 评论 0原文

到目前为止,我了解了动画的工作原理以及如何根据其状态设置按钮的背景,如这里

好吧,我定义了一个动画:

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<alpha 
    android:interpolator="@android:anim/accelerate_interpolator" 
    android:fromAlpha="0.0" 
    android:toAlpha="1.0"  
    android:duration="50" 
    android:repeatMode="reverse"
    android:repeatCount="6"/> 

我在 onClick(View v) 方法中启动动画。现在的问题是,实际的单击操作在动画完成之前得到处理。我知道我可以使用 AnimationListener,但这对我来说看起来不太好,因为我必须在 AnimationListener 中调用实际的单击过程。

有谁知道一种更巧妙的方法让按钮在单击后闪烁?

So far I learned how animations work and how to set the background of a button according to its state as described here.

Well, I defined an animation:

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<alpha 
    android:interpolator="@android:anim/accelerate_interpolator" 
    android:fromAlpha="0.0" 
    android:toAlpha="1.0"  
    android:duration="50" 
    android:repeatMode="reverse"
    android:repeatCount="6"/> 

I start the animation in the onClick(View v) method. The problem now is, that the actual click action gets processed before the animation finishes. I know I could use an AnimationListener, but this would not look very nice to me since I'd have to call the actual click processes within the AnimationListener then.

Does anyone know a more skilful way to let a button blink after it gets clicked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Saygoodbye 2024-12-11 12:30:50

您可以使用 Selector 标签,如下所示: 创建一个新的 xml 文件并将其放置在您的可绘制文件夹中,并将其命名为shadow_color.xml

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

然后转到声明您的 Button 的 xml:并在 Button

中写入一个属性android:background="@drawable/shadow_color"
你就完成了。

如果觉得有用就标记答案..

You can use the Selector tag like Follows: Create a New xml file and place it in your drawable folder and name it as shadow_color.xml

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

And then go to that xml in which your Button is declared: And write one attribute in Button

android:background="@drawable/shadow_color"
and you are done.

Mark the answer if you find it usefull..

野味少女 2024-12-11 12:30:50

为按钮定义 onclick

button1.setOnClickListener(
    new Button.OnClickListener() { 
        public void onClick (View v){ calcular(1,v); }
    }
);

让按钮闪烁

这使得 XML 中定义的图像彼此交替。

    public void calcular(final int p,final View v){
        MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
        mp.start();
        //v.setBackgroundResource(R.drawable.dia1btn_stl2);
        final TransitionDrawable transition1 = 
                 (TransitionDrawable) v.getBackground();
        Handler blinkHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

                switch (msg.what) {
                case 0:

                    transition1.startTransition(70);

                        break;
                case 1:
                    transition1.resetTransition();
                    break;

                }
                super.handleMessage(msg);
        }
    };


    for (int i=0; i<6; i++)
    {
        Message msg = new Message();
         if(i % 2 == 0){
             msg.what = 0;
         }
        else{
            msg.what=1;
        }

        blinkHandler.sendMessageDelayed(msg, i*100);
    }

        /*mCurrentSeries.clear();
        if(calcularctrl == 0){
            calcularctrl = 1;
            dtdodo = new DownloadImageTask(this , p , codacaovalue);
            dtdodo.execute("wwwkjhdijdh");
        }*/

        Handler handler2 = new Handler(); 
        handler2.postDelayed(new Runnable() { 
             public void run() { 
                  //v.setBackgroundResource(R.drawable.dia1btn_stl2); 
                mCurrentSeries.clear();
                if(calcularctrl == 0){
                    calcularctrl = 1;
                    dtdodo = new DownloadImageTask(outer() , p , codacaovalue);
                    dtdodo.execute("wwwkjhdijdh");
                }
                try {
                    this.finalize();
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
             public acoesdetalhes outer(){
                 return acoesdetalhes.this;
             }
        }, 1000);
    }

按钮背景的 XML

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
    <item android:drawable="@drawable/mes1btn_stl2" />
    <item android:drawable="@drawable/mes1btn_prssd2" />
</transition>

此代码部分由用户 Alin 提供。

Define the onclick for the button

button1.setOnClickListener(
    new Button.OnClickListener() { 
        public void onClick (View v){ calcular(1,v); }
    }
);

Make your button blink

This makes the images defined in the XML alternate between each other.

    public void calcular(final int p,final View v){
        MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
        mp.start();
        //v.setBackgroundResource(R.drawable.dia1btn_stl2);
        final TransitionDrawable transition1 = 
                 (TransitionDrawable) v.getBackground();
        Handler blinkHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

                switch (msg.what) {
                case 0:

                    transition1.startTransition(70);

                        break;
                case 1:
                    transition1.resetTransition();
                    break;

                }
                super.handleMessage(msg);
        }
    };


    for (int i=0; i<6; i++)
    {
        Message msg = new Message();
         if(i % 2 == 0){
             msg.what = 0;
         }
        else{
            msg.what=1;
        }

        blinkHandler.sendMessageDelayed(msg, i*100);
    }

        /*mCurrentSeries.clear();
        if(calcularctrl == 0){
            calcularctrl = 1;
            dtdodo = new DownloadImageTask(this , p , codacaovalue);
            dtdodo.execute("wwwkjhdijdh");
        }*/

        Handler handler2 = new Handler(); 
        handler2.postDelayed(new Runnable() { 
             public void run() { 
                  //v.setBackgroundResource(R.drawable.dia1btn_stl2); 
                mCurrentSeries.clear();
                if(calcularctrl == 0){
                    calcularctrl = 1;
                    dtdodo = new DownloadImageTask(outer() , p , codacaovalue);
                    dtdodo.execute("wwwkjhdijdh");
                }
                try {
                    this.finalize();
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
             public acoesdetalhes outer(){
                 return acoesdetalhes.this;
             }
        }, 1000);
    }

The XML of the button background

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
    <item android:drawable="@drawable/mes1btn_stl2" />
    <item android:drawable="@drawable/mes1btn_prssd2" />
</transition>

This code provided in part by user Alin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文