Android pop-up window with animation effect
It usually takes a lot of time to load data on the network. At this time, the program often needs to write a pop-up window indicating that the data is being loaded. This article uses two methods to achieve the Dialog with animation effect: both frame animation implementation and GIF dynamic image implementation can achieve the animation effect.
First, frame animation implementation
To customize a Dialog, first check the layout file dialog_animation.xml.
ImageView is used to display animations, and TextView is used to prompt users that we are loading data.
Public static Dialog createAnimationDailog (final Context context) {final Dialog dialog = new Dialog (context, R. style. dialog); dialog. setContentView (R. layout. dialog_animation); ImageView animationView = (ImageView) dialog. findViewById (R. id. dialog_animation_img); animationView. setBackgroundResource (R. drawable. animation_dialog); AnimationDrawable animationDrawable = (AnimationDrawable) animationView. getBackground (); animationDrawable. start (); final TextView textView = (TextView) dialog. findViewById (R. id. dialog_animation_txt); Animation animation = AnimationUtils. loadAnimation (context, R. anim. animation_dialog_txt); // The loop playback mode of animation does not work. You can only manually play the animation once after it is played. setAnimationListener (new AnimationListener () {@ Overridepublic void onAnimationEnd (Animation arg0) {Animation anim = AnimationUtils. loadAnimation (context, R. anim. animation_dialog_txt); textView. startAnimation (anim); anim. setAnimationListener (this) ;}@ Overridepublic void onAnimationRepeat (Animation arg0) {// TODO Auto-generated method stub} @ Overridepublic void onAnimationStart (Animation arg0) {// TODO Auto-generated method stub}); // bind the animated textView. startAnimation (animation); return dialog ;}
The Frame Animation file in ImageView is easy to play in sequence for three images. Create res/drawable/animation_dialog.xml
The animation file used by TextView. Create res/anim/animation_dialog_txt.xml
The screenshot effect is too poor, and the actual animation is very smooth. Believe me.
Second, GIF Dynamic Graph implementation
In this way, you can play animated GIF images.
First, this requires a third-party jar package, GifView. jar. Download it and import it to the project.
Create layout file dialog_gif.xml
Public static Dialog createGIFDialog (final Context context) {final Dialog dialog = new Dialog (context, R. style. dialog); dialog. setContentView (R. layout. dialog_gif); GifView gifView = (GifView) dialog. findViewById (R. id. dialog_gifView); gifView. setGifImage (R. drawable. ride); gifView. showAnimation (); final TextView textView = (TextView) dialog. findViewById (R. id. dialog_gif_txt); Animation animation = AnimationUtils. loadAnimation (context, R. anim. animation_dialog_txt); animation. setAnimationListener (new AnimationListener () {@ Overridepublic void onAnimationEnd (Animation arg0) {Animation anim = AnimationUtils. loadAnimation (context, R. anim. animation_dialog_txt); textView. startAnimation (anim); anim. setAnimationListener (this) ;}@ Overridepublic void onAnimationRepeat (Animation arg0) {// TODO Auto-generated method stub} @ Overridepublic void onAnimationStart (Animation arg0) {// TODO Auto-generated method stub}); // bind the animated textView. startAnimation (animation); return dialog ;}