Android - Custom Interpolator Class Reference In Xml - android

I have written my own custom interpolator class .
#Override
public float getInterpolation(float t) {
return t*t*t;
}
I am doing Fragment slide in and slide out animation .There I want to give the reference of this interpolator class in the xml.
But In the xml I am only able to add android provided interpolators.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="594"
android:startOffset="0"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toXDelta="0"
android:fillAfter="true"
android:duration="400"/>
</set>
So basically I wanted to know how to give reference of Custom Interpolator class in Xml.

Create your custom interpolator and save it in a XML file in the res/anim folder of your project. For example
XML file saved at res/anim/my_interpolator.xml:
Then where ever you need to use it access it like given below
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#anim/my_interpolator"
android:fromXScale="1.0"
android:toXScale="3.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700" />
In the android:interpolator section of your XML file change your default #android/ to #anim/ and choose your custom interpolator.
This works for me. Let me know if you have any trouble. Thanks.

Related

What is the default interpolator if android:interpolator is unspecified

In Android, I know we can define animations via an XML file.
For example, scale_button_up.xml might look something like:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillBefore="true"
android:fillAfter="true"
android:fillEnabled="true">
<scale
android:duration="5000"
android:fromXScale="0.25"
android:fromYScale="0.25"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.75"
android:toYScale="0.75"/>
</set>
I was wondering what the default behavior is for Android version 21+
android:interpolator="#android:anim/linear_interpolator"
If the above were not specified. I can't seem to find the relevant documentation.
All *Animation classes are subclasses of Animation, and it handles setting the interpolator specified in XML attributes in its constructor. If there isn't one specified, the default AccelerateDecelerateInterpolator is set in its ensureInterpolator() method.
/**
* Gurantees that this animation has an interpolator. Will use
* a AccelerateDecelerateInterpolator is nothing else was specified.
*/
protected void ensureInterpolator() {
if (mInterpolator == null) {
mInterpolator = new AccelerateDecelerateInterpolator();
}
}

Is it possible to inherit an anim xml resource from another and overwrite attributes?

I have these two files in res/anim folder:
my_anim.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:duration="3000"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="360" />
</set>
my_anim_faster.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:duration="1000"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="360" />
</set>
Both are the same animation, only the speed (android:duration) changes. Is there a way to make this code shorter? For instance, by my_anim_faster inheriting from my_anim and overwrite android:duration attribute or something similar.
Thanks.
A little briefing: XML resource cannot be inherited. The xml approach gives you the ability to create easy and fast resources like drawables, animations and layouts, but there is no way to inherit them. Google added fragments that allows you to extend and reuse layout functionality in some way but this is not an inheritance. In drawable/animation resources you can reuse other resources but you cannot extend them. So if you want to reuse some logic try to design your resources such a way that you can reuse them in another ones.
Now to your case: No, there is no way to do this via xml. Using xml you can only describe your resource. You can change the duration using java code programmatically.
Animation myAnimation = ...;
...
myAnimation.setDuration(1000);
As far as i'm aware you can't inherit other animations when creating an animation in xml like how you can "include" a layout when creating a layout.
Your best bet would to create the animation programmatically (in an AnimationUtils class for example) and when you call it supply the duration as an argument. e.g.
MyAnim myAnim = new MyAnim(1000);

How to change XML properties in running time?

I am trying to implementing an pending transition animation for my app.
I have over rided the pending transition with function
overridePendingTransition(R.anim.incomming, R.anim.outgoing);
and in the outgoing.xml file, it is like:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="1.0"
android:fromYScale="0"
android:toXScale="1.0"
android:toYScale="1.0"
android:startOffset="300"
android:duration="300" />
</set>
and I want to change android:pivotY in running time, so how can I change this value in the java code?
I know some thing about SharedPreference, but the variables in the xml files are different from SharedPreference. So what I should do?
In this case, it seems to be impossible to change the value of the attribute at run time in the xml file. Maybe the straightforward way is to create other xml files and decide which to use as animation resources at run time.

Multiple animations on 1 imageview android

I have 2 animations which are already working,
i want to fade my train + tween my train on the same time.
If I execute 1 of these lines it works.
But if I try to execute both it, only 1 will work..
I really can't find a solution here.
Maybe you can help?
final ImageView mytrain = (ImageView) findViewById(R.id.train);
final Animation traintween = AnimationUtils.loadAnimation(this,R.anim.treinanimation);
final Animation trainfade = AnimationUtils.loadAnimation(this,R.anim.trainfade);
mytrain.startAnimation(trainfade);
mytrain.startAnimation(trainntween);
I want mytrain to execute both animations..
Thank you for the help!
Use the AnimationSet class:
AnimationSet s = new AnimationSet(false);//false means don't share interpolators
s.addAnimation(traintween);
s.addAnimation(trainfad);
mytrain.startAnimation(s);
You need to use an AnimationSet, check out the docs. Just call addAnimation() for each Animation you want to play.
Can be done programatically using AnimatorSet class of android :
final AnimatorSet mAnimatorSet = new AnimatorSet();
mAnimatorSet.playTogether(
ObjectAnimator.ofFloat(img_view,"scaleX",1,0.9f,0.9f,1.1f,1.1f,1),
ObjectAnimator.ofFloat(img_view,"scaleY",1,0.9f,0.9f,1.1f,1.1f,1),
ObjectAnimator.ofFloat(img_view,"rotation",0 ,-3 , -3, 3, -3, 3, -3,3,-3,3,-3,0)
);
//define any animation you want,like above
mAnimatorSet.setDuration(2000); //set duration for animations
mAnimatorSet.start();
this example will start all the 3 animation on the target view(imgView) at same time ,you can also use playSequentially .....
For complete example check this out..
here is the example of all animation in a single xml file...
this will help you but first you should read the docs of AnimationSet
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1.0" android:toXScale="3.0"
android:fromYScale="1.0" android:toYScale="3.0" android:pivotX="50%"
android:pivotY="50%" android:duration="5000" />
<set>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.2" android:toAlpha="1.0" android:duration="3000" />
<rotate android:fromDegrees="0" android:toDegrees="-360"
android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%"
android:startOffset="700" android:duration="4000" />
<!-- <translate android:fromXDelta="0%" android:toXDelta="0%" -->
<!-- android:fromYDelta="0%" android:toYDelta="100%" android:duration="3000" -->
</set>
</set>
you also can use the ImageSwitcher, i think this is better then AnimationSet in your case

Avoid animation automatically "unwinding"?

I've a simple view animation, but I can't see to get "rid" of the animation "unwinding" (and I can't seem to find a solution online).
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:shareInterpolator="true" >
<scale
android:duration="250"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:toXScale="1.1"
android:toYScale="1.1" />
</set>
What this does is, simply, inflates the View by 10% proportionally, from the middle.
But, when it executes, it inflates and, when it reaches the end, it deflates back. I want to avoid that -- the "unwinding" effect -- when it scales back from 110% to 100%.
How can that be done?
Edit:
I'm starting it simply with this:
Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.<name>);
v.startAnimation(animation1);
The correct answer is found here: How can I animate a view in Android and have it stay in the new position/size?
It is putting this:
android:fillAfter="true"
android:fillEnabled="true"
inside the <set tag.

Categories

Resources