Android Question [SOLVED] - SetBitmap changes ImageView bitmap image from a circle to a square

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

Using SetBitmap changes one of my ImageView bitmap image from a circle shape to a square shape. That caught me by surprise. Please check my usage of SetBitmap and please let me know how I messed up the displaying of my image.

This is what the play-circle icon looked like before and after using SetBitmap:

WhatsApp Image 2020-11-18 at 06.17.40.jpeg WhatsApp Image 2020-11-18 at 06.17.40 (1).jpeg

B4X:
Sub ImageViewPlayPauseSong_Click
  
    ' The xCLV has a button on each item row. "Sender" will know which of those
    ' buttons raised this click event.
    '-------------------------------------------------------------------------------
    Dim ViewThisImageView As ImageView = Sender
    Dim intindex As Int = clvSongList.GetItemFromView(Sender)
  
    CSelections.ItemClicked(intindex) ' Show the CLV item as selected or unselected.

    kvs.Put("CurrentSongSelected", intindex +1) ' This is the song number to play.
      
    Log( "intIndex: " & intindex)
    Log( "intLastClickedIndex: " & intLastClickedIndex)
  
    If intindex <> intLastClickedIndex Then

        ' From the xCLV line item at the index from the view that was last clicked.
        '--------------------------------------------------------------------------
        Dim pnl As B4XView = clvSongList.GetPanel(intLastClickedIndex)

        ' Change the icon of the last clicked image view and make it a play icon because
        ' we clicked on another item line of the xCLV.
        ' This play/pause image view is from the panel and child panel.
        '--------------------------------------------------------------------------------
        pnl.GetView(0).GetView(6).SetBitmap(LoadBitmap(File.DirAssets, "play-circle.png"))

        ' For this current clicked ImageView we make it a pause icon.
        '------------------------------------------------------------
        ViewThisImageView.Bitmap = LoadBitmap(File.DirAssets, "pause-circle.png")

        intLastClickedIndex = intindex

        If blnAsongIsPlaying = False Then
            ImageViewPlayCurrentSong_Click
        End If
    Else
        If blnAsongIsPlaying Then
            ViewThisImageView.Bitmap = LoadBitmap(File.DirAssets, "play-circle.png")
        Else
            ViewThisImageView.Bitmap = LoadBitmap(File.DirAssets, "pause-circle.png")
        End If
  
        ImageViewPlayCurrentSong_Click
    End If
End Sub
 
Last edited:

rleiman

Well-Known Member
Licensed User
Longtime User
Use XUIViewsUtils.SetBitmapAndFill or switch to B4XImageView (XUI Views) instead. B4XImageView is the best option.
Forgive me for asking a stupid question. How do I create a click sub routine for the B4XImageView? I couldn't find it in the pop-up list.

Screenshot 2020-11-18 at 11.19.49.png
Since I couldn't find click, I tried to use XUIViewsUtils.SetBitmapAndFill like this:

B4X:
XUIViewsUtils.SetBitmapAndFill(pnl.GetView(0).GetView(7),LoadBitmap(File.DirAssets, "play-circle.png"))

I get this cast error in the logs:

B4X:
intLastClickedIndex: 0
The view at pnl.GetView(0).GetView(7) is: (BALayout) anywheresoftware.b4a.BALayout{2f8a6ae V.E...... ........ 730,488-908,666}

xuiviewsutils_setbitmapandfill (java line: 92)
java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to android.widget.ImageView
    at b4a.natures.song.xuiviewsutils._setbitmapandfill(xuiviewsutils.java:92)
    at b4a.natures.song.main._imageviewplaypausesong_click(main.java:672)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7869)
    at android.view.View.performClickInternal(View.java:7838)
    at android.view.View.access$3600(View.java:886)
    at android.view.View$PerformClick.run(View.java:29362)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:8125)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Since I couldn't find click, I tried to use XUIViewsUtils.SetBitmapAndFill like this:

There is no click:
You load the pictures with a code similar to this:
B4X:
B4XImageView1.Load(File.DirAssets, "play-circle.png")  'use whatever name you have for the B5XImageView, or pnl.getview(0)....etc
Then for example on my project I have:
B4X:
Sub Label1_Click  'Label1 is the event name
    Dim index As Int = CustomListView1.GetItemFromView(Sender)
    Dim pnl As B4XView = CustomListView1.GetPanel(index)
    Dim l As B4XView = pnl.GetView(0)  'label which is the 2nd view in the item panel
    Log(l.Text)  
    Dim b4xiv As B4XImageView = pnl.GetView(1).Tag  'make sure you respect the tree hierarchy. In your case it maybe: pnl.GetView(0).GetView(7)
    b4xiv.RoundedImage =True
