Android Question XUI Image Slider FillImageToView

Puthut Wibowo

Member
Licensed User
Based on the Threat in the following link

How to create FillImageToView in a XUI Slider?

I have tried the following code
B4X:
Sub ImageSlider1_GetImage (Index As Int) As ResumableSub
    Private ImageView1 As ImageView
    ImageView1.Initialize("ImageView1")
    
    Wait For (CallSub2(Starter, "DownloadImage", Index)) Complete (x As B4XBitmap)
     FillImageToView(x, ImageView1)
    
    Return x
End Sub

And Use this code

B4X:
Sub FillImageToView(bmp As B4XBitmap, ImageView As B4XView)   
    Dim bmpRatio As Float = bmp.Width / bmp.Height
    Dim viewRatio As Float = ImageView.Width / ImageView.Height
    If viewRatio > bmpRatio Then
        Dim NewHeight As Int = bmp.Width / viewRatio
        bmp = bmp.Crop(0, bmp.Height / 2 - NewHeight / 2, bmp.Width, NewHeight)
    Else if viewRatio < bmpRatio Then
        Dim NewWidth As Int = bmp.Height * viewRatio
        bmp = bmp.Crop(bmp.Width / 2 - NewWidth / 2, 0, NewWidth, bmp.Height)
    End If
    ImageView.SetBitmap(bmp.Resize(ImageView.Width, ImageView.Height, True))
End Sub


And Get this error messages

B4X:
Error occurred on line: 330 (Main)
java.lang.IllegalArgumentException: width and height must be > 0
    at android.graphics.Bitmap.createBitmap(Bitmap.java:1074)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:1041)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:991)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:912)
    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:782)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Resize(CanvasWrapper.java:568)
    at anywheresoftware.b4a.objects.B4XViewWrapper$B4XBitmapWrapper.Resize(B4XViewWrapper.java:617)
    at exemplo.menu.branco.main._fillimagetoview(main.java:1043)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:250)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1761)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7094)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
 

Puthut Wibowo

Member
Licensed User
Call ImageView1.SetLayout and set its width and height. Only then set the bitmap.

I Have try to ImageView1.SetLayout and set its width and height then set the bitmap with this following code and the result in Attachment (WithFill.Jpeg) and Without FillFit (NoFitFill.jpeg)



B4X:
Sub ImageSlider1_GetImage (Index As Int) As ResumableSub

Private ImageView1 As ImageView
ImageView1.Initialize("ImageView1")
  
    Wait For (CallSub2(Starter, "DownloadImage", Index)) Complete (x As B4XBitmap)
        '  ----- Image Slider From Designer -----
        'ImageSlider1.SetLeftAndRight(5%x,95%x)
        'ImageSlider1.SetTopAndBottom(3%y,30%y)
   
   
        ImageView1.SetLayout(5%x, 3%y, 90%x, 27%y)
        ImageView1.SetBackgroundImage(x)  
       
        FillImageToView(x, ImageView1)
   
    Return x
End Sub
 

Attachments

  • WithFill.jpeg
    WithFill.jpeg
    97.7 KB · Views: 151
  • NoFitFill.jpeg
    NoFitFill.jpeg
    96.7 KB · Views: 161
Upvote 0

Puthut Wibowo

Member
Licensed User
I'm not sure that I understand what you are trying to do.
You need to return a bitmap not ImageView. Maybe upload a small example.

I want to change the image in the image slider like Fit, radius rectangle

how do you change the image in the image slider?
 

Attachments

  • Asked.zip
    44.5 KB · Views: 175
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
"Fit" is already implemented in your code on this line:
B4X:
x = x.Resize(ImageSlider1.WindowBase.Width, ImageSlider1.WindowBase.Height, True)
This will return a round rect image though this doesn't look very good with wide images:
B4X:
Return CreateRoundRectBitmap(x, 25dip)
Might be better to use this one: https://www.b4x.com/android/forum/threads/b4x-xui-create-a-round-image.85102/
 
Upvote 0
Top