How to change XML properties in running time? - android

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.

Related

Android - Custom Interpolator Class Reference In Xml

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.

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

Flip card transition between two activities Android

I am trying to implement the Flip card transition effect between two activities in my app by taking help from :
http://blog.robert-heim.de/karriere/android-startactivity-rotate-3d-animation-activityswitcher/.But I couldn't understand what areActivitySwitcher.java and Roatate3dAnimation.java in the above mentioned site. I have two activities in my app between whom I want to show this transition effect. They are MainActivity.java and About_us.java.Please explain the code with reference to my activities. I also searched on http://developer.android.com/training/animation/cardflip.html but in vain as it not for activities.
Thanks!
Disclaimer: This is not a an actual 3D Animation Flip. This merely imitates it, though some don't agree. Give it a try and if you like it, great! If you don't, my apologies.
In my early days of learning to code, I was having issues implementing a proper 3D Animation flip, so I went with this, it simulated it enough to satisfy my needs, but to each her/his own. To do what I did, first make sure that you have a folder called anim under your res folder for your project. Then you will need to create two xml files (I have mine called from_middle and to_middle). Below is the code for each of those:
from_middle.xml:
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0" android:toXScale="1.0"
android:pivotX="50%"
android:fromYScale="1.0" android:toYScale="1.0"
android:pivotY="50%"
android:duration="500" />
to_middle.xml:
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0" android:toXScale="0.0"
android:pivotX="50%"
android:fromYScale="1.0" android:toYScale="1.0"
android:pivotY="50%"
android:duration="500" />
After those are created, all you need is one line of code to run this animation, which you should be placed after you start your next activity:
overridePendingTransition(R.anim.from_middle, R.anim.to_middle);
Done! Now run it!
Based on user1672053's answer, you need to add a start offset to the from_middle.xml resource which is equal to the duration of the to_middle.xml animation resource.

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.

android animation starting new activities or dismissing an Activity

Is there a tutorial or a code example of various kinds of View Animations available in Android. Basically what I am trying to do is say if start a new Activity, I am trying to get that activity start out like zoom in till it fills the screen or Fade out when I am going to finish the activity. Is there a way i can do this ?
Any help will be greatly appreciated.
Thanks
-Chandu
So you need to use OverridePendingTransition from the activity: http://developer.android.com/reference/android/app/Activity.html
Some examples with Fade in/out can be found here: http://www.anddev.org/novice-tutorials-f8/splash-fade-activity-animations-overridependingtransition-t9464.html
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:zAdjustment="top"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="1000" />
Zoom:
<scale
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale=".1"
android:fromYScale=".1"
android:toXScale="1.0"
android:toYScale="1.0"
android:duration="2000" />

Categories

Resources