I have an requirement to show the text on Recyclerview with FadeIn & FadeOut animation.
Below is the dynamic list where it's need to loop infinite items i.e; 1-2-3-1-2-3-1-2-3
List<String> mImageDesc = new List<String>();
mImageDesc.add("1");
mImageDesc.add("2");
mImageDesc.add("3");
I have used below code but which is not working for me.
final Animation animationFadeIn = AnimationUtils.loadAnimation(mContext, R.anim.fade_in_animation);
final Animation animationFadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out_animation);
Animation.AnimationListener animListener = new Animation.AnimationListener(){
// Required to change the image
int i = 0;
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (animation == animationFadeIn) {
// Start fade-out animation
mTxtImageDescription.startAnimation(animationFadeOut);
} else if (animation == animationFadeOut) {
*while(mImageDesc.listIterator().hasNext()) {
System.out.println(mImageDesc.listIterator().next());
mTxtImageDescription.setText(mImageDesc.listIterator().next());
mTxtImageDescription.startAnimation(animationFadeIn);*
}
}
}
};
// Set listener to animation
animationFadeIn.setAnimationListener(animListener);
animationFadeOut.setAnimationListener(animListener);
// Start fade-in animation
mTxtImageDescription.setText(mImageDesc.get(0));
mTxtImageDescription.startAnimation(animationFadeIn);
In the last line off your while loop you have RE-started the 'animationFadeIn' ,so
the 'animListener' callbacks will be called forever .Notice if while loop was that repeating you would have 'StackOverflowError'.
Related
I'm currently working in a custom android native plugin for Capacitor. The plugin consists in an app's footer which is working fine except for a hiding animation.
The problem is I'm changing the WebView margin everytime the footer is being shown/hide, which makes a black(sometimes is orange, probably because is one of app's main colors) bar appears in the space occupied by the footer. Black bar disappears once the animation ends.
I've tried changing the WebView margin at animation start/end and the result is the same.
Thanks in advance guys, here is some code.
Animation XML:
<translate
android:duration="150"
android:fromYDelta="0"
android:toYDelta="100%" />
WebView margin function:
private void changeWebViewMargin(float pixels) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) wb.getLayoutParams();
params.setMargins(0, 0, 0, (int) dpTopixel(getContext(), pixels));
wb.setLayoutParams(params);
wb.requestLayout();
}
Hide function:
#PluginMethod()
public void hide(PluginCall call) {
Boolean animated = call.getBoolean("animated");
if (animated == null) {
animated = false;
}
final boolean finalAnimated = animated;
this.getBridge().getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
if (finalAnimated && footer.getVisibility() == View.VISIBLE) {
changeWebViewMargin(0f);
Animation myAnim = AnimationUtils.loadAnimation(getBridge().getContext(), R.anim.hide_footer);
myAnim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
footer_img.setVisibility(View.INVISIBLE);
footer.setVisibility(View.INVISIBLE);
btn3.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
footer.startAnimation(myAnim);
footer_img.startAnimation(myAnim);
btn3.startAnimation(myAnim);
} else {
footer_img.setVisibility(View.INVISIBLE);
footer.setVisibility(View.INVISIBLE);
btn3.setVisibility(View.INVISIBLE);
changeWebViewMargin(0f);
}
}
});
}
Instead of working with margins try using WindowInsets:
ViewCompat.setOnApplyWindowInsetsListener(wb, (v, insets) -> {
((ViewGroup.MarginLayoutParams) v.getLayoutParams()).bottomMargin =
insets.getSystemWindowInsetTop() + (int) dpTopixel(getContext(), pixels);
return insets.consumeSystemWindowInsets();
});
I'm trying to set an image visibility after an animation ends, but i'm have difficulty.
I have created few images and set the tag for each. Whenever the user clicks the image the visibility gets set to invisible. I have also created a method that animates the images. I would like when the animation finishes for the visibility of the images to reset to Visible. Apologies,if this sounds a bit unclear i'm not entirely sure how to word this question. Any help would be appreciated.
public void popBubbles(View view) {
final String tag = String.valueOf(view.getTag());
if(tag=="0"){
bubble.setVisibility(View.INVISIBLE);
}else if(tag=="1"){
bubble1.setVisibility(View.INVISIBLE);
}else if(tag=="2"){
bubble2.setVisibility(View.INVISIBLE);
}else if(tag=="3"){
bubble3.setVisibility(View.INVISIBLE);
}else if(tag=="4"){
bubble4.setVisibility(View.INVISIBLE);
}else if(tag=="5"){
bubble5.setVisibility(View.INVISIBLE);
}else if(tag=="6"){
bubble6.setVisibility(View.INVISIBLE);
}else if(tag=="7"){
bubble7.setVisibility(View.INVISIBLE);
}else if(tag=="8"){
bubble8.setVisibility(View.INVISIBLE);
}
}
public void animateBubbles() {
for (final ImageView img : IMGS) {
animation = ObjectAnimator.ofFloat(img, "translationY", 0f, -deviceHeight);
animation.setDuration(4000);
animation.start();
animation.setRepeatCount(ValueAnimator.INFINITE);
}
}
This code is in kotlin but you can add a listener to the animation use the on AnimationEnd() method like this.
animation.addListener(object : Animator.AnimatorListener {
override fun onAnimationEnd(animation: Animator?) {
}
})
Hope this helps.
set Animation listener and override methods. You can write your logic in onAnimationEnd method
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//Do operation on ImageView
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
I'm quite new to Android Studio, and I would like to animate multiple images at the same time, over multiple coordinates.
Let's say I would like to move image A from 0/0 to 100/100, and then to 200/200; at the same time (eg. by the click of a button), image B shall be moved from 0/100 to 100/100, and then to 200/100. All with simple translations.
(As a plus, I would like to be able to set the duration of the movements independently.)
When the animations have finished, there should be an event (somthing like OnAnimationFinish?) to trigger other things, like enabling the start button again.
What would be the most effective way to do this? I know there is an AnimationSet in Android to store multiple animations, but I don't know if this is helpful here.
Thanks in advance!
You can use ObjectAnimator with AnimatorSet to play multiple animations simultaneously with proper listeners.
For example():
val imageXAnimator = ObjectAnimator.ofFloat(imageView, "translationX", translateX)
val imageYAnimator = ObjectAnimator.ofFloat(imageView, "translationY", translateY)
val imageAlphaAnimator = ObjectAnimator.ofFloat(imageView, "alpha", if (reverse) 0F else 1F)
val animationSet = AnimatorSet()
animationSet.playTogether(
imageXAnimator,
imageYAnimator,
imageAlphaAnimator)
animationSet.interpolator = DecelerateInterpolator()
animationSet.duration = 1000
animationSet.addListener(
onStart = {
//When animation is started
},
onEnd = {
//When animation finishes
}
)
animationSet.start()
Or a simple extension in kotlin which can be called on a specific View:
inline fun View.animateTranslationY(translationY: Float, duration: Long = 1000, startDelay: Long = 0) {
val translationYAnimator = ObjectAnimator.ofFloat(this, "translationY", translationY)
translationYAnimator.duration = duration
translationYAnimator.startDelay = startDelay
translationYAnimator.interpolator = LinearInterpolator()
translationYAnimator.addAnimatorListener(
onStart = { },
onEnd = { }
)
translationYAnimator.start()
}
You can use the "View.animate()" method that allow you to access to its sub-methods and events to manage all these stuff. You just have to call "animate()" on a View (TextView, ImageView, EditBox, etc..) and then call its sub-methods to decide what kind of animation to perform: motion, rotation, changing transparency, etc... For each animation you can set a listener that will be called at Start and/or at the End of animation.
Consider these two methods for both views
private void animateFirstView(final ImageView first, long durationFirst, final long durationSecond) {
first.setTranslationX(0);
first.setTranslationY(0);
first.animate().translationX(100).translationY(100).setDuration(durationFirst).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
first.animate().translationY(200).translationX(200).setDuration(durationSecond).setListener(null).setInterpolator(new LinearInterpolator()).start();
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
}).setInterpolator(new LinearInterpolator()).start();
}
private void animateSecondView(final ImageView second, long durationFirst, final long durationSecond) {
second.setTranslationX(0);
second.setTranslationY(100);
second.animate().translationX(100).translationY(100).setDuration(durationFirst).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
second.animate().translationY(200).translationX(100).setDuration(durationSecond).setListener(null).setInterpolator(new LinearInterpolator()).start();
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
}).setInterpolator(new LinearInterpolator()).start();
}
You can do this:
private void playAnimation(final View view, final int endRangeY) {
view.animate().translationY(endRangeY) //endRangeY where you want to end the animation
.alpha(0.0f)
.setDuration(3000)
.setListener(new AnimatorListenerAdapter() {
#Override public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
}
I'm developing a puzzle game. The essence of the issue is this. I has buttons (ImageButton) in the ArrayList mButtons collection and each button has listener, I prescribe an animation of the button for the listener. When I click on the button, the animation works well, BUT !!! if I go back to that button, I can't move it and not press it! As though on top of it the textures were superimposed! Other buttons are pressed. I checked different ways:
v.getAnimation().reset();
v.getAnimation().cancel();
v.clearAnimation();
v.setAnimation(null);
v.getAnimation().setAnimationListener(null);
did not help! If there are any tips and you have encountered the same issue, please reply!
Picture for understanding:
The code to make it clearer:
public class TouchButtonAction implements View.OnClickListener {
private GameController mController;
private List<ImageButton> mButtons;
private List<GridLayout.LayoutParams> mParams;
private final Animation myAnimA;
private volatile boolean isAnimAFinish = true;
public TouchButtonAction(Context context, GameController controller,
List<ImageButton> buttons, List<GridLayout.LayoutParams> params) {
this.mParams = params;
this.mButtons = buttons;
this.mController = controller;
this.myAnimA = AnimationUtils.loadAnimation(context, R.anim.bounce_swap);
final ButtonBounceInterpolator mInterpolatorA = new ButtonBounceInterpolator(0.07, 20);
this.myAnimA.setInterpolator(mInterpolatorA);
// this.myAnimA.setAnimationListener(new Animation.AnimationListener() {
// #Override
// public void onAnimationStart(Animation animation) {
// isAnimAFinish = false;
//// animation.setFillAfter(false);
//// animation.reset();
//
// }
//
// #Override
// public void onAnimationEnd(Animation animation) {
//// animation.reset();
// animation.cancel();
// isAnimAFinish = true;
//// synchronized (this){
//
//// }
// }
//
// #Override
// public void onAnimationRepeat(Animation animation) {
//
// }
// });
#Override
public void onClick(final View v) {
int a = mButtons.indexOf((ImageButton) v);
int b = 0;
for (ImageButton button : mButtons) {
if (!button.isEnabled()) {
b = mButtons.indexOf(button);
}
}
if (here check logic) {
SoundPlay.playSoundPool(SoundPlay.TOUCH);
if (isAnimAFinish){
// v.post(new Runnable() {
// #Override
// public void run() {
// synchronized (this){
v.startAnimation(myAnimA);
isAnimAFinish = false;
// }
//
// }
// });
}
if (v.getAnimation() != null){
if (v.getAnimation().hasEnded()){
Log.d("LOGGG", "hasEnded Anim");
v.getAnimation().reset();
v.getAnimation().cancel();
v.clearAnimation();
v.setAnimation(null);
v.getAnimation().setAnimationListener(null);
isAnimAFinish = true;
}
}
Collections.swap(mButtons, a, b);
mController.getGridLayout().removeAllViewsInLayout();
mController.getGridLayout().removeAllViews();
for (int i = 0; i < mButtons.size(); i++) { // i < 12
ImageButton button = mButtons.get(i);
mController.getGridLayout().addView(button, mParams.get(i));
}
mController.getGridLayout().invalidate();
} else {
SoundPlay.playSoundPool(SoundPlay.TOUCH_BANG);
}
mController.checkSolution(mButtons);
}
If you want to hide the button after click, then you can use button.setVisibility(View.GONE) or if you want, that after click user can't to click that button anymore, you can use after click button.setClickable(false). I think I helped you. If you have questions, ask me.
How to remove excess texture? Maybe you can keep two images, one with texture, and another without. Upon clicking first one with texture, hide it and show second one.
It turns out that it was not necessary to use AnimationUtils.loadAnimation (), but quite other classes that allow manipulating objects, such as widgets.
Example animation bouncing button:
AnimatorSet as = new AnimatorSet ();
float y = view.getTranslationY (), distance = 20F;
as.playSequentially (
ObjectAnimator.ofFloat (view, "translationY", y-distance), // animation 1
ObjectAnimator.ofFloat (view, "translationY", y), // animation 2
ObjectAnimator.ofFloat (view, "translationY", y - (distance / 2)), // animation 3
ObjectAnimator.ofFloat (view, "translationY", y)); // animation 4
as.setDuration (600);
as.start ();
And the animation of your widget starts to jump. I hope this helped you as much as me.
So I have implemenented a fragment of code which works perfectly found on another thread
The code is listed below :
private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever){
//imageView <-- The View which displays the images
//images[] <-- Holds R references to the images to display
//imageIndex <-- index of the first image to show in images[]
//forever <-- If equals true then after the last image it starts all over again with the first image resulting in an infinite loop. You have been warned.
int fadeInDuration = 500; // Configure time values here
int timeBetween = 3000;
int fadeOutDuration = 1000;
imageView.setVisibility(View.INVISIBLE); //Visible of invisible by default
imageView.setImageResource(images[imageIndex]);
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); //Add this
fadeIn.setDuration(fadeInDuration);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(fadeInDuration + timeBetween);
fadeOut.setDuration(fadeOutDuration);
AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
animation.setRepeatCount(1);
imageView.setAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
if(images.length -1 > imageIndex){
animate(imageView, images, imageIndex + 1, forever); // Calls itself until it gets to the end of the array.
}
else{
if(forever == true){
animate(imageView, images, 0, forever); //Calls itself to start the animation all over again in a loop if forever = true
}
}
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}
Basically this code accepts an Array of images and creates a slideshow with fadeIn and FadeOut effects. Once it reaches the end the animation stops. How can I modify the code to keep animating forever?
Found the solution Guys.
Just had to make a slight alteration! On the else statement I had to set Forever to false!
else{
if(forever == false){
animate(imageView, images, 0, forever);
}
}