首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有关Android的Movie类的信息

有关Android的Movie类的信息
EN

Stack Overflow用户
提问于 2011-09-11 02:23:40
回答 3查看 4.2K关注 0票数 6

我正在试着展示一个动画的gif。顺便说一句,我是用Movie类来做的。但Android开发人员页面不提供有关这些方法的信息。

如何调整gif的大小以适应布局?

提前感谢

EN

回答 3

Stack Overflow用户

发布于 2012-11-25 13:50:16

我一直在尝试使用this方法来做同样的事情(显示动画的GIF)。

仅当您指定uses-sdk android:minSdkVersion="3“时,它才有效

用于缩放...

代码语言:javascript
复制
package com.example.GIFShow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;


class MYGIFView extends View {

private static final boolean twigDebug = true;

private Movie mMovie;
private long mMovieStart;

MYGIFView(Context aContext, Movie aMovie) {
    super(aContext);

    if (aMovie == null)
        return;

    mMovie = aMovie;
    mMovieStart = android.os.SystemClock.uptimeMillis();
}

protected void onDraw(Canvas aCanvas) {
    super.onDraw(aCanvas);

    if (mMovie == null || mMovie.duration() == 0)
        return;

    int relTime = (int)((android.os.SystemClock.uptimeMillis() - mMovieStart)
                        % mMovie.duration());
    mMovie.setTime(relTime);

    Bitmap movieBitmap = Bitmap.createBitmap(mMovie.width(), mMovie.height(),
                                             Bitmap.Config.ARGB_8888);
    Canvas movieCanvas = new Canvas(movieBitmap);
    mMovie.draw(movieCanvas, 0, 0);

    Rect src = new Rect(0, 0, mMovie.width(), mMovie.height());
    Rect dst = new Rect(0, 0, this.getWidth(), this.getHeight());
    aCanvas.drawBitmap(movieBitmap, src, dst, null);

    this.invalidate();
}

}

现在,您可以通过以下两种方式之一获取Movie对象并将其传递给该类……

从项目可绘制文件夹获取输入GIF影片对象

代码语言:javascript
复制
    Movie movie = Movie.decodeStream
        (context.getResources().openRawResource(R.drawable.somefile));

或者从外部存储器获取输入GIF电影对象(/mnt/sdcard ... /mnt/ExtSDCard...等)

代码语言:javascript
复制
    Movie movie = null;
    try {
        FileInputStream stream =
        new FileInputStream(Environment.getExternalStorageDirectory() +
                            "/somefolder/somefile.gif");
        try {
            byte[] byteStream = streamToBytes(stream);
            movie = Movie.decodeByteArray(byteStream, 0, byteStream.length);
        }
        finally {
            stream.close();
        }
    }
    catch (IOException e) { }

现在将移动的GIF图像/电影对象设置到activity视图中:

代码语言:javascript
复制
    View view = new MYGIFView(this, movie);
    setContentView(view);

如果您从外部存储(第二个示例)获取GIF image / Movie对象,则需要支持例程:

代码语言:javascript
复制
private byte[] streamToBytes(InputStream is) {

    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];

    int len;
    try {
        while ((len = is.read(buffer)) >= 0)
            os.write(buffer, 0, len);
    }
    catch (java.io.IOException e) { }

    return os.toByteArray();
}
票数 5
EN

Stack Overflow用户

发布于 2015-08-13 19:39:31

代码语言:javascript
复制
You answer is good. It helped me alot.I have changed one thing for performance .Moved the bitmap creation to outside of ondraw

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.graphics.Rect;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;

public class GifView extends View {

    private Movie mMovie;
    InputStream mStream;
    long mMoviestart;
    private Context context;

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

        this.context = context;
        init();
    }

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

    public GifView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    Bitmap movieBitmap  = null;

    private void init() {

        InputStream object = this.getResources().openRawResource(
                R.raw.postloadinganimation);
        mMovie = Movie.decodeStream(object);// context.getResources().getAssets().open("PostLoadingAnimation.gif"));

        movieBitmap = Bitmap.createBitmap(mMovie.width(),
                mMovie.height(), Bitmap.Config.ARGB_8888);      
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);

        super.onDraw(canvas);
        final long now = SystemClock.uptimeMillis();

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

        final int relTime = (int) ((now - mMoviestart) % mMovie.duration());
        mMovie.setTime(relTime);
        // mMovie.draw(canvas, 0, 0);

        Canvas movieCanvas = new Canvas(movieBitmap);
        mMovie.draw(movieCanvas, 0, 0);

        Rect src = new Rect(0, 0, mMovie.width(), mMovie.height());
        Rect dst = new Rect(0, 0, this.getWidth(), this.getHeight());
        canvas.drawBitmap(movieBitmap, src, dst, null);

        this.invalidate();
    }


}
票数 1
EN

Stack Overflow用户

发布于 2014-04-25 07:52:36

我知道,这是一个非常古老的帖子。

但是你可以试试这个。

代码语言:javascript
复制
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    if (mMovie != null) {
        int movieWidth = mMovie.width();
        int movieHeight = mMovie.height();


         /** Calculate horizontal scaling*/

        float scaleH = 1f;
        int measureModeWidth = MeasureSpec.getMode(widthMeasureSpec);

        if (measureModeWidth != MeasureSpec.UNSPECIFIED) {
            int maximumWidth = MeasureSpec.getSize(widthMeasureSpec);
            if (movieWidth > maximumWidth) {
                scaleH = (float) movieWidth / (float) maximumWidth;
            }else{
                scaleH = (float) maximumWidth / (float) movieWidth;
            }
        }


         /** calculate vertical scaling*/

        float scaleW = 1f;
        int measureModeHeight = MeasureSpec.getMode(heightMeasureSpec);

        if (measureModeHeight != MeasureSpec.UNSPECIFIED) {
            int maximumHeight = MeasureSpec.getSize(heightMeasureSpec);
            if (movieHeight > maximumHeight) {
                scaleW = (float) movieHeight / (float) maximumHeight;
            }else{
                scaleW = (float) maximumHeight / (float) movieHeight;
            }
        }


         /** calculate overall scale*/

        mScale = Math.max(scaleH, scaleW);

        mMeasuredMovieWidth = (int) (movieWidth * mScale);
        mMeasuredMovieHeight = (int) (movieHeight * mScale);

        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);

    } else {

         /*No movie set, just set minimum available size.*/

        setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
    }
}

private void drawMovieFrame(Canvas canvas) {

    mMovie.setTime(mCurrentAnimationTime);

    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    canvas.scale(mScale, mScale);
            mLeft = (getWidth() - mMeasuredMovieWidth) / 2f;
    mTop = (getHeight() - mMeasuredMovieHeight) / 2f;
    mMovie.draw(canvas, mLeft / mScale, mTop / mScale);
    canvas.restore();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7373718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档