在安卓系统中,当我给按钮设置背景图片后,点击按钮时没有任何视觉效果。
我需要给按钮添加一些效果,以便用户能够识别出按钮已被点击。
当按钮被点击时应该暗淡几秒钟。如何实现这个效果?
在安卓系统中,当我给按钮设置背景图片后,点击按钮时没有任何视觉效果。
我需要给按钮添加一些效果,以便用户能够识别出按钮已被点击。
当按钮被点击时应该暗淡几秒钟。如何实现这个效果?
可以通过创建一个包含按钮不同状态列表的可绘制XML文件来实现。例如,如果您创建一个名为“button.xml”的新XML文件并编写以下代码:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/YOURIMAGE" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:drawable="@drawable/YOURIMAGE" />
</selector>
为了使背景图片在按下时呈现出变暗的外观,创建第二个xml文件并将其命名为gradient.xml,其中包含以下代码:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap android:src="@drawable/YOURIMAGE"/>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
</shape>
</item>
</layer-list>
在您的按钮的xml中,将背景设置为按钮的xml,例如:android:background="@drawable/button"
将上述代码更改为在按钮中显示图像(YOURIMAGE),而不是块颜色。
当你有很多图像按钮时,而且你不想为每个按钮编写xml,这时会更加简单。
Kotlin版本:
fun buttonEffect(button: View) {
button.setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
v.background.setColorFilter(-0x1f0b8adf, PorterDuff.Mode.SRC_ATOP)
v.invalidate()
}
MotionEvent.ACTION_UP -> {
v.background.clearColorFilter()
v.invalidate()
}
}
false
}
}
Java 版本:
public static void buttonEffect(View button){
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.getBackground().setColorFilter(0xe0f47521,PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP: {
v.getBackground().clearColorFilter();
v.invalidate();
break;
}
}
return false;
}
});
}
您可以简单地使用前景色为您的View
实现可点击效果:
android:foreground="?android:attr/selectableItemBackground"
如果要在暗色主题下使用,请将theme添加到您的layout
中(为了使可点击效果清晰):
android:theme="@android:style/ThemeOverlay.Material.Dark"
AlphaAnimation
对象,该对象决定按钮淡出效果的程度,然后在按钮的onClickListener
中启动它。private AlphaAnimation buttonClick = new AlphaAnimation(1F, 0.8F);
// some code
public void onClick(View v) {
v.startAnimation(buttonClick);
}
当然,这只是一种方式,不是最理想的方式,只是更容易而已。
为了使您的项目与系统外观和感觉一致,请在所需视图的背景或前景标签中引用系统属性android:attr/selectableItemBackground
:
<ImageView
...
android:background="?android:attr/selectableItemBackground"
android:foreground="?android:attr/selectableItemBackground"
...
/>
使用这两个属性可以在 API 级别 23 之前和之后实现所需的效果。
针对所有视图
android:background="?android:attr/selectableItemBackground"
但对于使用阴影的cardview
android:foreground="?android:attr/selectableItemBackground"
为了在工具栏中实现圆形点击效果
android:background="?android:attr/actionBarItemBackground"
同时您需要设置
android:clickable="true"
android:focusable="true"
android:background="@drawable/bluebutton"
用于将按钮的背景设置为图像。它已经被占用了,这样是行不通的。 - Houman或者只使用一张背景图片,您可以通过使用 setOnTouchListener
来实现点击效果。
两种方法。
((Button)findViewById(R.id.testBth)).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
Button view = (Button) v;
view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
// Your action here on button click
case MotionEvent.ACTION_CANCEL: {
Button view = (Button) v;
view.getBackground().clearColorFilter();
view.invalidate();
break;
}
}
return true;
}
});
如果您不想使用setOnTouchLister
,则实现此目的的另一种方法是:
myButton.getBackground().setColorFilter(.setColorFilter(0xF00, Mode.MULTIPLY);
StateListDrawable listDrawable = new StateListDrawable();
listDrawable.addState(new int[] {android.R.attr.state_pressed}, drawablePressed);
listDrawable.addState(new int[] {android.R.attr.defaultValue}, myButton);
myButton.setBackgroundDrawable(listDrawable);
我也遇到过同样的问题,需要透明背景但仍能进行动画效果。设置以下内容即可解决此问题:
android:background="?android:selectableItemBackground"
如果您想要实现循环效果,也可以这样做:
android:background="?android:selectableItemBackgroundBorderless"
void addClickEffect(View view)
{
Drawable drawableNormal = view.getBackground();
Drawable drawablePressed = view.getBackground().getConstantState().newDrawable();
drawablePressed.mutate();
drawablePressed.setColorFilter(Color.argb(50, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
StateListDrawable listDrawable = new StateListDrawable();
listDrawable.addState(new int[] {android.R.attr.state_pressed}, drawablePressed);
listDrawable.addState(new int[] {}, drawableNormal);
view.setBackground(listDrawable);
}
说明:
getConstantState().newDrawable()用于克隆现有的Drawable,否则将使用相同的drawable。可在此处阅读更多信息: Android: Cloning a drawable in order to make a StateListDrawable with filters
mutate()用于使Drawable克隆不与其他Drawable实例共享其状态。可在此处阅读更多信息: https://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()
用法:
您可以将任何类型的View(Button、ImageButton、View等)作为参数传递给函数,它们将应用点击效果。
addClickEffect(myButton);
addClickEffect(myImageButton);
简单易行的设置视图点击效果的方法。
方法
public AlphaAnimation clickAnimation() {
return new AlphaAnimation(1F, 0.4F); // Change "0.4F" as per your recruitment.
}
yourView.startAnimation(clickAnimation());