在Android中的视图上应用并清除ColorFilter

时间:2013-08-30 07:42:39

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

我有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个按钮,即使我点击一个按钮,效果也会应用到另一个按钮。

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:7)

view方法中的

onTouch,在您的情况下,点击了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.xml
<style name="Button">
    <item name="android:background">@drawable/background_button</item>
</style>

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

答案 1 :(得分:3)

尝试按钮替换为视图返回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;
        }

答案 2 :(得分:0)

我这样做了:

在视图上应用滤镜有两种方法。

private ImageView imageView;

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

应用滤色镜

方式1:

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

OR

方式2:

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

清除滤色镜

imageView.clearColorFilter();

两者都适合我。

希望这会对你有所帮助。