Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to animate or hide a view by Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to show or hide views with animation in Android". In daily operation, I believe many people have doubts about how to show or hide views with animation in Android. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to show or hide views with animation in Android". Next, please follow the editor to study!

I. demand background

Sometimes, we need to display new information on the screen and remove the old information at the same time. In general, we use VISIBILITY or GONE to set the view that needs to be shown or hidden. The disadvantage of this is that the changes in the actions shown or hidden are very abrupt, and sometimes the changes quickly make the user unable to notice these changes. At this point, you can use animation to show or hide the view, usually using a circle to expose the animation, fade in and fade the animation, or reverse the animation on the card.

Second, create fade in and out animation

Fade in and fade out animation will gradually fade out of one View or ViewGroup, while fade into another. This animation is suitable for switching content or views in the application. ViewPropertyAnimator is used here to create this animation.

The following animation is an example of switching from a progress indicator to some content text fade in and out.

1. Create a layout file 2. Animate fade in and out

For animations that need to fade in, first set their visibility to GONE, which is already set in the layout file. When you need to display a faded View, first set its alpha to 0, which ensures that the View is already displayed but not visible. Set the fade-in animation and the fade-out animation respectively. The fade-in animation changes the alpha attribute of the View from 0 to 1, and the fade-out animation changes the alpha attribute of the View from 1 to 0. For the fade-out animation, after the execution of the animation, set its visibility to GONE, thus speeding up the processing speed.

3. The code implements / / starts executing the fade in and out animation private fun crossFade () {/ / sets the alpha of the View to be 0 Visibility is VISIBLE mBinding.tvContent.apply {alpha = 0f visibility = View.VISIBLE / / change transparency to 1.0 animate () .alpha (1.0f) .setDuration (mShortAnimationDuration.toLong ()) .start ()} / / set the animation that needs to fade out Change its alpha from 1 to 0 and execute events by listening for animation Set the visibility of View to GONE mBinding.loadingProgress.animate () .alpha (0f) .setDuration (mShortAnimationDuration.toLong ()) .setListener (object: AnimatorListenerAdapter () {override fun onAnimationEnd (animation: Animator?)) {super.onAnimationEnd (animation) mBinding.loadingProgress.visibility = View at the end of the animation .Gone}}) .start ()} so far The study on "how Android uses animation to show or hide views" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report