0

我正在尝试为我的应用创建晕影效果。我一直在寻找帮助实现这一目标,但找不到任何东西。

我最近发现 了这个教程

我尝试使用以下代码在我的应用程序中实现它:

public int[] drawBitmap(Bitmap originalBitmap){
    Bitmap mask;
    Paint paint = new Paint();
    mask = convertToAlphaMask(BitmapFactory.decodeResource(context.getResources(), R.drawable.spot_mask));
    Shader shader = createShader(mask);
    paint.setShader(shader);

    Bitmap tempBit = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(tempBit);
    canvas.drawBitmap(originalBitmap, 0, 0,paint);
    tempBit.getPixels(pixels, 0, tempBit.getWidth(), 0, 0, tempBit.getWidth(), tempBit.getHeight());
    return pixels;
}
private static Bitmap convertToAlphaMask(Bitmap input) {
    Bitmap a = Bitmap.createBitmap(input.getWidth(), input.getHeight(), Bitmap.Config.ALPHA_8);
    Canvas c = new Canvas(a);
    c.drawBitmap(input, 0.0f, 0.0f, null);
    return a;
}
private static Shader createShader(Bitmap b) {
    return new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}

但是这个效果看起来像这样:(唯一的变化是在图像的顶部) http://postimg.org/image/rrivq28v1/

我做错了什么?

此外,还有其他方法可以在位图上应用晕影效果吗?

谢谢!

4

2 回答 2

4

很久之后才找到

public Bitmap vignett(Bitmap bm, int p){
    Bitmap image = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(), Bitmap.Config.ARGB_8888);
    int rad;
    Canvas canvas = new Canvas(image);
    canvas.drawBitmap(bm, 0, 0, new Paint());
    if(bm.getWidth()<bm.getHeight()){
        int o = (bm.getHeight()*2)/100;
        rad = bm.getHeight() - o*p/3;
    }else{
        int o = (bm.getWidth()*2)/100;
        rad = bm.getWidth() - o*p/3;
    }
    Rect rect = new Rect(0, 0, bm.getWidth(), bm.getHeight());
    RectF rectf = new RectF(rect);
    int[] colors = new int[] { 0, 0, Color.BLACK };
    float[] pos = new float[] { 0.0f, 0.1f, 1.0f };
    Shader linGradLR = new RadialGradient(rect.centerX(), rect.centerY(),rad, colors, pos, Shader.TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setShader(linGradLR);
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setAlpha(255);
    canvas.drawRect(rectf, paint);
    return image;
}

这里 intp是 seekbar 从 1 到 100 的标准值;对于效果强度,您可以设置paint.setAlpha0 到 255!

于 2017-05-20T09:36:01.313 回答
3

我的第一个答案被删除了,因为我通过链接指出了我的解决方案。所以我会在这里回答你的问题。诀窍是使用 4 个线性渐变。应用它们会给你一个非常接近真正的晕影效果的结果。而且它也非常快;)所以这是我的解决方案的一部分。首先,您必须创建一个画布:

    Canvas canvas = new Canvas(bitmapOut);
    canvas.drawBitmap(mVignette.getBitmapIn(), 0, 0, null);

然后你定义效果应该在你的图像中达到多远:

    int tenthLeftRight = (int)(width/5);
    int tenthTopBottom = (int)(height/5);

现在创建四个着色器:

    // Gradient left - right
    Shader linGradLR = new LinearGradient(0, height/2, tenthLeftRight/2, height/2, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
    // Gradient top - bottom
    Shader linGradTB = new LinearGradient(width/2, 0, width/2, tenthTopBottom, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
    // Gradient right - left
    Shader linGradRL = new LinearGradient(width, height/2, (width-tenthLeftRight), height/2, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
    // Gradient bottom - top
    Shader linGradBT = new LinearGradient(width/2, height, width/2, (height - tenthTopBottom), Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP); 

现在剩下要做的就是在画布上绘制:

    Paint paint = new Paint();
    paint.setShader(linGradLR);
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setAlpha(125);
    // Rect for Grad left - right
    Rect rect = new Rect(0, 0, tenthLeftRight, height);
    RectF rectf = new RectF(rect);
    canvas.drawRect(rectf, paint);

    // Rect for Grad top - bottom
    paint.setShader(linGradTB);
    rect = new Rect(0, 0, width, tenthTopBottom);
    rectf = new RectF(rect);
    canvas.drawRect(rectf, paint);

    // Rect for Grad right - left
    paint.setShader(linGradRL);
    rect = new Rect(width, 0, width - tenthLeftRight, height);
    rectf = new RectF(rect);
    canvas.drawRect(rectf, paint);

    // Rect for Grad bottom - top
    paint.setShader(linGradBT);
    rect = new Rect(0, height - tenthTopBottom, width, height);
    rectf = new RectF(rect);
    canvas.drawRect(rectf, paint);
于 2013-11-23T13:41:48.640 回答