Android Question draw bitmap with alpha

ilan

Expert
Licensed User
Longtime User
Upvote 0

sorex

Expert
Licensed User
Longtime User
you could read a pixel alter the alpha and store it again but that would be way too slow.

there might be faster techniques to do that tho.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
In the same link you provided, the third answer gives the way to go. It will not change the bitmap alpha since it can be inmutable, but will generate a alpha copy

A tested approach using the idea given there is (uses inline Java but could also be done with just Javaobjects)

B4X:
Sub CreateBitmapWithAlpha(myOriginalBitmap As Bitmap, newAlpha As Int) As Bitmap
  Dim canvas1 As Canvas
  Dim myNewBitmap As Bitmap
  myNewBitmap.initializeMutable(myOriginalBitmap.Width,myOriginalBitmap.Height)
  canvas1.initialize2(myNewBitmap)
 
  Dim CanvJO As JavaObject = canvas1
  CanvJO = CanvJO.GetField("canvas")  
 
  Dim J As JavaObject
  J.initializeContext()
  J.Runmethod("alphatize",Array(CanvJO,myOriginalBitmap,newAlpha))
  Return(myNewBitmap)

End Sub

Your Java code
B4X:
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public void alphatize(Canvas mCanvas, Bitmap mBitmap, int mAlpha){
  Paint paint =new Paint();
  paint.setAlpha(mAlpha);
  Rect mDstRect = new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight()); //We are assuming they are the same size
  mCanvas.drawBitmap(mBitmap, null, mDstRect, paint);
}
 
Upvote 0
Top