Android 如何使解析后的颜色更亮?
实际上,在我的RecyclerView适配器中,我通过Android 如何使解析后的颜色更亮?,android,android-recyclerview,Android,Android Recyclerview,实际上,在我的RecyclerView适配器中,我通过 holder.itemView.setBackgroundColor(Color.parseColor(currentItem.getSfondo())); 解析后的颜色是从DB中给出的,到目前为止一切都很好,但现在我已经在我的cardView中添加了一个底部栏,在这里我将显示一个价格,这样我就可以从itemView so color.parseColor(currentItem.getSfondo())中获得相同的颜色但是,通过添加一
holder.itemView.setBackgroundColor(Color.parseColor(currentItem.getSfondo()));
解析后的颜色是从DB中给出的,到目前为止一切都很好,但现在我已经在我的cardView中添加了一个底部栏,在这里我将显示一个价格,这样我就可以从itemView so color.parseColor(currentItem.getSfondo())中获得相同的颜色但是,通过添加一些alpha或idk,用户将能够从价格中分割文本,从而使其更轻
那是什么
我使用这些方法
lighten(Color.parseColor("#636161"), .5); //.5 is factor to lighten
只需将这些方法添加到Util类中
public static int lighten(int color, double fraction) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
red = lightenColor(red, fraction);
green = lightenColor(green, fraction);
blue = lightenColor(blue, fraction);
int alpha = Color.alpha(color);
return Color.argb(alpha, red, green, blue);
}
public static int darken(int color, double fraction) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
red = darkenColor(red, fraction);
green = darkenColor(green, fraction);
blue = darkenColor(blue, fraction);
int alpha = Color.alpha(color);
return Color.argb(alpha, red, green, blue);
}
private static int darkenColor(int color, double fraction) {
return (int)Math.max(color - (color * fraction), 0);
}
private static int lightenColor(int color, double fraction) {
return (int) Math.min(color + (color * fraction), 255);
}
更新
您可以通过color类将十六进制颜色转换为int-color
Color.parseColor("#636161");
并且可以使用这些方法
lighten(Color.parseColor("#636161"), .5);
我使用这些方法
lighten(Color.parseColor("#636161"), .5); //.5 is factor to lighten
只需将这些方法添加到Util类中
public static int lighten(int color, double fraction) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
red = lightenColor(red, fraction);
green = lightenColor(green, fraction);
blue = lightenColor(blue, fraction);
int alpha = Color.alpha(color);
return Color.argb(alpha, red, green, blue);
}
public static int darken(int color, double fraction) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
red = darkenColor(red, fraction);
green = darkenColor(green, fraction);
blue = darkenColor(blue, fraction);
int alpha = Color.alpha(color);
return Color.argb(alpha, red, green, blue);
}
private static int darkenColor(int color, double fraction) {
return (int)Math.max(color - (color * fraction), 0);
}
private static int lightenColor(int color, double fraction) {
return (int) Math.min(color + (color * fraction), 255);
}
更新
您可以通过color类将十六进制颜色转换为int-color
Color.parseColor("#636161");
并且可以使用这些方法
lighten(Color.parseColor("#636161"), .5);
实际上currentItem.getSfondo()是十六进制的,所以每个cardView从db获得不同的背景颜色,比如FF00FFFF,我怎么能使它变亮?实际上currentItem.getSfondo()是十六进制的,所以每个cardView从db获得不同的背景颜色,比如FF00FFFF,我怎么能使它变亮?