【问题标题】:How to apply the color animation for a button?如何为按钮应用彩色动画?
【发布时间】:2015-11-21 09:24:13
【问题描述】:

我的 xml 中有一个 LinearLayout。当我单击 LinearLayout 时,我需要特定时间的 onclick 事件的颜色外观。之后,颜色会消失。

holder.ll2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(final View v) {
                
                    v.setBackgroundColor(0xFF00FF00);
                    v.invalidate();
                }
            }
        });

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个 在资源文件中创建可绘制文件夹 创建这个 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/big_box_hover"/>
    <item android:state_focused="true"
             android:state_pressed="true"
             android:drawable="@drawable/big_box_hover" /> 
    <item android:state_focused="false" 
             android:state_pressed="true" 
             android:drawable="@drawable/big_box_hover" /> 
    <item android:drawable="@drawable/big_box" /> 
    </selector>
    

    然后 在你的布局中 你可以这样做

    <Button
                android:id="@+id/button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                android:descendantFocusability="blocksDescendants"
                android:background="@drawable/dashboardbuttonclick"
                android:gravity="center_vertical" >
    

    【讨论】:

    • 您可以根据您的要求设置drawables,即您按下视图时需要什么样的颜色
    【解决方案2】:

    您可以为此使用 Handler

    holder.ll2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(final View v) { 
                       handler=new Handler();
                       handler.postDelayed(myRunnable, TIME_MILI_TO_SHOW_COLOR_INT);
                      btn.setBackgroundColor(0xFF00FF00);
                    }
                }
            });
    

    Runnable 为那个

     myRunnable=new Runnable() {
    
                @Override
                public void run() {
    
                    btn.setBackgroundColor(YOUR_DEFAULT_COLOR);
                }
            }
    

    【讨论】:

      【解决方案3】:

      这个链接可以帮助你

      How to create custom button in Android using XML Styles

      基本上,您希望单击按钮声明..单击时应显示不同的颜色,释放时显示原始颜色。可以通过将声明为 xml 的按钮定义然后将该 xml 设置为按钮背景来完成。

      【讨论】:

      • 我想在点击事件完成后点击线性布局时显示颜色外观,颜色也消失了
      • 当我点击一个按钮时,我需要背景的颜色效果,然后它就会消失
      【解决方案4】:

      试试这个代码

      Button btn = (Button)this.findViewById(R.id.btn1);
      //Let's change background's color from blue to red.
      ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
      TransitionDrawable trans = new TransitionDrawable(color);
      //This will work also on old devices. The latest API says you have to use setBackground instead.
      btn.setBackgroundDrawable(trans);
      trans.startTransition(5000);
      

      【讨论】:

      • 如何在android中显示clickevent颜色效果?
      • 只需在按钮点击时调用此方法
      • 我需要特定时间的彩色动画,我正在使用 onclick 进行布局
      • 这里 5000 是时间...在中间时间颜色从蓝色变为红色
      • 之后我的背景颜色会消失
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2011-03-15
      • 1970-01-01
      相关资源
      最近更新 更多