Android Question Get value to an array

herryj

Member
Licensed User
Longtime User
Hi,
I have this randomly display of image based on one the forums given codes as shown below.

How can I pass the array value and use the reflection library to detect the touch event.

B4X:
For i = 1 To PZ.Length - 1
        bmp = LoadBitmap(File.DirAssets,"image_"&i&".png")
        vWidth = bmp.Width
        vHeight = vWidth*(bmp.Height/bmp.Width)
        img.Initialize("")
        img.Bitmap = bmp
        img.Gravity = Gravity.FILL
        Activity.AddView(img,50%x-(vWidth/2),50%y-(vHeight/2),vWidth,vHeight)
        img.Left = Rnd(100, 256)
        img.top = Rnd(100, 500)
        '------------------------------
        'PZ(i).Initialize(img,"PZ",i,True,Me)
    Next

Thanks in advance
 

herryj

Member
Licensed User
Longtime User
Hi Erel,
Yes I used Reflector.SetOnTouchListener but How can I set the reflector.Target, if my target is in the code I attached above?

can I used the Tag property of the imageview?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You need to add the Touch event for each ImageView.
Another solution would be to use Panels instead if ImageViews and use the Panels 'standard' Touch event.
Something like this, not tested:
B4X:
For i = 1 To PZ.Length - 1
    Dim bmp As Bitmap
    bmp = LoadBitmap(File.DirAssets,"image_"&i&".png")
    vWidth = bmp.Width
    vHeight = vWidth*(bmp.Height/bmp.Width)
    Dim pnl As Panel
    pnl.Initialize("pnlImage")
    Dim bdw As BitmapDrawable
    bdw.Bitmap = bmp
    pnl.Background = bdw
    pnl.Gravity = Gravity.FILL
    Activity.AddView(pnl, 50%x - (vWidth / 2), 50%y - (vHeight / 2), vWidth, vHeight)
    pnl.Left = Rnd(100, 256)
    pnl.Top = Rnd(100, 500)
    pnl.Tag = i
    '------------------------------
    'PZ(i).Initialize(img,"PZ",i,True,Me)
Next
And
B4X:
Sub pnlImage_Touch (Action As Int, X As Float, Y As Float)
    Dim pnl As Panel
    Dim Index As Int
  
    pnl = Sender
    Index = pnl.Tag
    ' your code
End Sub
 
Upvote 0

herryj

Member
Licensed User
Longtime User
Hi Klaus,
thanks for the answer such great help :)

One last question, I'm really being confuse on what to do about this.

i have an image and when I touch the image the code follows should work

I have a code for drag and drop and rotate as well, though when I combine all the codes its work abnormally.

Here is my code for drag and drop from one of this forum

B4X:
Private Sub Touch_Gestures(View As Object, PointerID As Int, Action As Int, X As Float, Y As Float) As Boolean
    Dim conta,xTemp,yTemp As Int
   
    Select Action
        Case G.ACTION_DOWN
        'CallSubDelayed(Main,"aas_Click")
        'rot.Initialize(obj,"PZ",Main)
            Touch.Add(PointerID)
            SingleClick = True
            If vBringToFront Then obj.BringToFront
            'TimePress = DateTime.Now
            '---Stores the position of View---
            X0 = X+obj.Left
            Y0 = Y+obj.Top
            X1 = obj.Left
            Y1 = obj.Top
            '---Display arrow fro---
            'DisplayArrow(obj)
            'rotate
            a.Initialize(File.DirAssets, "2.png")
            brect.Initialize(0,0,100%x,100%Y)
            c.Initialize(obj)
            c.DrawColor(Colors.Transparent)
            c.DrawBitmap(a, Null, brect)
