Merging Images with Opacity

vb1992

Well-Known Member
Licensed User
Longtime User
I noticed Klaus' posted a reflection solution


Dim obj1 As Reflector

obj1.Target = Panel1.Background
obj1.RunMethod2("setAlpha", 128, "java.lang.int")


but I still need to get my head around this
to merge two images into one...
 
Upvote 0

admac231

Active Member
Licensed User
Longtime User
This is using the Reflection library only.

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim imgForeGround As BitmapDrawable
   imgForeGround.Initialize(LoadBitmap(File.DirAssets,"vw.jpg"))
   
   Dim imgBackGround As Bitmap
   imgBackGround.Initialize(File.DirAssets,"sunset.jpg")
   
   Dim obj1 As Reflector
   obj1.Target = imgForeGround
   obj1.RunMethod2("setAlpha", 0, "java.lang.int")
   
   Dim displayImg As ImageView
   displayImg.Initialize("displayImg")
   displayImg.Bitmap = imgBackGround
   displayImg.Gravity = Gravity.FILL
   Activity.AddView(displayImg,10dip,50dip,100%x-20dip,100%y-50dip)

   Dim destRect As Rect
   destRect.Initialize(0,0,displayImg.Width,displayImg.Height)
   
   Dim canNewImg As Canvas
   canNewImg.Initialize(displayImg)
   canNewImg.DrawDrawable(imgForeGround,destRect)
   
   Dim seekBar1 As SeekBar
   seekBar1.Initialize("seekBar1")
   seekBar1.Max = 255
   Activity.AddView(seekBar1, 10dip,10dip,100%x-20dip,30dip)
End Sub

Sub seekBar1_ValueChanged (Value As Int, UserChanged As Boolean)
   obj1.RunMethod2("setAlpha", Value, "java.lang.int")
   canNewImg.DrawBitmap(imgBackGround,Null,destRect)
   canNewImg.DrawDrawable(imgForeGround,destRect)
   displayImg.Bitmap = canNewImg.Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Most of the stuff doesn't need to be there (loading background into imageview first and the seekbar) but I added it to give a good example. After you run the code in seekBar1_ValueChanged canNewImg.Bitmap contains your new bitmap.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I looked too at this problem.
You can use alwaysbusys' ABExtDraawing library.

Attached a small test program, where two images are overlayed.
The overlay can be set between 0 and 255 witha SeekBar.
0 = image1 opaque image2 transparent
255 = image2 opaque image1 transparent
B4X:
Sub Globals
    Dim bmp1, bmp2 As Bitmap
    Dim Panel1 As Panel
    Dim imvTest As ImageView
    Dim cvsTest As Canvas
    Dim skbTest As SeekBar
    Dim ABExtDrawas As ABExtDrawing
    Dim ABExtPaint As ABPaint
    Dim rectDest As Rect
    Dim Alpha As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    imvTest.Initialize("")
    Activity.AddView(imvTest, 10%x , 10%x, 80%x, 53%x)

    skbTest.Initialize("skbTest")
    Activity.AddView(skbTest, 10%x , 60%x, 80%x, 30dip)
    skbTest.Max = 255
    skbTest.Top = 70%x

    bmp1.Initialize(File.DirAssets, "image0.jpg")
    bmp2.Initialize(File.DirAssets, "image2.jpg")
    
    Alpha = 0
    skbTest.Value = 0
    
    rectDest.Initialize(0, 0, imvTest.Width, imvTest.Height)
    cvsTest.Initialize(imvTest)
    ABExtPaint.Initialize
    ABExtPaint.SetAlpha(255 - Alpha)
    ABExtDrawas.drawBitmap(cvsTest, bmp1, Null, rectDest, ABExtPaint)

    ABExtPaint.Initialize
    ABExtPaint.SetAlpha(Alpha)
    ABExtDrawas.drawBitmap(cvsTest, bmp2, Null, rectDest, ABExtPaint)
End Sub

Sub skbTest_ValueChanged (Value As Int, UserChanged As Boolean)
    If UserChanged Then
        Alpha = Value
        
        imvTest.RemoveView
        imvTest.Initialize("")
        Activity.AddView(imvTest, 10%x , 10%x, 80%x, 53%x)
        cvsTest.Initialize(imvTest)
        
        ABExtPaint.Initialize
        ABExtPaint.SetAlpha(255 - Alpha)
        ABExtDrawas.drawBitmap(cvsTest, bmp1, Null, rectDest, ABExtPaint)

        ABExtPaint.Initialize
        ABExtPaint.SetAlpha(Alpha)
        ABExtDrawas.drawBitmap(cvsTest, bmp2, Null, rectDest, ABExtPaint)
        
        imvTest.Invalidate
    End If
End Sub
Best regards.
 

Attachments

  • BitmapTransparency.zip
    85.3 KB · Views: 379
Upvote 0
Top