B4J Question B4XImageView Opacity?

techknight

Well-Known Member
Licensed User
Longtime User
I am trying to figure out how to set the opacity of the image view, so PNGs loaded inside of it can appear semi-transparent. I know I can do it with regular ImageViews with the designer, but I cannot figure out how to do it with a B4XImageView in code only.

Thoughts? thanks.
 

TILogistic

Expert
Licensed User
Longtime User
B4XImageView:
B4X SetAlphaToimage.

1720498550175.png

Adjust the alpha color you want
B4X:
    B4XImageView1.mBackgroundColor = xui.Color_Transparent
    B4XImageView1.Bitmap = SetAlphaToImage(xui.LoadBitmap(File.DirAssets, "1.png"), 120)

B4X (bitmapcreator library)
EDIT:
B4X:
Private Sub SetAlphaToImage(TargetImage As B4XBitmap, ValueAlpha As Int) As B4XBitmap
    Dim bc As BitmapCreator
    bc.Initialize(TargetImage.Width, TargetImage.Height)
    bc.CopyPixelsFromBitmap(TargetImage)
    Dim ARGBColor1 As ARGBColor
    For x = 0 To bc.mWidth - 1
        For y = 0 To bc.mHeight - 1
            bc.GetARGB(x, y, ARGBColor1)
            If ARGBColor1.a > 0 Then
                ARGBColor1.a = ValueAlpha
                bc.SetARGB(x, y, ARGBColor1)
                bc.BlendPixel(bc, x, y, x, y)
            End If
        Next
    Next
    Return bc.Bitmap
End Sub
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
if you don't use BlendPixel
B4X:
Private Sub SetAlphaToImage(TargetImage As B4XBitmap, ValueAlpha As Int) As B4XBitmap
    Dim bc As BitmapCreator
    bc.Initialize(TargetImage.Width, TargetImage.Height)
    bc.CopyPixelsFromBitmap(TargetImage)
    Dim ARGBColor1 As ARGBColor
    For x = 0 To bc.mWidth - 1
        For y = 0 To bc.mHeight - 1
            bc.GetARGB(x, y, ARGBColor1)
            If ARGBColor1.a > 0 Then
                ARGBColor1.a = ValueAlpha
                bc.SetARGB(x, y, ARGBColor1)
'                bc.BlendPixel(bc, x, y, x, y)
            End If
        Next
    Next
    Return bc.Bitmap
End Sub
1720499740435.png
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
B4XImageView:
B4X SetAlphaToimage.

View attachment 155331
Adjust the alpha color you want
B4X:
    B4XImageView1.mBackgroundColor = xui.Color_Transparent
    B4XImageView1.Bitmap = SetAlphaToImage(xui.LoadBitmap(File.DirAssets, "1.png"), 120)

B4X (bitmapcreator library)
EDIT:
B4X:
Private Sub SetAlphaToImage(TargetImage As B4XBitmap, ValueAlpha As Int) As B4XBitmap
    Dim bc As BitmapCreator
    bc.Initialize(TargetImage.Width, TargetImage.Height)
    bc.CopyPixelsFromBitmap(TargetImage)
    Dim ARGBColor1 As ARGBColor
    For x = 0 To bc.mWidth - 1
        For y = 0 To bc.mHeight - 1
            bc.GetARGB(x, y, ARGBColor1)
            If ARGBColor1.a > 0 Then
                ARGBColor1.a = ValueAlpha
                bc.SetARGB(x, y, ARGBColor1)
                bc.BlendPixel(bc, x, y, x, y)
            End If
        Next
    Next
    Return bc.Bitmap
End Sub

Awesome! it works well... Except now it has the side-effect that the b4ximageview no longer respects the ResizeMode property.

Perhaps it may be the order at which I am applying this property, unsure.
 
Upvote 0
Top