在android中调整gif文件的大小

时间:2016-03-14 10:28:39

标签: android view gif

我有一个应用程序包含从视图类扩展的gif文件。我想显示gif文件填充父级意味着适合屏幕,但我的代码以实际大小显示它。

这是我在电影中使用的gif视图代码:

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

}

这是xml的代码:

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

我搜索了很多并测试了这段代码:

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

}

和此:

public class Gif_loading_View extends WebView {

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

但这些视图也显示实际大小,我无法更改gif文件的大小。

请帮帮我。

0 个答案:

没有答案