I have to do this in xml of an item of a recycler view (I'm using databinding and the viewholder pattern). Based on the value of the variable that is bound to the view, I need to rotate a drawable and set it as the src of an ImageView.
I've checked many options online but haven't found any, rotating the original xml drawable 45 degrees cuts of some parts of the shape which is a curved rectangle. This results in a shape that does not match the requirements.
I need suggestions on how to get this done from inside the xml or adapter without rewriting it to use getView.
Hello #staa99 try following code may it will help you.
ImageView imageView = findViewById(R.id.imageView);
RotateAnimation anim = new RotateAnimation(0, 45,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(1000);
anim.setFillEnabled(true);
anim.setFillAfter(true);
imageView.startAnimation(anim);
The solution I eventually used was to create four different vector variables based of the original with rotation info included. I then used a variable bound to the view to store the data that determines the vector drawable to display.
This solution is not good when you need a large or unknown number of possible rotations, or if the angles are calculated at runtime. However, I've not seen a way to do that from xml so you still need to do it from java/kotlin code
Related
I am using RotateAnimation to rotate the imageView to -30 degrees. And it works good. Now I change the position of the view by setting different values for X and Y. All good until now.
Now when I perform the same RotateAnimation again to this view after the position changed, it works weird (see the video link below) and I cannot find the reason. I have tried a lot but without success. It seems like related to the pivotX and pivotY after changing the position but I cannot find the actual cause.
Code for RotateAnimation
final RotateAnimation rotateAnim = new RotateAnimation(0f, -30f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 1f);
rotateAnim.setDuration(2000);
rotateAnim.setFillAfter(false);
ivFace.startAnimation(rotateAnim);
Code of changing the position of an imageview
ivFace.setX(300);
ivFace.setY(200);
Here I am posting the video also for better understanding
https://www.youtube.com/watch?v=uVWlrTNDKCM
Looks like the RotationAnimation is still rotating around the 'old' view position, probably because it is 'final' and thats why it is tied to the Views 'old' position the moment it is created. You could create a new RotationAnimation for every position change, which might solve the issue, although I would suggest using ViewPropertyAnimator and/or ObjectAnimator for both, translating your View to another position and rotating it.
simple question I cant find the answer anywhere, is there a way to rotate a spinner, without rotating screen or anything just create a spinner which is rotated 90 degrees?
Thanks in advance.
On API Level 11+, you are welcome to rotate any View via android:rotation in your layout XML or setRotation() in Java. Whether this will give you something that your users will like, I cannot say.
Programmatically you can try something like this -
RotateAnimation animation = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// Adding the time needed to rotate the image
animation.setDuration(250);
// Set the animation to stop after reaching the desired position. Without this it would return to the original state.
animation.setFillAfter(true);
//Then set the animation to your view
view.startAnimation(animation);
I want to rotate a simple imageview which has an elevation of 5dp.
animRotate=ObjectAnimator.ofFloat(imgProgress, "rotationY", 0, 360);
animRotate.setDuration(ANIM_DURATION);
animRotate.setRepeatCount(5);
animRotate.start();
The animation for the above code is smooth if the android:elevation value for the ImageView is not set in the layout file. But when i set the elevation, the animation becomes jerky.
Can someone please suggest a fix?
Maybe the reason is that you create and run animatuion at once. As docs say, it is better first to init your animation
//OnCreate
animRotate=ObjectAnimator.ofFloat(imgProgress, "rotationY", 0, 360);
animRotate.setDuration(ANIM_DURATION);
animRotate.setRepeatCount(5);
And then when it is time for animation to be fired run it
animRotate.start();
Also, consider reading about what PivotX and PivotY are, it may be useful.
Also, using default interpolator will give strange result for rotating 5 times - i think using simple linear interpolator is much better choice.
I want to animate my AnimatedVectorDrawable at runtime without using .xml files. Actually I'm using .xml files same way as documentation's samples shows:
AnimatedVectorDrawable
So, I have vector_drawable.xml contains<vector> with nested <group> and <path> which defines a shape.
For this vector I have animated_vector_drawable.xml contains <animated-vector> with android:animation assinged to <target>.
Last step is define an animation file rotation.xml using <objectAnimator> which is used by animated_vector_drawable.xml
Everything works fine, but the problem appears, when I need to create many different shapes (vectors) with many different or similar animations, because this generate many .xml files.
I can't include ready and prepared <vector> from one .xml file to another (some kind of <include> tag) so i need to copy the same code to another files. It is very annoying.
If I want to use the same animation for few <target> elements but each animation must have f.e. different delay or any property value (alpha, rotation, interpolator...) , I must create new .xml file contains <objectAnimator> with changed one property value instead of use the same, one file with changed property value. It's also annoying.
I discovered that I can use ObjectAnimator and set alpha & fillColor for AnimatedVectorDrawable but there is a problem when I want to change it's translateX, translateY, rotation or any other properties. Is there a way to do this without .xml. I just want to have access to <group>
Constructor that you used creates animation with absolute values (pixels).
TranslateAnimation in = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);
Try use Animation.RELATIVE_TO_PARENT to fit your needs.
I am programming a board game using the Android Platform. I am using Tween Animation, specifically the TranslateAnimation tool to create an animation that moves an ImageView object across the screen. I would like that ImageView to remain in its new position after the animation finishes. Please teach me how to do this?
This is a snippet of code that I am using to run the animation.
ImageView image = (ImageView) findViewById(R.id.ImageView)
Animation personal = new TranslateAnimation(0,100,0,100);
personal.setDuration(2000);
image.startAnimation(personal);
I am using Java to declare the animation instead of using an xml file because I don't know how to modify the fromXDelta, toXDelta, fromYDelta, toYDelta variables inside of the main class code.
animation.setfillEnabled(true);
animation.setFillAfter(true);
I think you should set the image layout params. To keep the position in the end; the first answer I tried it, it was not useful.