Define xml animation into Java - android

Is it possible to write slide_out_left animation using java?
Here is my animation xml.
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="300" android:fromXDelta="0%" android:toXDelta="-100%" />
</set>

yes it is possible:
view.animate().translationX(_amount_).setDuration(_time in ms_).start();
So, you will have to calculate translation distance by yourself.
Note: if you translate to 0 later, after some other translations - it will move to starting position.
To complete slide in effect try to use withStartAction also.

Related

Android - animate View object - specific XML <set>

I'm new to Android application programming, and using search i've so far solved all of my problems (stackoverflow being one of the best search hits !)
Now i have a problem i don't know how to solve :
When separate animations are required, i could do it in code, but using XML files in anim folder would be much better for me.
Is it possible to play an animation of a single <set> inside XML file ?
I mean is it possible to "compress" animations from mulitple XML files into one file and still use them individually ?
Example for normal animation of TextView widget :
TexView exampleTextView = (TextView) findViewById (R.id.example_textview);
Animation animatorSequence = AnimationUtils.loadAnimation (this, R.anim.example_animation);
exampleTextView.startAnimation (animatorSequence);
XML code with two example blocks that i would like to use separately
<?xml version="1.0" encoding="utf-8"?>
<!--first independant animation segment -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"
android:duration="1400" />
</set>
<!--second independant animation segment -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:duration="1400" />
</set>
You can't.
But you can divide them to multiple files, load the in run time and add them all to a single AnimationSet (You can also define the empty set itself in a XML file so you won't have to configure it programmatically).

Stop image-view by using animation on particular screen position

This is what I have done now I want to stop this animation on a particular screen position and I know during the animation it could not stop so please provide another way where I could implement this functionality in my app.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<translate
android:toYDelta="100%p"
android:duration="6000"
android:fillEnabled="false"
android:startOffset="5000"
android:fillAfter="false"/>
</set>
You have to give android:fromXDelta=""
android:toXDelta=""
android:fromYDelta=""
android:toYDelta=""

android; setting in/out animations on AdapterViewFlipper: Unknown animator name translate

I have some very simple animations that work perfectly with a ViewFlipper, but if I try setting them on an AdapterViewFlipper in/out, I get a runtime error "Unknown animator name translate". In looking at the respective methods on each, it looks like ViewFlipper expects a ViewAnimation, and AdapterViewFlipper expects an AdapterViewAnimation. The api's are otherwise the same, and both build without error. Here's the xml for one of the animations:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="800"/>
</set>
and I set it on the flipper like:
vf.setOutAnimation(this, R.anim.out_to_left);
I can guess this might mean that I can't use translate, type, but then how would I accomplish the same animation? Lame...
Found the answer here: https://stackoverflow.com/a/26197426/1534666
It appears that a ViewFlipperAdapter needs a objectAnimator, not a set.
Example left_in.xml, declared in animator folder
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="-1500"
android:valueTo="0"
android:duration="600"/>

Animate the activity entry

I need to start an activity in an animated way..can anyone help me?
Creating an intent and starting an activity in normal way will show new activity.I need to start it from one side,say left side..how to animate it near creating intent..
Use the following:
this.overridePendingTransition(R.anim.slidein_left, R.anim.slideout_right);
Where R.anim.* are Animation XML files in your /res/anim/ folder.
The following is an example of my slidein_left:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0%" />
</set>
And slideout_right:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
What this will do is slide both activities to the left, making the new activity slide in from the left, pushing the old activity out to the right.
Also, as stated by #njzk2, please attempt to make an effort yourself before asking questions, and provide us with things that you may have already tried.

Android - Animation offset - How to prevent the view from being drawn while the offset has not yet passed?

I am trying to start an animation AFTER 1 second. I have used the attribute "android:startOffset" in my XML file, but it does not work completely the way I expected. I was expecting the view to NOT EVEN BE DRAW in its initial position (that is, the position set in the attributes "fromXDelta" and "fromYDelta") before the offset I set has passed. Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially"
android:shareInterpolator="false" >
<translate
android:duration="2000"
android:startOffset="1000"
android:fromXDelta="-70%p"
android:fromYDelta="0%p"
android:interpolator="#android:anim/linear_interpolator"
android:toXDelta="+0%p"
android:toYDelta="0%p" />
</set>
If I try to move my view using the above animation, the view is drawn IMMEDIATELY at the position -70% of the screen. Then the one second passes and then, as expected, the animation kicks in and starts to move the view. However, I DO NOT want the view to be drawn at all before that 1 second!. How can I achieve this?
Thank you in advance.
UPDATE
I am calling the above XML just after a startActivity call (the *R.anim.animation_coming_in* below), like this:
startActivity(new Intent(this, ThankYouActivity.class));
overridePendingTransition(R.anim.animation_coming_in, R.anim.animation_coming_out);
You could try using a pair of alpha animations with very short duration so that the view is hidden until it's needed. Something like this:
<set ...>
<alpha
android:fromAlpha="0.0"
android:toAlpha="0.0"
android:duration="1"
android:startOffset="0" />
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1"
android:startOffset="1000" />
<translate
...
/>
</set>
Alternatively, you could implement this set of animations in code. Doing so would enable you to use a Handler to start the animation after a delay so that the view is hidden until the animation starts.

Categories

Resources