End Sub
Just to give you an idea to use for your project , normal use of B4XImageView. B4XImageView is an XUI View, don't forget to use .tag in
pnl.GetView(1).Tag because the object is stored and referenced in the tag property when dealing with B4X Views.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
There is no click:
You load the pictures with a code similar to this:
B4X:
B4XImageView1.Load(File.DirAssets, "play-circle.png")  'use whatever name you have for the B5XImageView, or pnl.getview(0)....etc
Then for example on my project I have:
B4X:
Sub Label1_Click  'Label1 is the event name
    Dim index As Int = CustomListView1.GetItemFromView(Sender)
    Dim pnl As B4XView = CustomListView1.GetPanel(index)
    Dim l As B4XView = pnl.GetView(0)  'label which is the 2nd view in the item panel
    Log(l.Text)
    Dim b4xiv As B4XImageView = pnl.GetView(1).Tag  'make sure you respect the tree hierarchy. In your case it maybe: pnl.GetView(0).GetView(7)
    b4xiv.RoundedImage =True
End Sub
Just to give you an idea to use for your project , normal use of B4XImageView. B4XImageView is an XUI View, don't forget to use .tag in
pnl.GetView(1).Tag because the object is stored and referenced in the tag property when dealing with B4X Views.
Hi Mahares,

Thanks so much for helping me in the past.

I got it to load into the B4XImageView, but since it doesn't have a click handler I will try to do it with a swift button. Here's the coding I used:

B4X:
Dim b4xiv As B4XImageView = pnl.GetView(0).GetView(7).tag
b4xiv.Load(File.DirAssets, "pause-circle.png")
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I tried the swift button but the auto-complete had nothing to load a bitmap so I ultimately was able to get the original ImageView to successfuly display the bitmap image without any distortion by using the following coding based on Mahares samples. There is something off about using SetBitmap so any time I need to change an ImageView on an xCLV in a view click handler, I will use this technique of coding.

B4X:
Sub ImageViewPlayPauseSong_Click
    
    Dim intindex As Int = clvSongList.GetItemFromView(Sender) ' xCLV Index of clicked view.

    Dim pnlLastClicked As B4XView = clvSongList.GetPanel(intLastClickedIndex)
    Dim ImageViewPlayPauseSongLastClicked As ImageView = pnlLastClicked.GetView(0).GetView(6)
    Dim pnlCurrentlyClicked As B4XView = clvSongList.GetPanel(intindex)
    Dim ImageViewPlayPauseSongCurrentlyClicked As ImageView = _
        pnlCurrentlyClicked.GetView(0).GetView(6)

    CSelections.ItemClicked(intindex) ' Show the CLV item as selected or unselected.

    kvs.Put("CurrentSongSelected", intindex +1) ' This is the song number to play.
        
    If intindex <> intLastClickedIndex Then

        ' Change the icon of the last clicked image view and make it a play icon because
        ' we clicked on another item line of the xCLV.
        ' the play/pause image view from the panel And child panel.
        '--------------------------------------------------------------------------------       
        ImageViewPlayPauseSongLastClicked.Bitmap = LoadBitmap(File.DirAssets, "play-circle.png")

        ' For this current clicked ImageView we make it a pause icon.
        '------------------------------------------------------------
        ImageViewPlayPauseSongCurrentlyClicked.Bitmap = _
            LoadBitmap(File.DirAssets, "pause-circle.png")

        intLastClickedIndex = intindex

        If blnAsongIsPlaying = False Then
            ImageViewPlayCurrentSong_Click
        End If
    Else
        If blnAsongIsPlaying Then
            ImageViewPlayPauseSongCurrentlyClicked.Bitmap = _
                LoadBitmap(File.DirAssets, "play-circle.png")
        Else
            ImageViewPlayPauseSongCurrentlyClicked.Bitmap = _
                LoadBitmap(File.DirAssets, "pause-circle.png")
        End If
    
        ImageViewPlayCurrentSong_Click
    End If
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
There is something off about using SetBitmap
I am glad you got it figured out. If you want to use SetBitmap, here is what you need to do:
B4X:
Private ImageView1 As B4XView '  'Add an imageview to the layout in designer and declare it as B4XView in Globals
Then in your code you can use setbitmap like this:
B4X:
ImageView1.SetBitmap(LoadBitmap(File.DirAssets,"play-circle.png"))
The B4XImageView is an XUI View, different from B4XView declaration.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I am glad you got it figured out. If you want to use SetBitmap, here is what you need to do:
B4X:
Private ImageView1 As B4XView '  'Add an imageview to the layout in designer and declare it as B4XView in Globals
Then in your code you can use setbitmap like this:
B4X:
ImageView1.SetBitmap(LoadBitmap(File.DirAssets,"play-circle.png"))
The B4XImageView is an XUI View, different from B4XView declaration.
Hi Mahares,

Maybe it would work for non-xCLV use but since it distorted my bitmap, I'm going going to stick to using code based on what you taught me ealier.
 
Upvote 0
Top