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

时间:2013-11-18 05:51:07

标签: android image-processing

我正在开发一个图片编辑器安卓应用,例如:BeFunkey  我已经实现了很多像这个Android应用程序的效果,但仍然有一些影响让我很头疼。喜欢PopArt效果。请看下面的图片。一个是原创的,第二个是popart彩色效果:

原创::

enter image description here

PopArt ::

enter image description here

我使用了RGB Color Matrix来获得位图的色彩效果。但是当我使用它时,它在整个图像中就像一种颜色一样,但在这种效果中,图像上有多种颜色。如果有人可以指导我,请告诉我如何才能实现这种色彩效果。谢谢你的时间。

以下代码我用于色彩效果::

    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);

1 个答案:

答案 0 :(得分:8)

这段代码可以解决问题。我非常接近你正在寻找的结果。如果它不完全相同,你可以只使用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;
}

我将它应用到我拍摄的一张照片中,这就是结果

edited photo

希望它对你有所帮助。