Search code examples
androidandroid-custom-viewandroid-progressbar

Android custom progress bar with .gif file


In my app i have a custom progress bar

progress.xml

 <ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:progressDrawable="@drawable/loader" />

my gif file

My gif image

I want to create a progress bar with this image

In other word i want to show this image whenever i download data from server and remove it after downloading the data

or you can say , how to show this image as a progress bar


Solution

    • First Conver your Gif image to png Slice image sequence.

    • Declare Your Progress bar as Image view.

       <ImageView
           android:id="@+id/main_progress"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:visibility="visible" />
      
    • Create .xml file in drawable folder using your .png sequence image those are generated from gif. In this case, Loading_web_animation.xml

       <?xml version="1.0" encoding="utf-8"?>
           <animation-list    xmlns:android="http://schemas.android.com/apk/res/android"
           android:oneshot="false">
       <item
           android:drawable="@mipmap/wblod_0"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_1"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_2"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_3"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_4"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_5"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_6"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_7"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_8"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_9"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_10"
           android:duration="40" />
       <item
           android:drawable="@mipmap/wblod_11"
           android:duration="40" />
       </animation-list>
      
    • In Main Activity set the code like,

       private AnimationDrawable animationDrawable;
       private ImageView mProgressBar;
       mProgressBar.setBackgroundResource(R.drawable.loading_web_animation);
       animationDrawable = (AnimationDrawable)mProgressBar.getBackground();
       mProgressBar.setVisibility(View.VISIBLE);
       animationDrawable.start();
       mProgressBar.setVisibility(View.GONE);
       animationDrawable.stop();`