【问题标题】:Apply and Clear ColorFilter on view in Android在 Android 中的视图上应用和清除 ColorFilter
【发布时间】:2013-08-30 07:42:38
【问题描述】:

我有 2 个不同父母的按钮。

我正在尝试在按钮的触摸上应用按钮效果。

这是我的代码:

public class ButtonHighlighterOnTouchListener implements OnTouchListener {

    final Button button;

    public ButtonHighlighterOnTouchListener(final Button imageButton) {
      super();
      this.button = imageButton;
    }

    public boolean onTouch(final View view, final MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            button.getBackground().setColorFilter(Color.parseColor("#B7B2B0"), PorterDuff.Mode.MULTIPLY);
            button.invalidate();
            break;
        case MotionEvent.ACTION_CANCEL:
            button.getBackground().clearColorFilter();
            button.invalidate();
            break;
        case MotionEvent.ACTION_UP:
            button.getBackground().clearColorFilter();
            button.invalidate();
            break;
        }
        return false;
        }
}

它将效果应用到按钮,但问题是因为我有 2 个按钮,即使我点击一个按钮,效果也会应用到另一个按钮。

请帮我解决这个问题。

【问题讨论】:

    标签: android android-drawable android-button effects colorfilter


    【解决方案1】:

    viewin onTouchmethod 是,在你的情况下,Button 正在被点击。所以不是

    button.getBackground().setColorFilter(Color.parseColor("#B7B2B0"), PorterDuff.Mode.MULTIPLY);
    

    使用

    view.getBackground().setColorFilter(Color.parseColor("#B7B2B0"), PorterDuff.Mode.MULTIPLY);
    

    在其他情况下也是如此。

    编辑

    改为使用样式:

    //background_button.xml in drawable folder
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" >
        //Here you can set style you want when Button is pressed
            <shape>
                <solid
                    android:color="#22228C" />
                <stroke
                    android:width="1dp"
                    android:color="#9999DE" />
                <corners
                    android:radius="7dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
        <item>
        //Here you can set style you want when button is not pressed
            <shape>
                <gradient
                    android:startColor="#5858C8"
                    android:endColor="#22228C"
                    android:angle="90" />
                <stroke
                    android:width="1dp"
                    android:color="#9999DE" />
                <corners
                    android:radius="7dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
    </selector>
    
    //In your xml
    <Button
         android:background="@drawable/background_button" ... />
    

    编辑 2

    Button有一个构造函数,您可以在其中告诉您要应用哪种样式。

    public Button (Context context, AttributeSet attrs, int defStyle);
    

    所以你可以设置defStyle = android.R.style.Button 和你的styles.xmladd

    <style name="Button">
        <item name="android:background">@drawable/background_button</item>
    </style>
    

    这样,我认为您可以使用样式初始化您的Button(您将在background_button.xml 中具有突出显示的效果)并像现在一样添加背景图像。

    【讨论】:

    • 为什么不使用样式?
    • 怎么样?,我只能以编程方式完成
    • 是的,但是我在这里以编程方式创建按钮,并且我已经设置了 button.setBackgroundDrawable,所以我现在如何集成?
    • 我试过你的代码,它工作正常,你能帮我实现同样的程序
    • 默认情况下会有一张图片,按下时会在该图片上突出显示效果
    【解决方案2】:

    尝试:用视图替换按钮并返回true而不是false;

    public boolean onTouch(final View view, final MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                view .getBackground().setColorFilter(Color.parseColor("#B7B2B0"), PorterDuff.Mode.MULTIPLY);
                view .invalidate();
                break;
            case MotionEvent.ACTION_CANCEL:
                view .getBackground().clearColorFilter();
                view .invalidate();
                break;
            case MotionEvent.ACTION_UP:
                view .getBackground().clearColorFilter();
                view .invalidate();
                break;
            }
            return true;
            }
    

    【讨论】:

      【解决方案3】:

      我是这样做的:

      有两种方法可以在视图上应用颜色过滤器。

      private ImageView imageView;
      
      imageView = (ImageView) findViewById(R.id.imageView);
      

      应用颜色过滤器

      方式一:

      imageView.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
      

      或

      方式二:

      imageView.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
      

      清除彩色滤光片

      imageView.clearColorFilter();
      

      两者都适合我。

      希望这会对你有所帮助。

      【讨论】:

      • 知道为什么它在BottomNavigationView 上不起作用吗?
      猜你喜欢
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多