如果没有美工自己又不会PS,或者为了减少图片资源,可以采取动态修改drawable的方式实现按钮点击效果,首先,准备一份drawable图片资源,然后调用以下方法即可:
public Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(wrappedDrawable, colors);
return wrappedDrawable;
}
private void setDrawable(Button button, Drawable drawable, int color) {
drawable = tintDrawable(drawable, ColorStateList.valueOf(color));
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int size;
size = 90 * dm.heightPixels / 2560;
drawable.setBounds(0, 0, size, size);
button.setCompoundDrawables(null, drawable, null, null);
button.setTextColor(color);
}
监听按钮按下和松开事件,调用setDrawable传入不同color就可以。