1

I am new in android and I want to show a loading image in gif format. after searching in google I found this :

GifView :

package ComponentGif;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

public class GifView extends View {

    private InputStream gifInputStream;
    private Movie gifMovie;
    private int movieWidth, movieHeight;
    private long movieDuration;
    private long mMovieStart;
    private Canvas _canvas = null;

    public GifView(Context context) {
        super(context);
        init(context);
    }

    public GifView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GifView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setFocusable(true);
        gifInputStream = context.getResources().openRawResource(com.example.agency.R.drawable.loading);

        gifMovie = Movie.decodeStream(gifInputStream);
        movieWidth = gifMovie.width();
        movieHeight = gifMovie.height();
        movieDuration = gifMovie.duration();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(movieWidth, movieHeight);
    }

    public int getMovieWidth() {
        return movieWidth;
    }

    public int getMovieHeight() {
        return movieHeight;
    }

    public long getMovieDuration() {
        return movieDuration;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) { // first time
            mMovieStart = now;
        }

        if (gifMovie != null) {

            int dur = gifMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }

            int relTime = (int) ((now - mMovieStart) % dur);

            gifMovie.setTime(relTime);

            gifMovie.draw(canvas, 0, 0);

            invalidate();

        }
    }

}

in Main_Activity xml I added this:

<ComponentGif.GifView
        android:id="@+id/imgWaitLoading"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="90dp" />

but the image dose not show in loop.it loops just once.how can I show it in loop?

Iamat8
  • 3,888
  • 9
  • 25
  • 35
m n
  • 223
  • 1
  • 7
  • 19
  • 1
    try this link: "http://stackoverflow.com/questions/6533942/adding-gif-image-in-an-imageview-in-android?rq=1" it is run perfect – Damini Mehra Apr 29 '16 at 12:17

0 Answers0