I want to use ScaleAnimation (programmatically not in xml) to change height to view from 0 to 60% of parent height. Width of view is constant and is 50px. View is empty only background color is set.
Can someone give me code for scaleAnim using ScaleAnimation from code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layContainer
>
<View
android:layout_width="50px"
android:layout_height="fill_parent"
android:id="#+id/viewContainer"
android:background:"#00f00"
/>
</LinearLayout>
ScaleAnimation scaleAnim = new ScaleAnimation(...);
view before and after animation
.Thanks
Here is a code snip to do exactly that.
public void scaleView(View v, float startScale, float endScale) {
Animation anim = new ScaleAnimation(
1f, 1f, // Start and end values for the X axis scaling
startScale, endScale, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
anim.setFillAfter(true); // Needed to keep the result of the animation
anim.setDuration(1000);
v.startAnimation(anim);
}
The ScaleAnimation constructor used here takes 8 args, 4 related to handling the X-scale which we don't care about (1f, 1f, ... Animation.RELATIVE_TO_SELF, 0f, ...).
The other 4 args are for the Y-scaling we do care about.
startScale, endScale - In your case, you'd use 0f, 0.6f.
Animation.RELATIVE_TO_SELF, 1f - This specifies where the shrinking of the view collapses to (referred to as the pivot in the documentation). Here, we set the float value to 1f because we want the animation to start growing the bar from the bottom. If we wanted it to grow downward from the top, we'd use 0f.
Finally, and equally important, is the call to anim.setFillAfter(true). If you want the result of the animation to stick around after the animation completes, you must run this on the animator before executing the animation.
So in your case, you can do something like this:
View v = findViewById(R.id.viewContainer);
scaleView(v, 0f, .6f);
In XML, this what I use for achieving the same result. May be this is more intuitive.
scale_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
scale_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
See the animation on the X axis is from 1.0 -> 1.0 which means you don't have any scaling up in that direction and stays at the full width while, on the Y axis you get 0.0 -> 1.0 scaling, as shown in the graphic in the question. Hope this helps someone.
Some might want to know the java code as we see one requested.
Place the animation files in anim folder and then load and set animation files something like.
Animation scaleDown = AnimationUtils.loadAnimation(youContext, R.anim.scale_down);
ImagView v = findViewById(R.id.your_image_view);
v.startAnimation(scaleDown);
try this code to create Scale animation without using xml
ScaleAnimation animation = new ScaleAnimation(fromXscale, toXscale, fromYscale, toYscale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Use this method (No need to xml file)
If you want scale to quarter(half x,half y)
view.animate().scaleX(0.5f).scaleY(0.5f)
If you want scale and move to bottom right
view.animate().scaleX(0.5f).scaleY(0.5f)
.translationY((view.height/4).toFloat()).translationX((view.width/4).toFloat())
If you want move to top use (-view.height/4) and for left (-view.width/4)
If you want do something after animation ends use withEndAction(Runnable runnable) function.
You can use some other property like alpha and rotation
Full code
view.animate()
.scaleX(0.5f).scaleY(0.5f)//scale to quarter(half x,half y)
.translationY((view.height/4).toFloat()).translationX((view.width/4).toFloat())// move to bottom / right
.alpha(0.5f) // make it less visible
.rotation(360f) // one round turns
.setDuration(1000) // all take 1 seconds
.withEndAction(new Runnable() {
#Override
public void run() {
//animation ended
}
});
Resize using helper methods and start-repeat-end handlers like this:
resize(
view1,
1.0f,
0.0f,
1.0f,
0.0f,
0.0f,
0.0f,
150,
null,
null,
null);
return null;
}
Helper methods:
/**
* Resize a view.
*/
public static void resize(
View view,
float fromX,
float toX,
float fromY,
float toY,
float pivotX,
float pivotY,
int duration) {
resize(
view,
fromX,
toX,
fromY,
toY,
pivotX,
pivotY,
duration,
null,
null,
null);
}
/**
* Resize a view with handlers.
*
* #param view A view to resize.
* #param fromX X scale at start.
* #param toX X scale at end.
* #param fromY Y scale at start.
* #param toY Y scale at end.
* #param pivotX Rotate angle at start.
* #param pivotY Rotate angle at end.
* #param duration Animation duration.
* #param start Actions on animation start. Otherwise NULL.
* #param repeat Actions on animation repeat. Otherwise NULL.
* #param end Actions on animation end. Otherwise NULL.
*/
public static void resize(
View view,
float fromX,
float toX,
float fromY,
float toY,
float pivotX,
float pivotY,
int duration,
Callable start,
Callable repeat,
Callable end) {
Animation animation;
animation =
new ScaleAnimation(
fromX,
toX,
fromY,
toY,
Animation.RELATIVE_TO_SELF,
pivotX,
Animation.RELATIVE_TO_SELF,
pivotY);
animation.setDuration(
duration);
animation.setInterpolator(
new AccelerateDecelerateInterpolator());
animation.setFillAfter(true);
view.startAnimation(
animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
if (start != null) {
try {
start.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onAnimationEnd(Animation animation) {
if (end != null) {
try {
end.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onAnimationRepeat(
Animation animation) {
if (repeat != null) {
try {
repeat.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
public void expand(final View v) {
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1, 1, 0, 0, 0);
scaleAnimation.setDuration(250);
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
v.startAnimation(scaleAnimation);
}
public void collapse(final View v) {
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1, 0, 1, 0, 0);
scaleAnimation.setDuration(250);
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
v.startAnimation(scaleAnimation);
}
Add this code on values anim
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="#android:integer/config_longAnimTime"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="#android:integer/config_longAnimTime"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"/>
</set>
call on styles.xml
<style name="DialogScale">
<item name="android:windowEnterAnimation">#anim/scale_in</item>
<item name="android:windowExitAnimation">#anim/scale_out</item>
</style>
In Java code:
set Onclick
public void onClick(View v) {
fab_onclick(R.style.DialogScale, "Scale" ,(Activity) context,getWindow().getDecorView().getRootView());
// Dialogs.fab_onclick(R.style.DialogScale, "Scale");
}
setup on method:
alertDialog.getWindow().getAttributes().windowAnimations = type;
Related
I am trying to slid a view from the bottom of the screen and collapse another view a little everything will fit correctly.
I have managed to do that with the attribute animateLayoutChanges and 2 simple xml animations slid_up.xml and slid_down.xml.
My problem is that the animation that happens to ViewPager from the attribute animateLayoutChanges isn't smooth.
Is there a way to fix that?
slid_up.xml
<translate
android:duration="1000"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
slid_down.xml
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="100%" />
</set>
P.S. I have tried to create custom animators as Height animators but it messes with the original height of the view.
HeightResizeAnimation
public class HeightResizeAnimation extends Animation {
private View mView;
private float mToHeight;
private float mFromHeight;
private int duration = 300;
public HeightResizeAnimation(View v, float offset) {
mView = v;
mToHeight = v.getHeight() + offset;
mFromHeight = v.getHeight();
setDuration(duration);
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float newHeight = (mToHeight - mFromHeight) * interpolatedTime + mFromHeight;
mView.getLayoutParams().height = (int) newHeight;
mView.requestLayout();
}
}
after the animation the height will no longer be as match_parent that was before the animation.
Update
Here is the animation that happens now
You can see that the fab animation to bottom isn't smooth also for the viewpager that the fab is child
It seems like you just want to show a snackbar + move the FAB along with it as it shows?
To do so you could use a combination of Snackbar and CoordinatorLayout from the Android Support Library: http://www.androidhive.info/2015/09/android-material-design-snackbar-example/
In your xml layout add the following in the View:
android:visibility="gone"
android:alpha="0"
android:id="#+id/text_view_liked"
I am asumming the view to be TextView.
On the click of ImageView liked animate the text_view_liked with the following:
final TextView textViewLiked = (TextView) findViewById(R.id.text_view_get_liked);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
PropertyValuesHolder pvhYTextViewLiked = PropertyValuesHolder
.ofFloat(View.Y,
textViewLiked.getBottom(),
(textViewLiked.getBottom() - textViewLiked.getHeight()));
PropertyValuesHolder pvhAlphaTextViewLiked = PropertyValuesHolder
.ofFloat(View.ALPHA, 0f, 1f);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(textViewLiked,
pvhYTextViewLiked,
pvhAlphaTextViewLiked);
ObjectAnimator objectAnimatorFab = ObjectAnimator.ofFloat(fab, View.Y, fab.getY(),
fab.getY() - textViewLiked.getHeight());
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator).with(objectAnimatorFab);
animatorSet.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
textViewLiked.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
PropertyValuesHolder pvhYTextViewLiked = PropertyValuesHolder
.ofFloat(View.Y,
textViewLiked.getTop(),
(textViewLiked.getTop() + textViewLiked.getHeight()));
PropertyValuesHolder pvhAlphaTextViewLiked = PropertyValuesHolder
.ofFloat(View.ALPHA, 1f, 0f);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(textViewLiked,
pvhYTextViewLiked,
pvhAlphaTextViewLiked);
ObjectAnimator objectAnimatorFab = ObjectAnimator.ofFloat(fab, View.Y, fab.getY(),
fab.getY() + textViewLiked.getHeight());
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator).with(objectAnimatorFab);
animatorSet.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
textViewLiked.setVisibility(View.GONE);
}
});
animatorSet.start();
}
}, 1000);
}
});
You can adjust the duration accordingly
new Handler is created just to automate the process of progress indicator is finished. You can just call on the finish of progress indicator.
After all the solution was to use the android:clipChildren="false"
As can be seen on the gif, the FAB is getting cropped while it is going down to the original position. So after I have used the android:clipChildren="false" on the parent of the FAB viewPager then the FAB isn't getting cropped anymore.
I have an application where i spawn a new enemy every 4 seconds. the enemy goes from right to left using anTranslateAnimation. This animation takes 7 seconds total.
The problem that I'm facing now is that when the animation is running, every time that i spawn a new enemy, the animation stops for the old one and starts animating the new enemy.
Is there any way to animate two different objects using the same TranslateAnimation?
Just in case, here is my animation
translate= new TranslateAnimation(
Animation.ABSOLUTE, (float) 1.0,
Animation.ABSOLUTE, (float) -4.0,
Animation.ABSOLUTE,0,
Animation.ABSOLUTE,0);
translate.setDuration(10000);
translate.setFillAfter(true);
//newIV is the enemy's ImageView
newIV.startAnimation(translate);
You can apply animation like this
make an animation xml in anim folder
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"
/>
</set>
Then code in class
final Animation RightToLeft = AnimationUtils.loadAnimation(context,
R.anim.right_to_left);
((ImageView)findViewById(R.id.yourImage))
.startAnimation(RightToLeft);
Hope it will help.
You can set a same animation to Different views in same position.try to use animationLisener
translate.setAnimationListener(new TranslateAnimation.AnimationListener() { #Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
animation.setFillAfter(false);
RelativeLayout.LayoutParams params =(RelativeLayout.LayoutParams)mLogo.getLayoutParams();
//Set your firest view layout or hide the previous newIV
//This ll avoid filckering
animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
animation.setDuration(1);
ImageView mLogo = new ImageView(activity);// Second enemy as u said
animation = new TranslateAnimation(
Animation.ABSOLUTE, (float) 1.0,
Animation.ABSOLUTE, (float) -4.0,
Animation.ABSOLUTE,0,
Animation.ABSOLUTE,0);
mLogo.startAnimation(animation);mLogo.setLayoutParams(params);
mLogo.clearAnimation();
mLogo.startAnimation(animation);
}
});
This ll animate When first enemy going to end create a new instance of animation and use.
I want to be able to do a text animation and change the size of the text in a TextView. I read that there are property animations in android but if someone knows a simple code that can do this for me or an example somewhere I will deeply appreciate it. Thank u in advance!
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="2.0"
android:toYScale="2.0"
android:duration="3000"></scale>
</set>
A function into an Activity:
private void RunAnimation()
{
Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
a.reset();
TextView tv = (TextView) findViewById(R.id.firstTextView);
tv.clearAnimation();
tv.startAnimation(a);
}
extracted and modified from here
Animation animation=new TranslateAnimation(0,480,0,0);
animation.setDuration(5000);
animation.setRepeatMode(Animation.RESTART);
animation.setRepeatCount(Animation.INFINITE);
text.startAnimation(animation);
// applying animation to textview object..
If you are using button event to show animation then put the code inside onClick() otherwise use override method onWindowFocusChanged(boolean hasFocus) to start animation
Use ValueAnimator class in the android
final float startSize = o; // Size in pixels
final float endSize = 30;
final int animationDuration = 1000; // Animation duration in ms
ValueAnimator animator = ValueAnimator.ofFloat(startSize, endSize);
animator.setDuration(animationDuration);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float animatedValue = (float) valueAnimator.getAnimatedValue();
tv.setTextSize(animatedValue);
}
});
animator.start();
refer this link ValueAnimator
Another solution is that apply scale animation on Textview or its parent layout
ScaleAnimation scaleAnimation = new ScaleAnimation(0.7f, 1.1f, 0.7f, 1.1f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(600);
viewZoom.startAnimation(scaleAnimation);
I'm using an xml defined animation to slide a view off the screen. The problem is, as soon as the animation completes it resets to its original position. I need to know how to fix this. Here's the xml:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="#android:anim/accelerate_interpolator">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500"/></set>
Here's the Java that I use to call it:
homeScrn = (View)findViewById(R.id.homescreen);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
//Set Click Listeners For Menu
btnHelp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LayoutInflater.from(getApplicationContext()).inflate(R.layout.help, (ViewGroup)findViewById(R.id.subpage), true);
homeScrn.startAnimation(slideLeftOut);
}
});
So basically what happens is I inflate a view underneath one. Then I animate the view on top off to the left. As soon as it gets off screen and the animation is finished it resets its position back.
Finally got a way to work around,the right way to do this is setFillAfter(true),
if you want to define your animation in xml then you should do some thing like this
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:duration="1000"/>
</set>
you can see that i have defined filterAfter="true" in the set tag,if you try to define it in translate tag it won't work,might be a bug in the framework!!
and then in the Code
Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_out);
someView.startAnimation(anim);
OR
TranslateAnimation animation = new TranslateAnimation(-90, 150, 0, 0);
animation.setFillAfter(true);
animation.setDuration(1800);
someView.startAnimation(animation);
then it will surely work!!
Now this is a bit tricky it seems like the view is actually move to the new position but actually the pixels of the view are moved,i.e your view is actually at its initial position but not visible,you can test it if have you some button or clickable view in your view(in my case in layout),to fix that you have to manually move your view/layout to the new position
public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
new TranslateAnimation(-90, 150, 0, 0);
now as we can see that our animation will starts from -90 x-axis to 150 x-axis
so what we do is set
someView.setAnimationListener(this);
and in
public void onAnimationEnd(Animation animation)
{
someView.layout(150, 0, someView.getWidth() + 150, someView.getHeight());
}
now let me explain public void layout (int left, int top, int right, int botton)
it moves your layout to new position first argument define the left,which we is 150,because translate animation has animated our view to 150 x-axis, top is 0 because we haven't animated y-axis,now in right we have done someView.getWidth() + 150 basically we get the width of our view and added 150 because we our left is now move to 150 x-axis to make the view width to its originall one, and bottom is equals to the height of our view.
I hope you people now understood the concept of translating, and still you have any questions you can ask right away in comment section,i feel pleasure to help :)
EDIT Don't use layout() method as it can be called by the framework when ever view is invalidated and your changes won't presist, use LayoutParams to set your layout parameters according to your requirement
The animations are working as expected. Just because you animated something does not mean you actually moved it. An animation only affects drawn pixels during the animation itself, not the configuration of the widgets.
You need to add an AnimationListener, and in the onAnimationEnd() method, do something that makes your move permanent (e.g., removes the view on top from its parent, marks the view on top as having visibility GONE).
I might be a bit late in replying, but I have the solution for this:
Just add android:fillAfter="true"
in your xml
You can use
fillAfter=true
fillEnabled=true
your View will'not reset to original position, but if you have some buttons or something else in your view , their position will not change.
You must use ObjectAnimator , it works from API 11 level . It changes View position automatic,
here is the example
ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(mContent_container, "translationX", startX, endX);
objectAnimator.setDuration(1000);
objectAnimator.start();
Thanks JUL for his answer
If your app not found object animator, change the API level from Project -> properties -> Android , than import android.animation.ObjectAnimator;
Regards Hayk
You need to add this command:
final Animation animSlideLeft = new TranslateAnimation(0, -80, 0,-350, 0, 0, 0, 0);
animSlideLeft.setFillAfter(true);
I went to the same question and solved it with setting the marginLeft at OnAnimationEnd()..
MarginLayoutParams params = (MarginLayoutParams) this.getLayoutParams();
params.setMargins(this.getWidth()*position, 0,0,0);
this.setLayoutParams(params);
This problem has bothered for more than a week.
And Muhammad's answer pointed me a strightforward way to solve it.
I used sever layout to implement side menus with animation.
"view.layout will not be persistent. You should use LayoutParams, which will be persistent."
Here's my solution:
(Use what kind of LayoutParameter is depended on your own parent's layout):
#Override
public void onAnimationEnd(Animation animation) {
FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(screenWidth, screenHeight);
layoutParams.setMargins((int) -(screenWidth * RATE), 0,
(int) (screenWidth - screenWidth * RATE), screenHeight);
for(View v:views) {
v.setLayoutParams(layoutParams);
//v.layout((int) -(screenWidth * RATE), 0, (int) (screenWidth - screenWidth * RATE), screenHeight);
v.clearAnimation();
}
}
fillAfter = true will do the job to transform view to new animation positon.
rotateAnimation.fillAfter = true;
If anybody interested to move arrow_up & arrow_down animations for a recycle view expands/collapse behavior.
Initial state: Collapse
call the method with a child visibility flag(View.VISIBLE/View.GONE).
Below is code belongs to animations.
fun setArrowImage(imageView: ImageView, childVisibility: Int) {
val angleRange = if (childVisibility == View.VISIBLE) Pair(0F, 180F) else
Pair(100f, 0F)
val rotateAnimation = RotateAnimation(
angleRange.first, angleRange.second,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
)
rotateAnimation.duration = 300
rotateAnimation.fillAfter = true;
imageView.startAnimation(rotateAnimation)
}
Use helper methods below to easily animate your view. Then you can animate and reset after completion like this:
// Animate the view:
fly(
view1,
(float) 0,
(float) 0,
(float) 0,
(float) 1000,
250,
null,
null,
() -> {
// Return the view to initial position on animation end:
fly(
view1,
(float) 0,
(float) 0,
(float) 0,
(float) 0,
0);
return null;
});
Helper methods:
/**
* Translation of a view.
*/
public static void fly(
View view,
float fromXDelta,
float toXDelta,
float fromYDelta,
float toYDelta,
int duration) {
fly(
view,
fromXDelta,
toXDelta,
fromYDelta,
toYDelta,
duration,
null,
null,
null);
}
/**
* Translation of a view with handlers.
*
* #param view A view to animate.
* #param fromXDelta Amount to shift by X axis for start position.
* #param toXDelta Amount to shift by X axis for end position.
* #param fromYDelta Amount to shift by Y axis for start position.
* #param toYDelta Amount to shift by Y axis for end position.
* #param duration Animation duration.
* #param start On animation start. Otherwise NULL.
* #param repeat On animation repeat. Otherwise NULL.
* #param end On animation end. Otherwise NULL.
*/
public static void fly(
View view,
float fromXDelta,
float toXDelta,
float fromYDelta,
float toYDelta,
int duration,
Callable start,
Callable repeat,
Callable end) {
Animation animation;
animation =
new TranslateAnimation(
fromXDelta,
toXDelta,
fromYDelta,
toYDelta);
animation.setDuration(
duration);
animation.setInterpolator(
new DecelerateInterpolator());
animation.setFillAfter(true);
view.startAnimation(
animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
if (start != null) {
try {
start.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onAnimationEnd(Animation animation) {
if (end != null) {
try {
end.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onAnimationRepeat(
Animation animation) {
if (repeat != null) {
try {
repeat.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
Nothing from answers worked with me; although #Muhammad Babar answer inspired me to get a solution:
He mentioned that we can layout the view to the new position upon animation end:
public void onAnimationEnd(Animation animation) {
someView.layout(150, 0, someView.getWidth() + 150, someView.getHeight());
}
Although this didn't work with me instead; I had to do that in a Handler with delayed Runnable using a delay value that is slightly less than the animation duration.
So, in my case the animation duration is 500 msec, and I used a handler on 450 mesec, so it gets triggered just before the animation ends by 50 msec.
Handler().postDelayed({
someView.layout(150, 0, someView.width + 150, someView.hight)
someView.requestLayout()
someView.forceLayout()
}, 450)
I have two TranslateAnimations on a TextView and I want them to execute one after other. However, by using the code below, only the second one is executed.
How can I solve this?
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f);
animation.setDuration(200);
wave.startAnimation(animation);
TranslateAnimation animation1 = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f);
animation1.setDuration(200);
wave.startAnimation(animation1);
Link them together with
Animation Set
AnimationSet as = new AnimationSet(true)
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f);
animation.setDuration(200);
as.addAnimation(animation);
TranslateAnimation animation1 = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f);
animation1.setDuration(200);
animation1.setStartOffset(200);
as.addAnimation(animation1);
wave.startAnimation(as);
EDIT: Andy Boots answer below is the better answer imo.
Just set your first one like this and it'll start the other one, once the animation finishes:
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
wave.startAnimation(animation1);
}
});
edit: The reason only your second animation is executed with your current code, is because it overrides the playing of the first animation (both actually are played, but you only see the latest one to start). If you do like I wrote, they will play sequentially instead of in parallel.
also you can do this by XML itself using android:startOffset attribute , and there is an examble:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="300"
android:fromXScale="0%"
android:fromYScale="0%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="100%"
android:toYScale="100%" />
<alpha
android:duration="300"
android:fromAlpha="0"
android:toAlpha=".5" />
<alpha
android:duration="300"
android:fromAlpha=".5"
android:startOffset="300"
android:toAlpha="1" />
</set>
There is one more approach to reach this goal which can be useful when you need to animate a lot of views one after another. You can use setStartOffset method to set a delay before animation begins. So, if you know, how much time will take for your first animation to end, you can set this as a delay for your second animation. This is an example where I animated six ImageButtons and six TextViews below them one after another:
public void animateButtons() {
// An array of buttons
int[] imageButtonIds = {R.id.searchButton, R.id.favoriteButton, R.id.responseButton, R.id.articleButton, R.id.resumeButton, R.id.subscribeButton};
// Array of textViews
int[] textViewIds = {R.id.searchTextView, R.id.favoriteTextView, R.id.responseTextView, R.id.articleTextView, R.id.resumeTextView, R.id.subscribeTextView};
int i = 1;
for (int viewId : imageButtonIds) {
ImageButton imageButton = (ImageButton) findViewById(viewId);
// Animation from a file fade.xml in folder res/anim
Animation fadeAnimation = AnimationUtils.loadAnimation(this, R.anim.fade);
// Delay for each animation is 100 ms bigger than for previous one
fadeAnimation.setStartOffset(i * 100);
imageButton.startAnimation(fadeAnimation);
// The same animation is for textViews
int textViewId = textViewIds[i-1];
TextView textView = (TextView) findViewById(textViewId);
textView.startAnimation(fadeAnimation);
i ++;
}
}
In my res/anim folder I have a file, called fade.xml with these contents:
<?xml version="1.0" encoding="utf-8"?>
<!-- Fade animation with 500 ms duration -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
Create an animation array and use method for creating AnimationSet.
Animation[] animations = {
getScaleAnimation(0.4f, 1.3f, 2000),
getScaleAnimation(1.3f, 1.0f, 500),
getScaleAnimation(0.4f, 1.3f, 1000),
getScaleAnimation(1.3f, 1.0f, 3000),
getScaleAnimation(0.4f, 1.3f, 500),
getScaleAnimation(1.3f, 1.0f, 1700),
getScaleAnimation(0.4f, 1.3f, 2100),
getScaleAnimation(1.3f, 1.0f, 3400)
};
AnimationSet animationSet = addAnimationAr(animations);
view.startAnimation(animationSet);
Method:
public static AnimationSet addAnimationAr(Animation[] animations) {
AnimationSet animationSet = new AnimationSet(false);
long totalAnimationDuration = 0;
for (int i = 0; i < animations.length; i++) {
Animation a = animations[i];
a.setStartOffset(totalAnimationDuration);
totalAnimationDuration += a.getDuration();
animationSet.addAnimation(a);
}
return animationSet;
}
If you use code, you can call
Animation.setStartOffset()
to delay the second animation.
if you use xml you can android:ordering="sequentially" property to make the two animations perform sequentially.
For simple animations, you can achieve this by using the animate() and withEndAction() functions, like so:
ImageView img = findViewById(R.id.imageView);
img.animate().rotation(180).alpha(0).setDuration(3000).withEndAction(new Runnable() {
#Override
public void run() {
ImageView img = findViewById(R.id.imageView);
img.animate().rotation(0).alpha(1).setDuration(2000);
}
});
AnimationDrawable
Running such animation is pretty straightforward in Android. In fact, there is a class in Android API just for this specific task called AnimationDrwable. AnimationDrawable is a set of images together.
Step1: Create AnimationDrwable
Step2: Load Animation Drawable into ImageView
step3: start the animation
Step 1:
Create an XML and add all images like this.
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="#drawable/cat_image_1" android:duration="200" />
<item android:drawable="#drawable/cat_image_2" android:duration="200" />
<item android:drawable="#drawable/cat_image_3" android:duration="200" />
</animation-list>
Step 2:
public void onCreate(Bundle savedInstanceState) {
//Step 2
ImageView myimage = (ImageView) findViewById(R.id.my_image);
myimage.setBackgroundResource(R.drawable.rocket_thrust);
//Step 3
AnimationDrawable catAnimation = (AnimationDrawable) rocketImage.getBackground();
catAnimation.start();
}