How to pass a variable from .java file to .xml file in android?
I am new to android and I want to simulate a car steering wheel. I found it easy to insert an Image(of steering wheel) and use tween animation to rotate it though XML.
I measured the rotation of the phone using sensor classes in .java file.
Now I nee开发者_如何学Pythond to pass this measured value from .java
file to .xml
(under res\anim) so as to rotate the image as per the roation of the phone.
Kindly help me by advicing a way to do this.
It's impossible to pass calculated value to xml resource, since resources are static.
But you can load the xml animation with
Animation anim = AnimationUtils.loadAnimation(context, id)
modify its properties
and apply it to the desired view with
desiredView.startAnimation(anim);
M1shk4 and Chet Haase, as told "It's impossible to pass calculated value to xml resource"
So to implement my requirement, we need to do it using code in . Java file.
The below was the code I got from Chet Haase:
public void turn()
{
RotateAnimation anim = new RotateAnimation(currentRotation, currentRotation + 30,Animation.RELATIVE_TO_SELF, 0.5f,nimation.RELATIVE_TO_SELF,0.5f);
currentRotation = (currentRotation + 30) % 360;
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(1000);
anim.setFillEnabled(true);
anim.setFillAfter(true);
turnImg.startAnimation(anim);
}
Its working fine: )
For more info
精彩评论