Android Question [Solved] Flip functions question (B4XBitmapEffects)

Pflichtfeld

Active Member
Licensed User
What is my fault, that the flip functions do not work correctly in my testapp (attached)?
 

Attachments

  • Flip.zip
    9.7 KB · Views: 199

Pflichtfeld

Active Member
Licensed User
These are the screenshots
Source: Original.pngFlipped horizontally:FlipHorizontal.pngFlipped vertically:FlipVertical.png
(Lower imageview is not used in this example)
And this is the code: (img1 is a b4xview)
B4X:
    Dim iv As ImageView = Img1   
    Dim bmp As B4XBitmap = iv.Bitmap
    Img1.SetBitmap(bmCEf.FlipHorizontal(bmp))
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is related to the bitmap scale and to the way you set the image to the ImageView.
There are several ways to solve it. One way:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Btn1 As B4XView
    Private Btn2 As B4XView
    Private Btn3 As B4XView
    Private Img1 As B4XView
    Private Img2 As B4XView
    Dim bmCEf As BitmapCreatorEffects
    Private xui As XUI
End Sub


Sub Activity_Create(FirstTime As Boolean)
    bmCEf.Initialize
    Activity.LoadLayout("Ly0")
    Btn3.Visible=False
    fillImg1(Img1)
End Sub

Private Sub fillImg1(Img As B4XView)
    Dim bc As BitmapCreator
    bc.Initialize(Img.Width / xui.Scale, Img.Height / xui.Scale) '<-- disable the scale factor. This will improve performance.
    Dim gpx As BCPath
    gpx.Initialize(0,0)
    gpx.LineTo(bc.mWidth/2, bc.mHeight/3)
    gpx.LineTo(bc.mWidth/2,bc.mHeight)
    gpx.LineTo(0,bc.mHeight)
    gpx.LineTo(0,0)
    bc.DrawPath(gpx,Colors.Red,True,0)
    Img1.SetBitmap(bc.Bitmap)
End Sub

Sub Btn2_Click
    Dim iv As ImageView = Img1    
    Dim bmp As B4XBitmap = iv.Bitmap
    bmp=bmCEf.FlipVertical(bmp)
    Img1.SetBitmap(bmp)
End Sub

Sub Btn1_Click
    Dim iv As ImageView = Img1    
    Dim bmp As B4XBitmap = iv.Bitmap
    Img1.SetBitmap(bmCEf.FlipHorizontal(bmp))
End Sub
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
Thank you Erel, another time!
so it seems that there are more differences as just Gravity between .setBitmap and .setBitmapToImageview than the intellisense tells me:

setbitmap.png


Hard to understand...
Do you somewhere have a tutorial, where I can understand the reason for that and get to know, why my case is not one of the 'most cases'.
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
I am not shure, if I may introduce the effect of fliphorizontally in my main-project in this thread or if I shall open a new one. I do it here, because I suppose it is all the same issue.
I set a figure in a label with a csbuilder (draws is a class, in original code another bmp is added too)
B4X:
Private Sub fillLblPicKanten
    Dim bmp As B4XBitmap
    Dim cs As CSBuilder
    'not flipped
    bmp=draws.drawGrindEdge(LblPicKanten.Width/2,LblPicKanten.Height,gv.LastAngle,2,0,False,False,True,True,True,False)
    'flipped                                                                          v ^
   'bmp=draws.drawGrindEdge(LblPicKanten.Width/2,LblPicKanten.Height,gv.LastAngle,2,0,True,False,True,True,True,False)
    cs.Initialize.Image(bmp,LblPicKanten.Width/2,LblPicKanten.Height,False).Popall
    LblPicKanten.Text=cs
End Sub
I build this figure in a sub in class draws: (lots of code omitted)
B4X:
public Sub drawGrindEdge(picWidth As Float,picHeight As Float, grindAngle As Float, _
    doAdjust As Int, rotAngle As Float, _
    flipHoriz As Boolean, flipVertical As Boolean, highlightBevelUpper As Boolean, _
    highlightBevelLower As Boolean,showHelpLines As Boolean, onebevel As Boolean) As B4XBitmap
    Dim canv As B4XCanvas
    Dim gpx As B4XPath
    
    '...lots of code calculating the shape of path
    Dim xview As B4XView = xui.CreatePanel("")
    xview.SetLayoutAnimated(0, 0, 0, picWidth, picHeight)
    '... lots of code, filling canvas
    canv.Initialize(xview)   
    canv.DrawPath(gpx,Colors.Black,True,0)
    canv.Invalidate

    Dim bmp As B4XBitmap=canv.CreateBitmap

    If flipHoriz Then
        bmp=BmEf.FlipHorizontal(bmp)   'this flip changes quality of bmp
    End If
    
    Return bmp
End Sub
The original bmp looks pretty, the flipped one looks poor:
OriginalBMP.png Flipped.png
Is it possible for you, to tell why the flipped one looks so bad just from this rudimentary codelines?
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
Thank you for your effort, Erel!
The draw-routine is not yet ready - I am still struggling with this project. But the strange flip-effect is to be seen.
 

Attachments

  • Flip.zip
    12.1 KB · Views: 212
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
For performance reasons, BitmapCreatorEffects scales down the bitmap, you can prevent it with this code:
B4X:
    If flipHoriz Then
        bmp=bmEF.FlipHorizontal(ScaleBitmap(bmp))
    End If
   
    Return bmp
End Sub

Sub ScaleBitmap(bmp As B4XBitmap) As B4XBitmap
    #if B4A
    Dim jo As JavaObject = bmp
    jo.RunMethod("setDensity", Array(160))
    #End If
    Return bmp
End Sub

Add a reference to JavaObject.
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
Thank you Erel for the support!! This workaround does a god job!
Pitty that these flip-functions seem to be a bit balky for newbies like me :confused:
 
Upvote 0
Top