Android Question xResizeAndCrop Square?

hasexxl1988

Active Member
Licensed User
Longtime User
Hello,
I have the following question:

I am slowly with my advice at the end and would like to know if anyone has an idea how can I make the marketed area at xResizeAndCrop always the same width and high?


So if I change the size of the selection area height and width should always be the same.

I found an old example from Klaus, this works with older Version of ResizeAndCrop at https://www.b4x.com/android/forum/threads/resize-and-crop.19550/#post-113884


Does somebody has any idea?

The Old Code:

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: ResizeAndCrop
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
    ' Version 1.3 with markers in the angles
    Dim Left, Top, Right, Bottom, Width As Int
    Dim DeltaDot, Delta, DeltaX, DeltaY, StartX, StartY, MarkerDot, MarkerDot_1 As Int
    Dim fTopLeft, fTopRight, fBottomLeft, fBottomRight, fCenter As Boolean
    Dim colMarker As Int        : colMarker = Colors.RGB(255, 215, 0)
End Sub

Sub Globals
    Dim imvImage As ImageView
    Dim pnlAction As Panel
    Dim cvsAction As Canvas
    Dim rectImage, rectOuter As Rect
End Sub

Sub Activity_Create(FirstTime As Boolean)
    imvImage.Initialize("")
    Activity.AddView(imvImage, 0, 20%y, 100%x, 67%x)
    imvImage.Bitmap= LoadBitmap(File.DirAssets, "image0.jpg")
    imvImage.Gravity = Gravity.FILL
  
    pnlAction.Initialize("pnlAction")
    Activity.AddView(pnlAction, 0, 0, 100%x, 100%y )
    pnlAction.Color = Colors.ARGB(128, 0, 0, 0)
    cvsAction.Initialize(pnlAction)
  
    Width = imvImage.Height
    Top = imvImage.Top
    Bottom = Top + Width
    Left = 50%x - Width / 2
    Right = Left + Width
    rectImage.Initialize(Left, Top, Right, Bottom)    ' square of croped bitmap
    rectOuter.Initialize(Left, Top, Right, Bottom)    ' outer square including the markers
    cvsAction.DrawRect(rectImage, Colors.Transparent, True , 1)    ' draws the transparent croped square
  
    DeltaDot = 10dip                            ' half of selection square
    MarkerDot = 6dip                            ' half of marker square
    MarkerDot_1 = MarkerDot + 1        ' half of marker square + 1 for outer square
    DrawMarkers
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub pnlAction_Touch (Action As Int, X As Float, Y As Float)
    Select Action
    Case Activity.ACTION_DOWN
        fTopLeft = False
        fTopRight = False
        fBottomLeft = False
        fBottomRight = False
        fCenter = False
        If X >= Left - DeltaDot And X <= Left + DeltaDot And Y >= Top - DeltaDot And Y <= Top + DeltaDot Then
            fTopLeft = True
            Activity.Title = "TopLeft"
        Else If X >= Left - DeltaDot And X <= Left + DeltaDot And Y >= Bottom - DeltaDot And Y <= Bottom + DeltaDot Then
            fBottomLeft = True
            Activity.Title = "BottomLeft"
        Else If X >= Right - DeltaDot And X <= Right + DeltaDot And Y >= Top - DeltaDot And Y <= Top + DeltaDot Then
            fTopRight = True
            Activity.Title = "TopRight"
        Else If X >= Right - DeltaDot And X <= Right + DeltaDot And Y >= Bottom - DeltaDot And Y <= Bottom + DeltaDot Then
            fBottomRight = True
            Activity.Title = "BottomRight"
        Else If X >= Left + DeltaDot And X < Right - DeltaDot And Y >= Top + DeltaDot And Y < Bottom - DeltaDot Then
            fCenter = True
            Activity.Title = "Center"
        End If     
        StartX = X
        StartY = Y
    Case Activity.ACTION_MOVE
        Dim Diff, Sign As Int
        DeltaX = X - StartX
        DeltaY = Y - StartY
        If Abs(DeltaX) >= Abs(DeltaY) Then
            Delta = DeltaX
        Else
            Delta = DeltaY
        End If
        cvsAction.DrawRect(rectOuter, Colors.Transparent, True , 1)
        cvsAction.DrawRect(rectOuter, Colors.ARGB(128, 0, 0, 0), True , 1)
        pnlAction.Invalidate2(rectOuter)
        If fCenter = True Then
            rectImage.Left = Left + DeltaX
            rectImage.Right = Right + DeltaX
            rectImage.Top = Top + DeltaY
            rectImage.Bottom = Bottom + DeltaY
            If rectImage.Left < imvImage.Left Then
                Diff = imvImage.Left - rectImage.Left
                rectImage.Left = imvImage.Left
                rectImage.Right = rectImage.Right + Diff
            End If
            If rectImage.Top < imvImage.Top Then
                Diff = imvImage.Top - rectImage.Top
                rectImage.Top = imvImage.Top
                rectImage.Bottom  = rectImage.Bottom + Diff
            End If
            If rectImage.Right >  imvImage.Left + imvImage.Width Then
                Diff = rectImage.Right - (imvImage.Left + imvImage.Width)
                rectImage.Right = imvImage.Left + imvImage.Width
                rectImage.Left  = rectImage.Left - Diff
            End If
            If rectImage.Bottom > imvImage.Top + imvImage.Height Then
                Diff = rectImage.Bottom - (imvImage.Top + imvImage.Height)
                rectImage.Bottom = imvImage.Top + imvImage.Height
                rectImage.Top  = rectImage.Top - Diff
            End If
        Else
            If fTopLeft = True Then
                If Left + Delta > imvImage.Left And Top + Delta > imvImage.Top Then
                    rectImage.Left = Left + Delta
                    rectImage.Top = Top + Delta
                End If
            End If
            If fTopRight = True And DeltaX <> 0 Then
                Sign = DeltaY / Abs(DeltaY)
                If Abs(DeltaX) >= Abs(DeltaY) Then
                    Delta = Abs(DeltaX) * Sign
                Else
                    Delta = DeltaY
                End If
                If Top + Delta > imvImage.Top And Right - Delta < imvImage.Left + imvImage.Width Then
                    rectImage.Top = Top + Delta
                    rectImage.Right = Right - Delta
                End If
            End If
            If fBottomLeft = True Then
                Sign = DeltaY / Abs(DeltaY)
                If Abs(DeltaX) >= Abs(DeltaY) Then
                    Delta = Abs(DeltaX) * Sign
                Else
                    Delta = DeltaY
                End If
                If Bottom + Delta < imvImage.Top + imvImage.Height And Left - Delta > imvImage.Left Then
                    rectImage.Bottom = Bottom + Delta
                    rectImage.Left = Left - Delta
                End If
            End If
            If fBottomRight = True Then
                If Bottom + Delta < imvImage.Top + imvImage.Height And Right + Delta < imvImage.Left + imvImage.Width Then
                    rectImage.Bottom = Bottom + Delta
                    rectImage.Right = Right + Delta
                End If
            End If
        End If
        cvsAction.DrawRect(rectImage, Colors.Transparent, True , 1)
        pnlAction.Invalidate2(rectImage)
        DrawMarkers
        rectOuter.Initialize(rectImage.Left - MarkerDot_1, rectImage.Top - MarkerDot_1, rectImage.Right + MarkerDot_1, rectImage.Bottom + MarkerDot_1)
    Case Activity.ACTION_UP
        Left = rectImage.Left
        Top = rectImage.Top
        Right = rectImage.Right
        Bottom = rectImage.Bottom
        Activity.Title = ""
    End Select
