android animation frame animation FrameAnimation

Frame animation - FrameAnimation

Play a series of pictures in order to form an animation effect. Its essence is a Drawable, which is a collection of a series of pictures, which can be used as a picture itself

1. Under the Drawable folder, create a resource file (drawable_anim) with animation-list as the root node

<animation-listandroid:oneshot="false">
    <item
android:drawable="@mipmap/progress_1"android:duration="50"/>
    <item
android:drawable="@mipmap/progress_2"android:duration="50"/>
    <item
android:drawable="@mipmap/progress_3"android:duration="50"/>
    <item
android:drawable="@mipmap/progress_4"android:duration="50"/>
</animation-list>

 

oneshot: Whether to play only once 

drawable: a picture referenced by a frame

duration: the playing time of one frame

 

2. Set the background in imageview ( R.layout.anim_drawable_dialog_layout )

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageView
        android:id="@+id/refreshing_drawable_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scaleType="fitCenter"
        android:src="@drawable/drawable_anim" />
</RelativeLayout>

 

 

3. Inherit AlertDialog and set the style

Get the animationDrawable of the image (progressImg.getDrawable())

If the image is set to background (progressImg.getBackground())

animationDrawable.start, start animation

public class AnimDrawableAlertDialog extends AlertDialog {


    private ImageView progressImg ;
     // frame animation
     private AnimationDrawable animation ;

    public AnimDrawableAlertDialog(Context context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anim_drawable_dialog_layout);

        // Click the outside area of ​​imageview , the animation will not disappear
         setCanceledOnTouchOutside( false ) ;

        progressImg = (ImageView) findViewById(R.id. refreshing_drawable_img ) ;
         // load animation resources
         animation = (AnimationDrawable) progressImg .getDrawable() ;
     }

    /**
     * Execute the start animation in
      the onStart() life cycle of AlertDialog */
     @Override
     protected void onStart () {
         super .onStart() ;
         animation .start() ;
     }

    /**
     * Execute stop animation in AlertDialog 's onStop() lifecycle
      */
     @Override
     protected void onStop () {
         super .onStop() ;
         animation .stop() ;
     }

}

 

 

4. Set the dialog style ( R.style.MyDialog )

<resources>

    <!-- Custom dialog background fully transparent without borders -->
     <style name= "MyDialog" parent= "android:style/Theme.Dialog" >

        <!-- Background color and degree of transparency -->
         <item name= "android:windowBackground" > @android:color/transparent </item>
         <!-- Whether to remove the title -->
         <item name= "android: windowNoTitle" > true </item>
         <!-- Whether to remove the border -->
         <item name= "android:windowFrame" > @null </item>
         <!-- Whether to float above the activity -->
         <item name = "android:windowIsFloating" >true</item>
        <!-- Is it blurry -->
         <item name= "android:backgroundDimEnabled" > false </item>
    </style>

</resources>

 

5. Call the dialog box in the activity

AnimDrawableAlertDialog progressDrawableAlertDialog = new AnimDrawableAlertDialog(this);
progressDrawableAlertDialog.show();

 

6. Effect



Guess you like

Origin blog.csdn.net/l331258747/article/details/70856666