'            TimerLC.Enabled = True
        Case G.ACTION_POINTER_DOWN
            Touch.Add(PointerID)
            If Touch.Size = 2 Then
                xTemp = G.GetX(Touch.Get(0))-G.GetX(Touch.Get(1))
                yTemp = G.GetY(Touch.Get(0))-G.GetY(Touch.Get(1))
                Hypotenuse = Sqrt(Power(xTemp,2)+Power(yTemp,2))/Density
            End If
            SingleClick = False
           
           
        Case G.ACTION_MOVE
            '---clears the first slight movement to the first pressure---
           
            If DateTime.Now-TimePress < 100 Then
                X0 = X+obj.Left
                Y0 = Y+obj.Top
                X1 = obj.Left
                Y1 = obj.Top
            End If
            If Touch.Size = 1 Then
                '---Move View---
                If vCanMove Then
                    xTemp = X1+G.GetX(Touch.Get(0))+obj.Left-X0
                    yTemp = Y1+G.GetY(Touch.Get(0))+obj.Top-Y0
                    If LimitBorder Then
                        xTemp = Min(Max(xTemp,0),Parent.Width-obj.Width)
                        yTemp = Min(Max(yTemp,0),Parent.Height-obj.Height)
                    Else If LimitArea Then
                        xTemp = Max(Min(xTemp,0),Parent.Width-obj.Width)
                        yTemp = Max(Min(yTemp,0),Parent.Height-obj.Height)
                    End If
                    obj.Left = xTemp
                    obj.Top = yTemp
                End If
                If Abs(vTouchData.X-X) > MovingTouch OR Abs(vTouchData.Y-Y) > MovingTouch Then TimerLC.Enabled = False
            End If
        Case G.ACTION_POINTER_UP
            '---Removes touches are no longer present on the screen---
            For conta = 0 To Touch.Size-1
                If Touch.Get(conta) = PointerID Then
                    Touch.RemoveAt(conta)
                    Exit
                End If
            Next
        Case G.ACTION_UP
            Touch.Clear
            TimerLC.Enabled = False
            If Abs(X+obj.Left-X0) < MovingTouch AND Abs(Y+obj.Top-Y0) < MovingTouch AND SingleClick Then
                If SubExists(vModule,vEventName&"_Click") Then
                    vTouchData.Tag = vTag
                    CallSub2(vModule,vEventName&"_Click",vTouchData)
                End If
            End If
    End Select
    Log(Action)
    'rot.Initialize(2,2,0)
    Return True
End Sub

and here is the rotate code


B4X:
Sub Knob_Touch(ViewTag As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
Log("ME - " & Action)
'Log("Last View Touched - " & LastObj)
    Select Action
        Case 0 ' DOWN
            'LastObj = ivRotatKnob
                OldAngle = getAngle(X, Y)
                ivRotatKnob.SetBackgroundImage(bp.Rotate(bmp, bmp.Width, bmp.Height, Angle, True))
'        Case 1 ' UP
'                ivRotatKnob.SetBackgroundImage(bp.Rotate(bmp,bmp.Width, bmp.Height, Angle, True))
        Case 2 ' MOVE
            CalcAngle(X, Y)
                ivRotatKnob.SetBackgroundImage(bp.Rotate(bmp, bmp.Width, bmp.Height, Angle, True))
    End Select
    Return True
End Sub

it really act abnormally, cause the event I used is same.
can I ask for any suggestion to combine all this code.

Thanks.
 
Upvote 0

herryj

Member
Licensed User
Longtime User
Hi klaus,

here is the project attached.

Upon initializing the rotateBitmap class module. There will be an image appear in the image you touch. and that image appeared will triggered the entire code of rotatebitmap.

I used canvas upon the appearance of the another image once I've touch the existing image. Though I'm still finding solution on how to used canvas on getting array object.

Thanks for the responses.
 

Attachments

  • tryr.zip
    23.7 KB · Views: 145
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I had a look at your program.
I get an error in line 21 r.Target = ivRotatKnob: Object should first be initialized in RotateBitmap when I touch an image.

It's difficult to help because I have never worked with the two classes you are using and I don't know what exactly your program is supposed to do.

You want to add a Touch event to the ImageViews, but when you touch an image you already have an event routine Touch_Gestures in DragMove. Can't you use this routine ?
 
Upvote 0
Top