Jak zmienić krycie bitmapy?

Mam bitmapę:

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

Ale nie zamierzam wyświetlać obrazu użytkownikowi. Chcę, aby Alfa była 100 (z 255). Jeśli nie jest to możliwe, Czy Mogę ustawić krycie Bitmap?

Author: Jona, 2011-02-25

6 answers

Możesz również spróbować BitmapDrawable zamiast Bitmap. Jeśli jest to dla Ciebie przydatne, zależy od sposobu korzystania z bitmapy...

Edit

Jako komentator zapytał, jak może przechowywać bitmapę z alfą, oto kod:

// 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);
 31
Author: WarrenFaith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-09 09:52:40

Z tego co wiem, nieprzezroczystość lub inne filtry kolorów nie mogą być ustawione na samej Bitmapie. Musisz ustawić Alfę, gdy używasz obrazu:

Jeśli używasz ImageView, istnieje ImageView.setAlpha () .

Jeśli używasz płótna, musisz użyć Paint.setAlpha():

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

Również, włączając odpowiedź Warrenfaitha, jeśli użyjesz bitmapy, w której wymagany jest drawable, możesz użyć BitmapDrawable.setAlpha () .

 78
Author: Matthew Willis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-25 15:30:12
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;
}
 19
Author: Ruban,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-24 10:49:31
Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);       
Paint transparentpainthack = new Paint();
transparentpainthack.setAlpha(100);
canvas.drawBitmap(bgr, 0, 0, transparentpainthack);
 18
Author: Martin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-05 20:21:49

Https://dzone.com/articles/adjusting-opacity-android proponuje:

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

Zauważ, że za pomocą DST_IN możesz zmodyfikować (zamiast zresetować) przezroczystość już przezroczystego obrazu, czyli możesz uczynić obraz coraz bardziej przezroczystym.

 2
Author: 18446744073709551615,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:42

Jeśli używasz Drawable do wyświetlania obrazu, możesz zmienić Alfę w następujący sposób:

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);
    }
}
 0
Author: Frédéric,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-03 20:04:38