B4J Question change the center of a ImageView

BeneBarros

Active Member
Licensed User
Longtime User
Is there any way to change the center of a ImageView to rotate?
Without changing the size.
 

moster67

Expert
Licensed User
Longtime User
For animations you can also use ViewPropertyAnimator.
Add the JavaObject library to your project and use as follows:

B4X:
Sub Animate(view As View, Degree As Float, Duration As Long)
    Dim jo As JavaObject = view
    jo.RunMethodJO("animate", Null).RunMethodJO("rotationY", Array(Degree)).RunMethodJO("setDuration", Array(Duration)).RunMethodJO("start", Null)
End Sub

Animate(yourview, 360.0F, 5000)

You can of course change the sub Animate adding other parameters.

To learn more, read about the APIs in the page linked above.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
In the past, when I explored this method I could only get the animation to work once, subsequent calls to the start method did not produce any animation (unless I changed orientation and forced a re-load of the activity, then it would animate again once). Is that normal behavior or was my test flawed?
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
I hadn't noticed that. You can resolve it by using the xBy form instead i.e. rotationYBy instead of rotationY.

In the first case, I guess it is related to start and end values and we are only indicating the "to end" value when we first run the animation. The second time we run it, the value is still 360 so no animation is shown. We would need to find a way to reset the start value....
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Is there any way to change the center of a ImageView to rotate?
Do you mean allow it to rotate around a point that is not the default point which is the centre of the imageview ?
ie, rotate around the top left corner for example.
 
Upvote 0

BeneBarros

Active Member
Licensed User
Longtime User
Do you mean allow it to rotate around a point that is not the default point which is the centre of the imageview ?
ie, rotate around the top left corner for example.
Exactly.
I want to change the center of rotation to another point x, y.

I will try to use "javafx.scene.effect.PerspectiveTransform".
recalculate new position to the corners, and apply the effect.
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
This sample code does what you want
B4X:
#if java
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.transform.Rotate;

private static Rotate rotation = new Rotate();

public static void setRotatePoint(ImageView n,Double angle,Double x,Double y){
    // clear transforms
    n.getTransforms().clear();
    //rotation
    rotation = new Rotate(angle,x,y);
    // add rotation to the imageview
    n.getTransforms().add(rotation);
}
#end if
Just call it like normal inline code
the params are :imageview, angle, xpos, ypos
so
B4X:
??.RunMethod("setRotatePoint",Array(imgv,30.0,50.0,75.0))
will rotate the imageview 30 degrees clockwise around the point x 50, y 75

Hope this helps.
 
Last edited:
Upvote 0

BeneBarros

Active Member
Licensed User
Longtime User
This sample code does what you want
B4X:
#if java
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.transform.Rotate;

private static Rotate rotation = new Rotate();

public static void setRotatePoint(ImageView n,Double angle,Double x,Double y){
    // clear transforms
    n.getTransforms().clear();
    //rotation
    rotation = new Rotate(angle,x,y);
    // add rotation to the imageview
    n.getTransforms().add(rotation);
}
#end if
Just call it like normal inline code
the params are :imageview, angle, xpos, ypos
so
B4X:
??.RunMethod("setRotatePoint",Array(30.0,50.0,75.0))
will rotate the imageview 30 degrees clockwise around the point x 50, y 75

Hope this helps.



would it be this?

B4X:
Dim imV as ImageView
imV.Initialize("imV")
MainForm.RootPane.AddNode(imV, 50, 50, -1, -1)
imV.SetImage(fx.LoadImage(File.DirAssets, "moto.png"))

asJavaObject(imV).RunMethod("setRotatePoint",Array(30.0,15.0,15.0))


Sub asJavaObject(j As JavaObject) As JavaObject
    Return j
End Sub
 
Upvote 0

BeneBarros

Active Member
Licensed User
Longtime User
Yes that's how I call it.
This returning an error

Error occurred on line: 31 (Main)
java.lang.RuntimeException: Method: setRotatePoint not found in: javafx.scene.image.ImageView
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at b4j.example.main._bt_action(main.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:226)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA$2.run(BA.java:165)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
 

Attachments

  • test.zip
    69.1 KB · Views: 198
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Sorry read what you posted again, not so tired now.
it should be
B4X:
asJavaObject(Me).RunMethod("setRotatePoint",Array(imv,30.0,15.0,15.0))
 
Upvote 0

BeneBarros

Active Member
Licensed User
Longtime User
Sorry read what you posted again, not so tired now.
it should be
B4X:
asJavaObject(Me).RunMethod("setRotatePoint",Array(imv,30.0,15.0,15.0))
Excellent .. now works perfectly
Thanks for your help

Its function working.



One more question ... Is it possible to turn B4A?
 
Last edited:
Upvote 0
Top