关于电影类为Android信息 [英] Info about the Movie class for Android
问题描述
我想显示GIF动画。顺便说一句,我做它与类电影。但Android开发者页面不授予有关方法的信息。
I'm trying to show an animated gif. By the way I'm doing it with the class Movie. But the Android Developer page dont grant info about the methods.
我怎样才能让GIF大小进行调整以适应布局?
How can I make the gif to be resized to fit the layout?
在此先感谢
推荐答案
我一直在试图做同样的事情(可显示动画GIF)使用的这个方法。
I've been trying to do the same thing (to display animated GIF) using this method.
它的工作原理只有在指定的用途-SDK安卓的minSdkVersion =3
It works only if you specify 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();
}
}
现在你可以和电影对象传递给类两种方式之一...
Now you can get and pass a Movie object to the class in one of two ways ...
获取输入GIF Movie对象从项目绘制文件夹
Get input GIF Movie object from project drawable folder
Movie movie = Movie.decodeStream
(context.getResources().openRawResource(R.drawable.somefile));
或者从外部存储输入GIF Movie对象 (到/ mnt / SD卡...到/ mnt / extSdCard ...等)
Or get input GIF Movie object from external storage (/mnt/sdcard ... /mnt/extSdCard ... etc)
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图像/短片对象插入到活动的看法:
Now set the moving GIF image / Movie object into your activity view:
View view = new MYGIFView(this, movie);
setContentView(view);
如果你得到的GIF图片来自外部存储(第二个例子)/ Movie对象,你需要的支持程序:
If you get the GIF image / Movie object from external storage (second example) you'll need the supporting routine:
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();
}
这篇关于关于电影类为Android信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!