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);
}
});
}
Related
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();
}
}
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
I'm currently using Nine Old Androids to animate a RelativeLayout like a slider menu, which slides down form the top. I've tried in many ways, Can someone make a suggestion?
Here is the animation code:
private OnClickListener slideClick = new OnClickListener(){
#Override
public void onClick(View v) {
slideGroups.bringToFront();
if(!mPulledDown) {
ObjectAnimator oa = ObjectAnimator.ofFloat(slideGroups, "translationY", slideGroups.getHeight()-80);
oa.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {}
#Override
public void onAnimationRepeat(Animator arg0) {}
#Override
public void onAnimationEnd(Animator arg0) {
arrow.setImageResource(R.drawable.arrowup);
if(Application.getAndroidAPILevel() < 11) {
// only if pre 3.0 (api level 11)
Toast.makeText(FriendsActivity.this, String.format("api level %s",
Application.getAndroidAPILevel()), Toast.LENGTH_SHORT).show();
//RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) slideGroups.getLayoutParams();
// clear animation to prevent flicker
slideGroups.clearAnimation();
// set new "real" position of wrapper
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, slideGroups.getWidth());
//lp.addRule(RelativeLayout.BELOW, R.id.branchFinderIncludeHeader);
slideGroups.setLayoutParams(lp);
}
}
#Override
public void onAnimationCancel(Animator arg0) {}
});
oa.start();
}
else
{
ObjectAnimator oa = ObjectAnimator.ofFloat(slideGroups, "translationY", 0);
oa.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {}
#Override
public void onAnimationRepeat(Animator arg0) {}
#Override
public void onAnimationEnd(Animator arg0) {
arrow.setImageResource(R.drawable.arrowdown);
if(Application.getAndroidAPILevel() < 11) {
// only if pre 3.0 (api level 11)
Toast.makeText(FriendsActivity.this, String.format("api level %s",
Application.getAndroidAPILevel()), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onAnimationCancel(Animator arg0) {}
});
oa.start();
}
mPulledDown = !mPulledDown;
}
};
I just need something to work with sdk level 4 and newer. Currently, touching/tapping passes that event to the list underneath the slider when expanded. Please let me know if any other information is needed. slideGroups is the RelativeLayout. arrow is an imageview. groupsButton is a LinearLayout with text and an image button. groupsButton should be clickable; that's what expands and is supposed to collapse it.
Thanks in advance.
A few people have contributed by editing my question with solutions. I'd like to give them credit, but can't upvote an edit. For posterity, here is the solution that works for me.
int originalMarginLeft = 0;
int originalMarginTop = 0;
int originalMarginRight = 0;
int originalMarginBottom = 0;
int originalLeft = 0;
int originalTop = 0;
int originalRight = 0;
int originalBottom = 0;
int originalX = 0;
int originalY = 0;
int originalHeight = 0;
private OnClickListener slideClick = new OnClickListener(){
#Override
public void onClick(View v) {
slideGroups.bringToFront();
if(originalMarginLeft == 0) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) slideGroups.getLayoutParams();
originalMarginLeft = params.leftMargin;
originalMarginTop = params.topMargin;
originalMarginRight = params.rightMargin;
originalMarginBottom = params.bottomMargin;
originalLeft = slideGroups.getLeft();
originalTop = slideGroups.getTop();
originalRight = slideGroups.getRight();
originalBottom = slideGroups.getBottom();
originalHeight = params.height;
}
if(!mPulledDown) {
ObjectAnimator oa = ObjectAnimator.ofFloat(slideGroups, "translationY", originalHeight);
oa.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {}
#Override
public void onAnimationRepeat(Animator arg0) {}
#Override
public void onAnimationEnd(Animator arg0) {
arrow.setImageResource(R.drawable.arrowup);
if(Application.getAndroidAPILevel() < 11) {
slideGroups.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(slideGroups.getWidth(), slideGroups.getHeight());
lp.leftMargin = originalMarginLeft;
lp.rightMargin = originalMarginRight;
slideGroups.setLayoutParams(lp);
}
}
#Override
public void onAnimationCancel(Animator arg0) {}
});
oa.start();
}
else
{
ObjectAnimator oa = ObjectAnimator.ofFloat(slideGroups, "translationY", -originalHeight+60);
oa.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {}
#Override
public void onAnimationRepeat(Animator arg0) {}
#Override
public void onAnimationEnd(Animator arg0) {
arrow.setImageResource(R.drawable.arrowdown);
if(Application.getAndroidAPILevel() < 11) {
slideGroups.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(slideGroups.getWidth(), slideGroups.getHeight());
lp.leftMargin = originalMarginLeft;
lp.rightMargin = originalMarginRight;
lp.topMargin = originalMarginTop;
lp.bottomMargin = originalMarginBottom;
slideGroups.setLayoutParams(lp);
}
}
#Override
public void onAnimationCancel(Animator arg0) {}
});
oa.start();
}
mPulledDown = !mPulledDown;
}
};
I want to play one animation with multiple views in android.
This is the simplified example of code.
This code works incorrectly.
Every startAnimation() call, affects all previously animated views
Please tell me, why it doesn't works and how to make it properly.
public SomeClass() {
private int currentViewID = 0;
private View[] views = { view1, view2, view3, view4, view5 }
private Animation anim = AnimationUtils.loadAnimation(this.getContext(), android.R.anim.fade_out);
public SomeClass() {
this.anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
if (SomeClass.this.currentViewID != SomeClass.this.views.length) SomeClass.this.hideNextView();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
this.hideNextView();
}
private void hideNextView() {
this.views[this.currentViewID++].startAnimation(this.anim);
}
}
If you don't want to deal with many Animations in your class (one for each View) you could do things locally:
private int currentViewID = 0;
private View[] views = { view1, view2, view3, view4, view5 }
public SomeClass() {
this.hideNextView();
}
private void hideNextView() {
final Animation anim = AnimationUtils.loadAnimation(this.getContext(), android.R.anim.fade_out);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
if (SomeClass.this.currentViewID != SomeClass.this.views.length) SomeClass.this.hideNextView();
}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationStart(Animation animation) {}
});
this.views[this.currentViewID++].startAnimation(anim);
}
I would like to make a simple animation. My logo is divided in 4 parts :
1|2
3|4
I would like to fadein-out each part, but only one after the other : 1 -> 2 -> 4 -> 3 -> 1...
I have no problem with this, but sometimes my animation is not synchronized and the 4 parts animate simultaneously :
final ImageView logo1 = (ImageView) v.findViewById(R.id.logo1);
logo1.clearAnimation();
final ImageView logo2 = (ImageView) v.findViewById(R.id.logo2);
logo2.clearAnimation();
final ImageView logo3 = (ImageView) v.findViewById(R.id.logo3);
logo3.clearAnimation();
final ImageView logo4 = (ImageView) v.findViewById(R.id.logo4);
logo4.clearAnimation();
final Animation anim1 = AnimationUtils.loadAnimation(c, R.anim.animlogo);
anim1.setFillAfter(true);
anim1.setStartOffset(0);
final Animation anim2 = AnimationUtils.loadAnimation(c, R.anim.animlogo);
anim2.setStartOffset(anim1.getDuration());
anim2.setFillAfter(true);
final Animation anim3 = AnimationUtils.loadAnimation(c, R.anim.animlogo);
anim3.setStartOffset(anim1.getDuration()+anim2.getDuration());
anim3.setFillAfter(true);
final Animation anim4 = AnimationUtils.loadAnimation(c, R.anim.animlogo);
anim4.setStartOffset(anim1.getDuration()+anim2.getDuration()+anim3.getDuration());
anim4.setFillAfter(true);
anim1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
anim1.setStartOffset(anim2.getDuration()+anim4.getDuration()+anim3.getDuration());
logo1.startAnimation(anim1);
}
});
anim2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
anim2.setStartOffset(anim1.getDuration()+anim4.getDuration()+anim3.getDuration());
logo2.startAnimation(anim2);
}
});
anim3.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
anim3.setStartOffset(anim2.getDuration()+anim4.getDuration()+anim1.getDuration());
logo4.startAnimation(anim3);
}
});
anim4.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
anim4.setStartOffset(anim2.getDuration()+anim1.getDuration()+anim3.getDuration());
logo3.startAnimation(anim4);
}
});
logo1.startAnimation(anim1);
logo2.startAnimation(anim2);
logo4.startAnimation(anim3);
logo3.startAnimation(anim4);
anim1.startNow();anim2.startNow();anim3.startNow();anim4.startNow();
Any idea ?
Start only anim1:
logo1.startAnimation(anim1);
and in anim1's AnimationListener's onAnimationEnd do:
logo2.startAnimation(anim2);
This way anim2 won't start before anim1 is finished.