End Sub

Sub DrawMarkers
    Dim r As Rect
  
    r.Initialize(rectImage.Left - MarkerDot, rectImage.Top - MarkerDot, rectImage.Left + MarkerDot, rectImage.Top + MarkerDot)
    cvsAction.DrawRect(r, colMarker, True , 1)
    pnlAction.Invalidate2(r)
  
    r.Initialize(rectImage.Right - MarkerDot, rectImage.Top - MarkerDot, rectImage.Right + MarkerDot, rectImage.Top + MarkerDot)
    cvsAction.DrawRect(r, colMarker, True , 1)
    pnlAction.Invalidate2(r)
  
    r.Initialize(rectImage.Left - MarkerDot, rectImage.Bottom - MarkerDot, rectImage.Left + MarkerDot, rectImage.Bottom + MarkerDot)
    cvsAction.DrawRect(r, colMarker, True , 1)
    pnlAction.Invalidate2(r)
  
    r.Initialize(rectImage.Right - MarkerDot, rectImage.Bottom - MarkerDot, rectImage.Right + MarkerDot, rectImage.Bottom + MarkerDot)
    cvsAction.DrawRect(r, colMarker, True , 1)
    pnlAction.Invalidate2(r)
  
End Sub
 

klaus

Expert
Licensed User
Longtime User
Can you please try the attached class file?
In the Designer check the Square property.

upload_2019-1-29_21-46-52.gif
 

Attachments

  • xResizeAndCrop.bas
    21 KB · Views: 317
Upvote 0

hasexxl1988

Active Member
Licensed User
Longtime User
Can you please try the attached class file?
In the Designer check the Square property.

View attachment 76878

The Class not Work. :-(

Error:
B4X:
Error occurred on line: 75 (xResizeAndCrop)
java.lang.RuntimeException: Cannot parse: null as boolean
    at anywheresoftware.b4a.BA.parseBoolean(BA.java:607)
    at anywheresoftware.b4a.BA.ObjectToBoolean(BA.java:677)
    at com.logo.picture.xresizeandcrop._designercreateview(xresizeandcrop.java:635)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:355)
    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.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:61)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:162)
    at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
    at com.logo.picture.main._activity_create(main.java:433)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:355)
    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 com.logo.picture.main.afterFirstLayout(main.java:104)
    at com.logo.picture.main.access$000(main.java:17)
    at com.logo.picture.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:794)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:173)
    at android.app.ActivityThread.main(ActivityThread.java:6653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)
** Activity (main) Resume **

Any ideas?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Click on the custom view in the designer. It will then add the new property.

Tip for Klaus:
When you add a new designer property you should get its value like this:
B4X:
Dim Square As Boolean = Props.GetDefault("square", False)
This way it will work with old layouts which do not include the new property.
 
Upvote 0
Top