有关Android电影课程的信息

时间:2011-09-10 18:23:40

标签: java android image animation

我正在尝试展示GIF动画。顺便说一下,我正在使用班级Movie。但Android Developer页面不会授予有关方法的信息。

如何调整gif以适应布局?

提前致谢

3 个答案:

答案 0 :(得分:5)

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

只有在指定uses-sdk android:minSdkVersion =“3”

时才有效

缩放......

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 Movie对象

    Movie movie = Movie.decodeStream
        (context.getResources().openRawResource(R.drawable.somefile));

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

    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图像/电影对象设置到您的活动视图中:

    View view = new MYGIFView(this, movie);
    setContentView(view);

如果从外部存储器获取GIF图像/电影对象(第二个示例),则需要支持例程:

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

答案 1 :(得分:1)

declare
y number;
begin
 palindrome(133,y);
end;

答案 2 :(得分:0)

我知道,这是一个很老的帖子。

但你可以试试这个......

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