Creating animation at runtime in Android - android

i can use an XML file like below
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator">
<translate android:fromXDelta="100%" android:toXDelta="0%" android:duration="500" />
</set>
and load this xml from code like
AnimationUtils.loadAnimation(mContext, com.....R.anim.slidein)
everything works fine
But now for some reason i need to do that same thing without using XML
how do i create the same animation using just code
i tried something like this
TranslateAnimation in = new TranslateAnimation(1.0f,0.0f,0.0f,0.0f);
in.setInterpolator(AnimationUtils.loadInterpolator(mContext,
android.R.anim.accelerate_interpolator));
in.setDuration(500);
but didnt work, nothing animates
i think the problem is with percentages, in the xml i have specified percentages but in the TranslateAnimation constructor how do i specify percentages

You are right. Constructor that you used creates animation with absolute values (pixels). You need to use another constructor. Like this for example:
TranslateAnimation in = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);
Experiment with first and third parameter. Try use Animation.RELATIVE_TO_PARENT to fit your needs.

Try this (use percent 'p'):
<translate android:fromXDelta="100%p" android:toXDelta="0%p" android:duration="500" />

Related

Scale animation in Android degrades image quality

In Android 5.0 (API Level 21) and above, you can define vector
drawables, which scale without losing definition.
I use Vector Assets in Android Studio (API 28). Example: ic_mybutton.xml.
Vector drawables are displayed on Views in original quality: image, image, image.
ImageButton button = new ImageButton(context);
button.setImageResource(R.drawable.ic_mybutton);
Using this animation on Buttons and ImageButtons (or other Views):
Animation animation = new ScaleAnimation(
1.0f, 0.9f,
1.0f, 0.9f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1f);
animation.setFillAfter(true);
animation.setDuration(1000);
button.startAnimation(animation);
produces terrible image distortion: image, image, image.
What am I doing wrong?
The problem can be solved using the animator.
All other known libraries, such as Boom, RxAnimation and the like, degrade image quality when scaling.
anim_scale.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="scaleX"
android:repeatCount="1"
android:valueFrom="1.0"
android:valueTo="0.9"
android:valueType="floatType" />
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="scaleY"
android:repeatCount="1"
android:valueFrom="1.0"
android:valueTo="0.9"
android:valueType="floatType" />
</set>
animated_vector.xml:
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/ic_mybutton">
<target
android:animation="#animator/anim_scale"
android:name="anim_scale" />
</animated-vector>
Put your paths in <group> tag and set the pivot.
ic_mybutton.xml:
<group
android:name="anim_scale"
android:pivotX="177.5"
android:pivotY="37.5">
...
</group>
Thus (with slight changes), you can make an animation of a button click without losing vector image quality:
ImageButton button = findViewById(R.id.button_apply);
button.setImageResource(R.drawable.animated_vector);
button.setOnClickListener(v -> ((Animatable) button.getDrawable()).start());
P.S.
Following method does not work (losing quality):
ObjectAnimator animator = ObjectAnimator.ofFloat(button, "scale", 0.9f);
animator.setDuration(200);
animator.start();
And StateListAnimator class also degrades image quality.

Animation increase height

What I'm doing now is a sound wave and to do it I have created an animation that increase the height
please see this picture
so my problem is that the animation works and it increase the height however I wanted to make it reverse grow
Hope you can help me thank you
So my code is here
<scale
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="0.5"
android:duration="2000"
android:fillAfter="false"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
Animation animation = AnimationUtils.loadAnimation(context, R.anim.sound_wave);
view.setAnimation(animation);
view.animate();
animation.start();
I do not know of a way to solve your problem while continuing to use an animation resource. I would instead use ObjectAnimator.
Here's how to build your same animation using ObjectAnimator:
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.5f);
anim.setDuration(2000);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.start();
You also have to add an attribute to your View in whatever layout file defines it. In my case, I made my view 400dp tall, so I added this:
android:transformPivotY="400dp"
This will cause the scale animation to be anchored at the bottom of the view (since its pivot is equal to its height).

How can I control the animation speed in android

I have a music app and the music notations perform an animation from right to left, I want to control the animation speed, while they are performing.Can anyone please help me for that?
My animation layout is:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="500%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="10000"
android:fillAfter="true"
android:repeatCount="infinite"/>
And my code to perform the animation is:
Animation am=AnimationUtils.loadAnimation(this, R.anim.note);
music1.startAnimation(am);
set your android:duration with this help you can control your animation speed
android:duration="300"
change in the duration will control the speed of animation in your file.
android:duration="10000" // here you have to change the time in milliseconds.
you can implement onTouchListener to control your animation speed at the run time. inside touch listener event put coding to change your animation duration.
From official documentation:
ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
animation.setDuration(1000);
animation.start()
In your case:
music.setDuration(setValue);

Animation to move view with 200dp upside/downside

I want to make animation to move layout exactly 200 dp up for android application. So far I tryed many ways and cannot make it to work on multiple screen sizes.
This is xml file that im using:
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:fillAfter="true"
android:interpolator="#android:anim/linear_interpolator"
android:duration="10000"
android:fromXDelta="0%" android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="-400" />
</set>
I tried this also
final TranslateAnimation moveUpAnimation = new TranslateAnimation(0, 0, dpAsPixels, 0);
dissapearAnnimation.setDuration(20000);
The problem is that this layout has dinamic content so its lenght is changing, so % of its lenght to move up is not working for me . Thanks in advancce !
Take a look at ViewPropertyAnimator.
yourView.animate().translateY(getResources().getDimensionPixelSize(R.dimen.animationHeight)).setDuration(yourDurationInMs);
In your dimension resources:
<dimen name="animationHeight">-200dp</dimen>
What kind of layout are you using? To animation you sholdu use relative / frame layouts.

Android ScaleAnimation

I'd like to grow a view with each click using a ScaleAnimation. I've managed the effects of the animation persist after it has finished with the fillAfter, but the problem now is, the animation always starts from state 0 (as the View is defined in the XML) - on click the view resets and animates back to the state it was just after the first animation.
The animation is defined in an XML:
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1"
android:toXScale="1.5"
android:fromYScale="1"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:fillAfter="true"
/>
I solved the issue by not resorting to animation defined in the XML, but rather doing
anim = new ScaleAnimation(from, to, from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
and adjusting from/to each time I needed to expand it. I'm not so sure that's a good thing regarding performance, but it works nicely.
How exactly is the animation defined?
When defining ScaleAnimation with Java code, you can set fromX/fromY (look here) starting scaling factors, so I assume you can do the same with XML attributes.

Categories

Resources