这段代码应该可以解决问题。我非常接近您正在寻找的结果。如果不完全一样,您可以使用 colors 数组中的颜色。我使用this 漂亮的工具来查找十六进制颜色代码。
/**
*
* @param context
* @param yourDrawableResource (ex R.drawable.ic_drawable)
* @return the image bitmap with the correct overlay
*/
public static Bitmap setPopArtGradient(Context context, int yourDrawableResource) {
int[] colors = new int[]{Color.parseColor("#FFD900"),Color.parseColor("#FF5300"),
Color.parseColor("#FF0D00"),Color.parseColor("#AD009F"),
Color.parseColor("#1924B1")};
float[] colorPositions = new float[]{0.2f,0.4f,0.6f,0.8f,1.0f};
final Resources res = context.getResources();
Drawable drawable = res.getDrawable(yourDrawableResource);
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
/* Create your gradient. */
LinearGradient grad = new LinearGradient(0, 0, 0, canvas.getHeight(), colors, colorPositions, TileMode.CLAMP);
/* Draw your gradient to the top of your bitmap. */
Paint p = new Paint();
p.setStyle(Style.FILL);
p.setAlpha(110); //adjust alpha for overlay intensity
p.setShader(grad);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), p);
return bitmap;
}
然后你只需通过这样做来实现它
imageView.setImageBitmap(setPopArtGradient(yourContext, R.drawable.your_drawable));
如果你想传递一个位图,你可以这样做。
/**
*
* @param context
* @param bmp (your bitmap)
* @return the image bitmap with the correct overlay
*/
public static Bitmap setPopArtGradientFromBitmap(Context context, Bitmap bmp) {
int[] co = new int[]{Color.parseColor("#FFD900"),Color.parseColor("#FF5300"),Color.parseColor("#FF0D00"),Color.parseColor("#AD009F"),Color.parseColor("#1924B1")};
float[] coP = new float[]{0.2f,0.4f,0.6f,0.8f,1.0f};
Bitmap bitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
/* Create your gradient. */
LinearGradient grad = new LinearGradient(0, 0, 0, canvas.getHeight(), co, coP, TileMode.CLAMP);
/* Draw your gradient to the top of your bitmap. */
Paint p = new Paint();
p.setStyle(Style.FILL);
p.setAlpha(110);
p.setShader(grad);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), p);
return bitmap;
}
我将它应用到我拍摄的一张照片上,这就是结果
希望对你有帮助。