i have a button that i animating when touched. the animation is working but after the animation is compelete, i am calling setAnimation(null) to reset the button, but the button is not getting back to its default position.
here is the code:
ok_btn_id.setOnTouchListener(new OnSwipeTouchListener(BarCodeReaderActivity.this) {
public void onSwipeTop() {
}
public void onSwipeRight() {
slidefromLeftToRight(ok_btn_id);
}
public void onSwipeLeft() {
slidefromRightToLeft(ok_btn_id);
}
public void onSwipeBottom() {
}
});
public void slidefromRightToLeft(final View view)
{
TranslateAnimation animate;
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (view.getHeight() == 0) {
animate = new TranslateAnimation(metrics.widthPixels/2,
0, 0, 0);
} else {
animate = new TranslateAnimation(0,-viewGroup.getWidth(), 0, 0); // View for animation
}
animate.setDuration(500);
animate.setFillAfter(false);
view.startAnimation(animate);
view.postDelayed(new Runnable() {
#Override
public void run() {
view.setAnimation(null);
view.setVisibility(View.VISIBLE);
}
}, 500);
}
Please try this:
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
aView.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
button.clearAnimation();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Hope this will help you.
If you want to reset the position try Animation.reset();
animate.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation)
{
animation.reset();
}
}
Related
I am trying the following code in Andorid for animations
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay);
rel = (LinearLayout) findViewById(R.id.rel);
rel.setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_background));
animation = new AlphaAnimation(1, 0);
animation.setDuration(500);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(3);
rel.startAnimation(animation);
animation1 = new AlphaAnimation(1, 0);
animation1.setDuration(1500);
animation1.setInterpolator(new LinearInterpolator());
animation1.setRepeatCount(3);
animationchala();
}
public void animationchala(){
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
animation.setAnimationListener(null);
rel.startAnimation(animation1);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation1) {
}
public void onAnimationEnd(Animation animation1) {
animation1.setAnimationListener(null);
rel.startAnimation(animation);
}
#Override
public void onAnimationRepeat(Animation animation1) {
}
});
}
the code works for the first time.
when animation ends animation1 is started , then animation1 ends and animation is started again, but now when animation1 has to start again it stops, how can I repeat the animations one after the other infinitely
The problem is in your case that you are setting your animationListeners to null when you started new animation. This makes them stop after you started animation. Just re-arrange your codes like this;
public void animationchala(){
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
rel.startAnimation(animation1);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation1) {
}
public void onAnimationEnd(Animation animation1) {
rel.startAnimation(animation);
}
#Override
public void onAnimationRepeat(Animation animation1) {
}
});
}
I can't get the circular reveal animation to work.
I think I checked the most obvious things:
It starts, width and height are > 0 and it is visible, no Exception..
I load some data from the internet and display it in the view(fab)
The animation should only play after the download finishes.
TmdbHelper helper = new TmdbHelper();
helper.getMovieById(id, "en", new TmdbHelper.ResultListener() {
#Override
public void onResultReceived(JSONObject result) {
// called when finished downloading
try {
String rating = result.getString("vote_average");
AnimationHelper.circularReveal(fab, 500, 0);
fab.setText(rating);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
AnimationHelper:
public static void circularReveal(final View view, final long duration, long startDelay) {
// get the center for the clipping circle
int cx = (view.getLeft() + view.getRight()) / 2;
int cy = (view.getTop() + view.getBottom()) / 2;
// get the final radius for the clipping circle
int finalRadius = Math.max(view.getWidth(), view.getHeight());
// create the animator for this view (the start radius is zero)
Animator anim =
ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
anim.setDuration(duration);
anim.setStartDelay(startDelay);
anim.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
view.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {}
});
// make the view visible and start the animation
anim.start();
}
I use the circular reveal animation in other parts like this to make sure the view is attached, and it works:
headerContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
headerContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
AnimationHelper.circularReveal(headerContainer, 500, 200);
}
});
Perhaps you should erase this line inside your onResultRecieved():
fab.setVisibility(View.VISIBLE);
My assumption is that your circular reveal method is working just fine. It is because of you have made the FAB visible before the animation even begin, you can't see it in action.
As an addition, those lines you've shown which is working doesn't have fab.setVisibility(View.VISIBLE) called anywhere in it.
1st Approach:
Try Transition Listener.
getWindow().getSharedElementExitTransition().addListener(new Transition.TransitionListener() {
#Override
public void onTransitionStart(Transition transition) {
}
#Override
public void onTransitionEnd(Transition transition) {
}
#Override
public void onTransitionCancel(Transition transition) {
}
#Override
public void onTransitionPause(Transition transition) {
}
#Override
public void onTransitionResume(Transition transition) {
}
});
2nd Approach: Try setting start delay and listener to the reveal animation and when animation starts then set the view visible
if (Build.VERSION.SDK_INT >= 21) {
Animator anim = ViewAnimationUtils.createCircularReveal(viewRoot, cx, cy, 0, finalRadius);
anim.setStartDelay(300);
anim.setDuration(1000);
anim.setInterpolator(new DecelerateInterpolator());
anim.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
viewRoot.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();
I'm trying to animate buttons with fade in animation using AnimatorSet
Button fades in > Click button > Remaining buttons fade out
So in order to do this, I want to set the onClickListner after the animation is completed, but that doesn't seem to work. Clicking a button in the middle of the animation triggers the onClick action:
setQuestion = new AnimatorSet();
setQuestion.playSequentially(fadeinAnimationQ,fadeinAnimation1,fadeinAnimation2,fadeinAnimation3,fadeinAnimation4,fadeinAnimation5);
setQuestion.start();
This is the method that checks if the animation has finished.
private void checkAnimation() {
while (true) {
// Check if animation has ended
if (setQuestion.isRunning() == false) {
assignListners();
break;
}
}
}
You can set an AnimatorListener on fadeinAnimation5.
This will give you an onAnimationEnd callback.
fadeinAnimation5.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// ...
}
#Override
public void onAnimationRepeat(Animator animation) {
// ...
}
#Override
public void onAnimationEnd(Animator animation) {
// ...
}
#Override
public void onAnimationCancel(Animator animation) {
// ...
}
});
Or, as suggested by slott use an AnimatorListenerAdapter
fadeinAnimation5.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
// ...
}
}
I was having a similar problem and here is how I solved it:
private void crossFadeAnimation(final View fadeInTarget, final View fadeOutTarget, long duration){
AnimatorSet mAnimationSet = new AnimatorSet();
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(fadeOutTarget, View.ALPHA, 1f, 0f);
fadeOut.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
fadeOutTarget.setVisibility(View.GONE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
fadeOut.setInterpolator(new LinearInterpolator());
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(fadeInTarget, View.ALPHA, 0f, 1f);
fadeIn.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
fadeInTarget.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
});
fadeIn.setInterpolator(new LinearInterpolator());
mAnimationSet.setDuration(duration);
mAnimationSet.playTogether(fadeOut, fadeIn);
mAnimationSet.start();
}
You can actually set a listener to the AnimatorSet directly since AnimatorSet inherits from Animator. Here's some code:
import android.animation.Animator;
AnimatorSet setQuestion = new AnimatorSet();
setQuestion.playSequentially(fadeinAnimationQ,fadeinAnimation1,fadeinAnimation2,fadeinAnimation3,fadeinAnimation4,fadeinAnimation5);
setQuestion.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
// !! turn on your onClickListener here !!
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
setQuestion.start();
This is the code for swapping based on finger movement. I am able to swap but the images are not replacing the positions. After swapping the swapped image has to take the new position and animation has to set again for that. For this code it is redirecting to the old position and taking animation effect second time.
if (imageendX > imagestartX && imageendY < imagestartY) {
if (imageendX - imagestartX < 60) {
} else {
TranslateAnimation tr = new TranslateAnimation(0,
90, 0, 0);
tr.setDuration(1000);
// tr.setFillAfter(true);
// tr.setFillEnabled(true);
TranslateAnimation tr2 = new TranslateAnimation(0, -90, 0, 0);
tr2.setDuration(1000);
// tr2.setFillAfter(true);
// tr2.setFillEnabled(true);
currentView.startAnimation(tr);
RightView.startAnimation(tr2);
tr.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tr2.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
} else if (imageendY < -10) {
TranslateAnimation tr = new TranslateAnimation(0, 0, 0, -90);
tr.setDuration(1000);
// tr.setFillAfter(true);
TranslateAnimation tr2 = new TranslateAnimation(0, 0, 0,90);
tr2.setDuration(1000);
// tr2.setFillAfter(true);
currentView.startAnimation(tr);
TopView.startAnimation(tr2);
tr2.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
android.widget.RelativeLayout.LayoutParams pr = (android.widget.RelativeLayout.LayoutParams) TopView
.getLayoutParams();
// pr.topMargin += 90;
pr.topMargin += 90;
TopView.setLayoutParams(pr);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tr.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
android.widget.RelativeLayout.LayoutParams pr = (android.widget.RelativeLayout.LayoutParams) currentView
.getLayoutParams();
// pr.topMargin += 90;
pr.bottomMargin += 90;
currentView.setLayoutParams(pr);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
} else if (imageendY > imagestartY && imageendX < imagestartX) {
if (imageendY - imagestartY < 60) {
} else {
TranslateAnimation tr = new TranslateAnimation(0, 0, 0, RM);
tr.setDuration(1000);
// tr.setFillAfter(true);
TranslateAnimation tr2 = new TranslateAnimation(0, 0, 0, -RM);
tr2.setDuration(1000);
// tr2.setFillAfter(true);
currentView.startAnimation(tr);
BottomView.startAnimation(tr2);
tr2.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tr.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
} else if (imagestartX >= imageendX) {
if (imagestartX - imageendX < 90) {
} else {
System.out.println("right to left");
TranslateAnimation tr = new TranslateAnimation(0, RM, 0, 0);
tr.setDuration(1000);
tr.setFillAfter(true);
TranslateAnimation tr2 = new TranslateAnimation(0, -RM, 0, 0);
tr2.setDuration(1000);
tr2.setFillAfter(true);
LeftView.startAnimation(tr);
currentView.startAnimation(tr2);
tr2.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tr.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
// }
}
Using View Animation the views will be in Original Position after the animation, So according to your requirement use Property Animation the views will be in Animated positons after the animation but the only downside is Property Animation supports from API 11
No Problem...Use NineOldAndroids to overcome that Problem..
Sample Code with two image views
Animation between imageOne and imageTwo
ObjectAnimator imageOneAnimator = ObjectAnimator.ofFloat(
imageOne, "X", imageOne.getX(), imageTwo.getX());
ObjectAnimator imageTwoAnimator = ObjectAnimator.ofFloat(
imageTwo, "X", imageTwo.getX(), imageOne.getX());
imageOneAnimator.setDuration(1000);
imageTwoAnimator.setDuration(1000);
AnimatorSet set = new AnimatorSet();
set.playTogether(imageOneAnimator, imageTwoAnimator);
set.start();
Animation between imageThree and imageOne
ObjectAnimator imageOneYAnimator = ObjectAnimator.ofFloat(
imageOne, "Y", imageOne.getY(), imageThree.getY());
ObjectAnimator imageThreeYAnimator = ObjectAnimator.ofFloat(
imageThree, "Y", imageThree.getY(), imageOne.getY());
imageOneYAnimator.setDuration(1000);
imageThreeYAnimator.setDuration(1000);
AnimatorSet setY = new AnimatorSet();
setY.setStartDelay(1000);
setY.playTogether(imageOneYAnimator, imageThreeYAnimator);
setY.start();
You can start the animation in your touch listener according to your calculations
private void kartyafeleanim(String idx1, String idx2) {
Animation anim1=AnimationUtils.loadAnimation(mycontext, R.anim.scalable_anim);
Animation anim2=AnimationUtils.loadAnimation(mycontext, R.anim.scalable_anim);
try {
for (int i=0; i<6; i++) {
LinearLayout LL = (LinearLayout) myLinearLayout.getChildAt(i);
for (int j=0; j<LL.getChildCount(); j++) {
if (LL.getChildAt(j).getTag().toString().equals(idx1) || LL.getChildAt(j).getTag().toString().equals(idx2)) {
final View v = LL.getChildAt(j);
int rtop=0;
if (v.getTag().toString().equals(idx1)) rtop=110+(53*((int)((Integer.parseInt(idx1)-100)/6)));
if (v.getTag().toString().equals(idx2)) rtop=110+(53*((int)((Integer.parseInt(idx2)-100)/6)));
int left=v.getLeft();
int top =v.getTop()+rtop-30;
FrameLayout.LayoutParams lp =new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT);
lp.setMargins(left, top, 0, 0);
if (v.getTag().toString().equals(idx1)) {
final ImageView img1 =new ImageView(mycontext);
img1.setBackgroundResource(R.drawable.match);
img1.setLayoutParams(lp);
myLinearLayoutAll.addView(img1);
img1.setVisibility(View.VISIBLE);
img1.startAnimation(anim1);
anim1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
//img1.setVisibility(View.INVISIBLE);
myLinearLayoutAll.removeView(img1);
}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationStart(Animation animation) { }
});
}
else if (v.getTag().toString().equals(idx2)) {
final ImageView img2 =new ImageView(mycontext);
img2.setBackgroundResource(R.drawable.match);
img2.setLayoutParams(lp);
myLinearLayoutAll.addView(img2);
img2.setVisibility(View.VISIBLE);
img2.startAnimation(anim2);
anim2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
//img2.setVisibility(View.INVISIBLE);
myLinearLayoutAll.removeView(img2);
}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationStart(Animation animation) { }
});
}
}
}
}
} catch (Exception ex) {}
}
in the animation end, i calling removeview, and then the error occure. Why?
It's a weird problem of android, you can't change the view hierarchy in animationEnd.
All you have to do is to postpone your call like this :
#Override
public void onAnimationEnd(Animation animation) {
post(new Runnable() {
public void run() {
myLinearLayoutAll.removeView(img2);
}
});
}