Resize gif file in android

Viewed 324

I have an application include gif file that extended from a view class. I want to display gif file fill parent means fit on the screen, but my code show it in real size.

This is my code for gif view that use from the Movie:

public class GifView extends View{
    private InputStream gifInputStream;
    private Movie gifMovie;
    private int movieWidth, movieHeight;
    private long movieDuration;
    private long movieStart;

    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(R.drawable.m);

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

    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;
    }    

    protected void onDraw(Canvas canvas) {
        long now = SystemClock.uptimeMillis();

        if(movieStart == 0){
            movieStart = now;
        }

        if(gifMovie != null){

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

            int relTime = (int)((now - movieStart)%dur);
            gifMovie.setTime(relTime);
            gifMovie.draw(canvas, 0, 0);
            invalidate();
        }
    }

}

this is xml's code:

<com.example.test.GifView 
        android:id="@+id/gefview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

I search lots and test this code:

public class SampleView extends View {

private Movie mMovie;
private long mMovieStart;

public SampleView(Context context) {
    super(context);
    setFocusable(true);

    java.io.InputStream is;
    is = context.getResources().openRawResource(R.drawable.m);
    mMovie = Movie.decodeStream(is); 
}

public SampleView(Context context, AttributeSet attrSet) {
    super(context, attrSet);
    setFocusable(true);

    java.io.InputStream is;
    is = context.getResources().openRawResource(R.drawable.m);
    mMovie = Movie.decodeStream(is);
}

public SampleView(Context context, AttributeSet attrSet, int defStyle) {
    super(context, attrSet, defStyle);
    setFocusable(true);

    java.io.InputStream is;
    is = context.getResources().openRawResource(R.drawable.mojri);
    mMovie = Movie.decodeStream(is);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(0x00000000);

    Paint p = new Paint();
    p.setAntiAlias(true);

    long now = android.os.SystemClock.uptimeMillis();
    if (mMovieStart == 0) { // first time
        mMovieStart = now;
    }
    if (mMovie != null) {
        int dur = mMovie.duration();
        if (dur == 0) {
            dur = 1000;
        }
        int relTime = (int) ((now - mMovieStart) % dur);
        mMovie.setTime(relTime);
        mMovie.draw(canvas, getWidth() / 2 - mMovie.width() / 2,
                getHeight() / 2 - mMovie.height() / 2);
        invalidate();
    }
}

}

and this:

public class Gif_loading_View extends WebView {

public Gif_loading_View(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    loadUrl("myUrl/m.gif");
}
}

But these views show real size too, and i can't change the size of gif file.

Please help me.

0 Answers
Related