Animate visibility of a view from gone to visible with animation

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Animate visibility of a view from gone to visible with animation



I have a view that is invisible by default(Just for the first time).


invisible



Now I need to switch the visibility to VISIBLE with this animation:


VISIBLE


animation


if (myView.getVisibility() == View.INVISIBLE)
myView.setVisibility(View.VISIBLE);
myView.animate().translationY(0);



(Like the SnackBar default animation)



But this isn't working. It will turn visible with default animation



Is there any simple way that I could achieve this?



Note



I'm animating my view to dismiss, like this:


myView.animate().translationY(myView.getHeight());




3 Answers
3



You can do this using XML animation.


XML



Create a slide-up animation XML using set and alpha and put this XML into your resource anim folder.


slide-up


XML


set


alpha


XML


anim



slide_up.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="500"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>



USE:



Use AnimationUtils.loadAnimation() to load animation from XML and set and start animation using .startAnimation() method.


AnimationUtils.loadAnimation()


XML


.startAnimation()



Here is an example:


ImageView imageView = (ImageView) findViewById(R.id.imageView);

// slide-up animation
Animation slideUp = AnimationUtils.loadAnimation(this, R.anim.slide_up);

if (imageView.getVisibility() == View.INVISIBLE)
imageView.setVisibility(View.VISIBLE);
imageView.startAnimation(slideUp);



Hope this will help~





I have updated my answer. Its now showing slide up animation as like snackbar
– Ferdous Ahamed
May 23 '17 at 22:35





thanks for the reply, your solution is working too, and I'm gonna accept it because it looks cleaner to me.
– DastakWall
May 23 '17 at 23:00





Glad to know that. If my answer seems useful, please give an upvote. Thanks in advance
– Ferdous Ahamed
May 24 '17 at 2:59



Add animations using ConstraintLayout



Just add below code above the views whose visibility is updated:


TransitionManager.beginDelayedTransition(constraintLayout)



Note:



For more see this post https://robinhood.engineering/beautiful-animations-using-android-constraintlayout-eee5b72ecae3



Based on this answer:



with this methods, I can set the visibility of my view to VISIBLE with a slideUp animation(Like snackbar animation):


int getScreenHeight()
DisplayMetrics displaymetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.heightPixels;


public void animateOnScreen(View view)
final int screenHeight = getScreenHeight();
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "y", screenHeight, (screenHeight * 0.8F));
animator.setInterpolator(new DecelerateInterpolator());
animator.start();



Then I can use it like this:


if (myView.getVisibility() == View.INVISIBLE)
myView.setVisibility(View.VISIBLE);
animateOnScreen(myView);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard