I have an animation in my code.
But I want when I click Button my ImageView return to original place(reset animation)
I have a method for that.
My code is :
private void returnToOriginalRotationState() {
RotateAnimation animation = new RotateAnimation(0.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration((long) 2*1000);
animation.setFillAfter(true);
animation.setFillEnabled(true);
imgHeade.startAnimation(animation);
}
It works as well
But
animation.setDuration((long) 2*1000);
not working !!!
try this for clear animation:
imgHeade.clearAnimation();
Related
I wonder how to animate current Activity, not transition between activities. For example, I want to shift the activity slightly to the left and then move it back, so that it looks like a little shake effect. Any ideas? Thanks
Finally I have found a solution:
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -0.05f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
animation.setDuration(100);
animation.setInterpolator(new AccelerateInterpolator());
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.REVERSE);
View viewActivity = findViewById(android.R.id.content);
if (viewActivity != null)
viewActivity.startAnimation(animation);
you need to create animation instance like below
Animation startAnimation = AnimationUtils.loadAnimation(context, R.anim.shaking_animation);
then u need to start that animation with your parent layout
parent_layout.startAnimation(startAnimation);
you can get shake animation here shaking / wobble view animation in android
finally you can clear animation like below
parent_layout.clearAnimation();
The basic idea is to get access to the root layout of the activity and then do some animations on it, e.g. if you use DataBindung it is just like this (but not the best idea for the kind of animation you want):
binding.getRoot().animate().rotation(-5).setDuration(500).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
binding.getRoot().animate().rotation(5).setDuration(1000).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
binding.getRoot().animate().rotation(0).setDuration(500);
}
});
}
});
I have an arrow drawable that I simply want to rotate without it moving either in the x or y direction.
To explain further, consider a ball spinning about a fixed point or the circular Android progressbar.
Additionally, the drawable will rotate -180 degree when clicked and 180 degress when clicked again.
Please, is there anyway this can be done programmatically?
Update
From Jason Wihardja's answer, I was able to achieve this:
#Override
public void onClick(View v) {
if (v.getId() == imageButton.getId()) {
int visibilty =newsBody.getVisibility();
if (visibilty == View.VISIBLE) {
Animation animation = new RotateAnimation(180.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new DecelerateInterpolator());
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setDuration(300);
imageButton.startAnimation(animation);
newsBody.setVisibility(View.GONE);
} else {
Animation animation = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new DecelerateInterpolator());
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setDuration(300);
imageButton.startAnimation(animation);
newsBody.setVisibility(View.VISIBLE);
}
}
}
But is there anyway I can avoid repeating the animation building? Like just build it once and toggle the degrees in each click.
You could use RotateAnimation class to do that. Here's an example on how to do that
RotateAnimation rotateAnimation = new RotateAnimation(180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new DecelerateInterpolator());
rotateAnimation.setRepeatCount(0);
rotateAnimation.setDuration(animationDuration);
rotateAnimation.setFillAfter(true);
arrowImageView.startAnimation(rotateAnimation);
To rotate in a different direction, just flip the angle parameters.
I want multiple animations to be played on the same ImageView. I'm using an animation set but it never fades in. But it does rotate. Can somebody tell me what I'm missing ?
AnimationSet s = new AnimationSet(false);//false mean dont share interpolators
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
iv.setAnimation(fadeIn);
iv.startAnimation(fadeIn);
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(800);
s.addAnimation(fadeIn);
s.addAnimation(anim);
iv.startAnimation(s);
You are missing:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
fadeIn.setFillEnabled(true); // to apply setFillBefore and setFillAfter
fadeIn.setFillBefore(true); // it means that at start of animation you set alpha="0"
fadeIn.setFillAfter(false);
iv.setAnimation(fadeIn);
iv.startAnimation(fadeIn);
Also, in your styles you must remove alpha="0"
AlphaAnimation is working on your image as it is. So if you set alpha="0" it works between this 0. If alpha="1" or without it (when style default is alpha="1"), it works between 0-1.
I have a custom slide-out menu implementation (not using any library) in which I slide my layout towards right and show menus on the left (like facebook, google+ app does). After the menu is shown I fade the right layout by giving some alpha value as shown in the code below:
<FrameLayout
android:id="#+id/fl_mask"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0.7"
android:background="#color/black">
</FrameLayout>
But this all happens when my menu is shown. What I want is, as the layout slides towards right, I want to darken it. Farther the layout from left edge, darker the layout. Moreover, I use following code for layout animation which slides my layout towards right.
public Animation SlidingAnimation(int animateDuration, boolean slideLeftToright, final boolean executeOnAnimEndCode) {
Animation animation = null;
AnimationSet set = new AnimationSet(true);
if (slideLeftToright) {
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
} else {
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
}
animation.setDuration(animateDuration);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (executeOnAnimEndCode) {
... // Some piece of code
}
}
});
set.addAnimation(animation);
return animation;
}
How can I fade/darken my layout as it slides towards right?
In your
public void onAnimationEnd(Animation animation)
{
if (executeOnAnimEndCode)
{
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);
alphaAnimation.setFillAfter(true);
someLayout.startAnimation(alphaAnimation);
}
}
});
or you can also declare animation as xml and then use it....
suppose i have three text as heading.Any how you can take textview or anything.one text on left,one in center and one in right side.so i want an animation when i will slide the screen left then left text will be gone,center text will come to left and right text will come to center.Same thing for right slide.Can anyone suggest how can i achieve this type of animation.Plz help.
You can try to create an animation variable :
private Animation leftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
then
set this action to your frame:
public void onLeftArrowClick(View v) {
frame.setAnimation(moveComingSoonToBottomAnim); // your Frame variable
animVariable.startNow();// your anime variable
frame.setVisibility(View.VISIBLE);
rightFrame.setVisibility(View.GONE); // hide your right text view
}