简体   繁体   English

Android中图像位图的透明色彩效果

[英]Transparent Color Effect Over the Image Bitmap in Android

I am working on an Image editor android app like : BeFunkey I have achieved many effects like this android app but still some effects are been headache for me. 我正在开发一个图像编辑器的Android应用程序,如: BeFunkey我已经实现了很多这样的Android应用程序的效果,但仍然有些影响让我很头疼。 Like PopArt Effect. 喜欢PopArt效果。 Please see below images. 请看下面的图片。 One is original and second is popart colored effect : 一个是原创,第二个是popart彩色效果:

Original :: 原版的 ::

在此输入图像描述

PopArt :: 流行艺术 ::

在此输入图像描述

I have used RGB Color Matrix To get color effect on bitmap. 我使用了RGB Color Matrix来获得位图的颜色效果。 But When i use it worked like one color in whole image but here in this effect there are multiple colors on the image. 但是当我使用它时,它在整个图像中就像一种颜色一样,但在这种效果中,图像上有多种颜色。 If anybody here who can guide me please tell me how can i achieve this color effect. 如果有人可以指导我,请告诉我如何才能实现这种色彩效果。 Thank you for your time. 感谢您的时间。

Following Code I am using for color Effects :: 以下代码我用于颜色效果::

    public static final float[] float_color_matrix = new float[] { 2.6f, 0, 0.1f, 0, 0, 0.1f, 1.2f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 };

    ColorMatrix cm = new ColorMatrix();
    cm.postConcat(new ColorMatrix(float_color_matrix));
    ColorFilter colorFilter = new ColorMatrixColorFilter(cm);

    imageView.setColorFilter(colorFilter);

This code should do the trick. 这段代码应该可以解决问题。 I came pretty close to the result you're looking for. 我非常接近你正在寻找的结果。 If it's not exactly the same you can just play around with the colors in the colors array. 如果它不完全相同,你可以只使用colors数组中的colors I use this nifty tool to find hex color codes. 我使用这个漂亮的工具来找到十六进制颜色代码。

/**
 * 
 * @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;
}

Then you just implement it by doing this 然后你就这样实现它

imageView.setImageBitmap(setPopArtGradient(yourContext, R.drawable.your_drawable));

If you want to pass a bitmap you can do this. 如果要传递位图,可以执行此操作。

/**
 * 
 * @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;
}

I applied it to one of my Photos I took and this was the result 我将它应用到我拍摄的一张照片中,这就是结果

编辑过的照片

Hope it helps you. 希望它能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM