如何在Android上加速电影/ gif再现

时间:2015-09-07 14:28:05

标签: android gif

View重现GIF和Seekbar让用户更快或更慢地播放gif,我遇到了成功实现速度的问题。

GifView将gif重现为直接工作的用户:

public class GifView extends View {

    private int gifRawId;   
    private InputStream gifInputStream;
    private Movie gifMovie;
    private int movieWidth, movieHeight;
    private long movieDuration;
    private long mMovieStart;

    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){
        if(gifRawId!=0) {
            setFocusable(true);
            gifInputStream = context.getResources().openRawResource(gifRawId);
            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();

        }

    }

    public void setGifRawId(int gifDrawable, Context context) {
        this.gifRawId = gifDrawable;
        init(context);
    }
}

然后,我得到一个SeekBar让用户增加或减少复制的速度。但是relTime = (int)(((now - mMovieStart) % dur) / gifSpeed);无法正常工作,因为它无法正常工作(gifSpeed是从Seekbar获得的值。)

如何加快gif / video的复制?

1 个答案:

答案 0 :(得分:0)

问题是relTime中的术语顺序。正确的答案是:

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

如果有人想要速度控制的GifView完整代码,接下来我发布它:

public class GifView extends View {

    private int gifRawId;
    private double gifSpeed = 1;    
    private InputStream gifInputStream;
    private Movie gifMovie;
    private int movieWidth, movieHeight;
    private long movieDuration;
    private long mMovieStart;

    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){
        if(gifRawId!=0) {
            setFocusable(true);
            gifInputStream = context.getResources().openRawResource(gifRawId);
            gifMovie = Movie.decodeStream(gifInputStream);
            movieWidth = gifMovie.width();
            movieHeight = gifMovie.height();
            movieDuration = gifMovie.duration();
        }
    }

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

    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) * gifSpeed) % dur));


            gifMovie.setTime(relTime);

            gifMovie.draw(canvas, 0, 0);
            invalidate();

        }

    }

    public void setGifRawId(int gifDrawable, Context context) {
        this.gifRawId = gifDrawable;
        init(context);
    }

    public void setGifSpeed(double gifSpeed){
        this.gifSpeed = gifSpeed;
    }
}