Android ScaleAnimation - android

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.

Related

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).

can only perform rotation of image view without holding it

Hi guys i uses the code below to perform rotation but it only rotates one time and goes back to its original position, how to let the image stay at the rotated position?
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:startOffset="0"
/>
xml file of my code
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate);
a.startAnimation(rotation);
use:
anim.setFillAfter(true);
If fillAfter is true, the transformation that this animation performed
will persist when it is finished. Defaults to false if not set. Note
that this applies to individual animations and when using an
AnimationSet to chain animations.

Android Animation Resources with constant velocity (zero acceleration)?

I have a anim resource which scrolls TextView (credits screen).
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="50000"
android:fromYDelta="200"
android:interpolator="#android:interpolator/linear"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toYDelta="-10000" />
</set>
I want to have constant transision speed with zero acceleration (same velocity over the whole animation period). I cannot find android:interpolator value that would allow that. Without android:interpolator value translate still uses some sort of acceleration profile.
Try using #android:anim/linear_interpolator for the interpolator.
Source containing list of available xml resources corresponding to common animators: http://developer.android.com/guide/topics/resources/animation-resource.html#Interpolators

Android fade out animation using Cosine function

I in my Android Honeycomb application there is a View which has a fade in and fade out animation. Here's most part of the animation code:
<scale
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="0%"
android:pivotY="0%"
android:duration="500" />
The way it's working right now the animation happens in a linear time but I want to do it according the Cosine function, that is from Cos(0) until Cos(0,5*pi) the fade out case.
Is it possible to do such thing?
Thank you.
You are free to create your own Interpolator. (See the interface description here.) This can then be applied with Animation.setInterpolator(Interpolator). Or, see this discussion for ways of applying custom Interpolators in XML.
If you don't want linear interpolation, you have choice between:
AccelerateDecelerateInterpolator, AccelerateInterpolator,
AnticipateInterpolator, AnticipateOvershootInterpolator,
BounceInterpolator, CycleInterpolator, DecelerateInterpolator,
LinearInterpolator, OvershootInterpolator
http://developer.android.com/reference/android/view/animation/Interpolator.html
So maybe what is the closest to a Cosine is:
AccelerateDecelerateInterpolator
An interpolator where the rate of change starts and ends slowly but
accelerates through the middle.

Creating animation at runtime in 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" />

Categories

Resources