B4J Question Load image from file location and move it from (X,Y) to another (X,Y)

ElliotHC

Active Member
Licensed User
I want to load up a number of images from a file location e.g. C:/Images/Image1.png and move them on the screen from one position to another, then remove them.

I've tried to find a project which I could use to do this but I'm sure what I need is much more simple, modifying an existing project is probably over-complicating it.

Any help would be greatly appreciated.

Thanks
 

Daestrum

Expert
Licensed User
Longtime User
Simple example - just using 1 image
B4X:
Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Dim iv As ImageView
 Dim im As Image
End Sub
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 MainForm.Show
 im.Initialize("c:/temp","hannah.jpg") ' your image file
 iv.Initialize("iv")
 iv.SetImage(im)
 MainForm.RootPane.AddNode(iv,0,20,100,100)  
 iv.SetLayoutAnimated(5000,200.0,0,100,100) ' 5 seconds to travel 200 pixels right 
End Sub
Sub iv_AnimationCompleted
 iv.Visible = False ' when complete hide the imageview
End Sub
 
Upvote 0

ElliotHC

Active Member
Licensed User
This works really well guys thank you. How would I go about adding multiple different images and tracking their position?

Ultimately, what I'm trying to do is randomly select from my images and send them from one side of the screen to the other.

When they get to the other side (I need to know the position for this) I can use "iv.RemoveNodeFromParent".

I will start off with only perhaps 3 or 4 on the screen but as time goes on I want there to be loads going across.

I guess it's something like.

IF iv.position = 100,100 then

is there something like this?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I guess it's something like.

IF iv.position = 100,100 then
This is not a good idea.

Why do you need to know the position?

This code will move a view and remove it when it is done moving:
B4X:
Sub MoveAndRemoveView(View As B4XView, TargetX As Int, TargetY As Int, Duration As Int)
 View.SetLayoutAnimated(Duration, TargetX, TargetY, View.Width, View.Height)
 Sleep(Duration)
 View.RemoveViewFromParent
End Sub
Depends on jXUI library.
 
Upvote 0

ElliotHC

Active Member
Licensed User
I need to do this because I have an area where I need to hit a key and get points. As they pass in to the green zone, pressing a key will dissolve them and you get a point. Hit the key when they are outside the green zone and you lose a point.

So I need to know if there is an image in that area, and to specifically make that one disappear.

I want to keep it simple.

Currently its running nice and smoothly, my random images called from USB stick are moving across the screen, I have created 10 image views. Iv1 - iv10 but surely you can have an array and for next loops? Would be less code. But this is working.

How do I track iv1 for example? If I can do this then its easy, i can make iv1 disappear when the button is pressed and when its in the green zone.

This is an app for recycling waste, images are cans, paper etc..
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…