在按钮上应用滤色镜 [英] Apply color filter on button
问题描述
我有很多按钮有很多不同的背景颜色。我想知道是否有一种方法来应用一些点击的颜色过滤器。例如,我想让我的所有按钮变得更暗点击。
I have a lot of button with a lot of different background colors. I want to know if there is a way to apply some color filter on click. For example, i want that all my buttons become darker on click. They keep the original color, but it's darker.
有没有简单的方法,或者我必须为每个按钮定义更深的颜色?
Is there an easy way to do it, or i have to define the darker color for each button?
感谢。
推荐答案
我假设您希望按钮在触摸时变暗,当用户释放按钮时恢复正常。
I assume you want the button to darken on touch, and revert to normal when the user releases the button.
我建议您为自己的工作创建一个自定义按钮:
I would suggest creating a custom button which does the work for you:
import android.content.Context;
import android.graphics.LightingColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
public class DarkenButton extends Button {
public DarkenButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DarkenButton(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// darken background
getBackground().setColorFilter(
new LightingColorFilter(0xff888888, 0x000000));
break;
case MotionEvent.ACTION_UP:
// clear color filter
getBackground().setColorFilter(null);
break;
}
return super.onTouchEvent(event);
}
}
然后使用DarkenButton使用Button。
Then use DarkenButton anywhere you would normally use a Button.
这篇关于在按钮上应用滤色镜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!