简体   繁体   English

如何更改位图的不透明度?

[英]How to change a bitmap's opacity?

I have a bitmap:我有一个位图:

Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");

But I'm not going to display the image to the user.但我不会向用户显示图像。 I want the alpha to be 100 (out of 255).我希望 alpha 为 100(共 255 个)。 If this is not possible, can I set the opacity of the Bitmap ?如果这是不可能的,我可以设置Bitmap的不透明度吗?

As far as I know, opacity or other color filters can't be set on the Bitmap itself.据我所知,不能在位图本身上设置不透明度或其他颜色过滤器。 You will need to set the alpha when you use the image:使用图像时需要设置 alpha:

If you're using ImageView, there is ImageView.setAlpha() .如果您使用 ImageView,则有ImageView.setAlpha()

If you're using a Canvas, then you need to use Paint.setAlpha() :如果您使用的是 Canvas,则需要使用Paint.setAlpha()

Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawBitmap(bitmap, src, dst, paint);

Also, incorporating WarrenFaith's answer, if you will use the Bitmap where a drawable is required, you can use BitmapDrawable.setAlpha() .此外,结合 WarrenFaith 的回答,如果您将在需要可绘制的地方使用 Bitmap,则可以使用BitmapDrawable.setAlpha()

You could also try BitmapDrawable instead of Bitmap .您也可以尝试BitmapDrawable而不是Bitmap If this is useful for you depends on the way you use the bitmap...如果这对您有用取决于您使用位图的方式...

Edit编辑

As a commenter asked how he can store the bitmap with alpha, here is some code:正如一位评论者询问他如何使用 alpha 存储位图,这里有一些代码:

// lets create a new empty bitmap
Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
// create a canvas where we can draw on
Canvas canvas = new Canvas(newBitmap);
// create a paint instance with alpha
Paint alphaPaint = new Paint();
alphaPaint.setAlpha(42);
// now lets draw using alphaPaint instance
canvas.drawBitmap(originalBitmap, 0, 0, alphaPaint);

// now lets store the bitmap to a file - the canvas has drawn on the newBitmap, so we can just store that one
// please add stream handling with try/catch blocks
FileOutputStream fos = new FileOutputStream(new File("/awesome/path/to/bitmap.png"));
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
public Bitmap makeTransparent(Bitmap src, int value) {  
    int width = src.getWidth();
    int height = src.getHeight();
       Bitmap transBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
       Canvas canvas = new Canvas(transBitmap);
       canvas.drawARGB(0, 0, 0, 0);
        // config paint
        final Paint paint = new Paint();
        paint.setAlpha(value);
        canvas.drawBitmap(src, 0, 0, paint);    
        return transBitmap;
}
Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);       
Paint transparentpainthack = new Paint();
transparentpainthack.setAlpha(100);
canvas.drawBitmap(bgr, 0, 0, transparentpainthack);

https://dzone.com/articles/adjusting-opacity-android proposes: https://dzone.com/articles/adjusting-opacity-android建议:

/**
 * @param bitmap The source bitmap.
 * @param opacity a value between 0 (completely transparent) and 255 (completely
 * opaque).
 * @return The opacity-adjusted bitmap.  If the source bitmap is mutable it will be
 * adjusted and returned, otherwise a new bitmap is created.
 */
private Bitmap adjustOpacity(Bitmap bitmap, int opacity)
{
    Bitmap mutableBitmap = bitmap.isMutable()
                       ? bitmap
                       : bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int colour = (opacity & 0xFF) << 24;
    canvas.drawColor(colour, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}

Note that with DST_IN you can modify (rather than reset) the transparency of already transparent image, that is, you can make the image more and more transparent.请注意,使用DST_IN可以修改(而不是重置)已经透明图像的透明度,即可以使图像越来越透明。

If you are using a Drawable to display the image, you can change the alpha as follows:如果您使用 Drawable 来显示图像,您可以按如下方式更改 alpha:

private Drawable mTriangle;
mTriangle = context.getResources().getDrawable(R.drawable.triangle_arrow_for_radar);

...

protected void onDraw(Canvas canvas)
{
    // Draw the triangle arrow
    float imageTargetWidth = getWidth() / 15;
    float scale = mTriangle.getIntrinsicWidth() / imageTargetWidth;

    int imgWidth  = (int)(imageTargetWidth);
    int imgHeight = (int)(mTriangle.getIntrinsicHeight() / scale);

    if (mTriangle != null)
    {
        mTriangle.setBounds(getWidth() / 2 - imgWidth / 2, getHeight() / 2 -       imgHeight / 2, getWidth() / 2 + imgWidth / 2, getHeight() / 2 + imgHeight / 2);

        mTriangle.setAlpha(150); // from (transparent) to 255 (opaque)
        mTriangle.draw(canvas);
    }
}

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

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