【问题标题】:ImageButton Icon Tint based on State基于状态的 ImageButton 图标色调
【发布时间】:2019-01-10 09:46:39
【问题描述】:

我目前正在尝试使用当前纯白色的 Drawable 对一些 ImageButton 进行着色。 Tint Color 应该由 StateList 提供,并且应该根据按钮的当前状态而改变。状态列表看起来像:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#FF0000"/>
    <item android:color="#00FF00" android:state_enabled="false"/>
    <item android:color="#0000FF" android:state_pressed="true" android:state_enabled="true"/>
</selector>

按钮的布局 XML sn-p 是:

<ImageButton
    android:id="@+id/btnNext"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/vertical_content_padding"
    android:layout_marginEnd="@dimen/horizontal_content_padding"
    android:contentDescription="@string/next"
    android:src="@drawable/ic_chevron_right_white_24dp"
    android:tint="@color/state_btn_tint_light"/>

颜色状态列表中的默认色调已选择并正确显示。但是禁用按钮或按下它不会触发图标的任何颜色变化。我也尝试使用setImageTintList 进行务实的设置。

【问题讨论】:

    标签: android


    【解决方案1】:

    您的代码应该可以工作。但是,有一个错误:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="#FF0000"/>
        <item android:color="#00FF00" android:state_enabled="false"/>
        <item android:color="#0000FF" android:state_pressed="true" android:state_enabled="true"/>
    </selector>
    

    你需要颠倒状态的顺序。

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="#0000FF" android:state_pressed="true" android:state_enabled="true"/>
        <item android:color="#00FF00" android:state_enabled="false"/>
        <item android:color="#FF0000"/>
    </selector>
    

    我不完全确定它在内部是如何工作的,但它的功能似乎类似于 If/Else 语句。如果第一个条件失败,则进入第二个条件。由于你的第一个项目没有条件,它总是会被选中而其他的会被忽略,给人的印象是颜色状态列表根本不起作用。因此,始终将具有最严格条件的颜色状态放在首位。

    【讨论】:

      【解决方案2】:

      我将简化我的答案。希望它会有所帮助。 您可以随时在colors.xml 中输入您想要的任何颜色,对吧?所以让我们使用它。

      你可以像这样创建一个你想要的颜色的整数数组列表

      integer[] colors = new integer[]{R.color.white, R.color.black, ...};
      

      现在 imageButtons 可以像这样采用背景色调:

      imagebutton.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(mContext,R.color.white)));
      
      1. 现在让我们将它们与动画放在一起,创建动画方法:

      public ValueAnimator ColorAnimation(ImageButton imagebutton, int colorFrom, int colorTo){
              ValueAnimator anim = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
              anim .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                  @Override
                  public void onAnimationUpdate(ValueAnimator animation) {
                      imagebutton.setImageTintList(ColorStateList.valueOf((int) animation.getAnimatedValue()));
                  }
              });
              anim.setInterpolator(new DecelerateInterpolator());
              anim.setDuration(300); // -- Whatever time
              return anim;
          };
      1. 现在,在您的活动中,您可以像这样实现它:

      @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.layout);
              mContext = this;
              ...
      
              ImageButton myButton = (ImageButton) findViewById(R.id.imagebutton);
      
              //-- Define All the Colors You want
              integer[] listOfColors = new integer[]{R.color.white, R.color.black, R.color.yellow, R.color.red, R.color.blue};
      
              //-- With this you can randomly get a color from the list
              int aColor = (int)(Math.random()*listOfColors.length); 
      
              //-- Your Default imageButton Color
              int DefaultColor = ContextCompat.getColor(mContext,R.color.white);
      
              myButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      ColorAnimation(myButton, DefaultColor, listOfColors[aColor]).start;
                      //-- You can even enter whatever color want directly for "colorTo"
                      //-- You can Reverse this animation as well
                  }
              });
      
              ...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-23
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        • 2018-03-21
        • 2015-10-13
        • 1970-01-01
        相关资源
        最近更新 更多