Copyright © android 2011 . Powered by
青藤园
Courtesy of Open Web Design
& Hotels - Dubai
如何让Android支持GIF图片
public class SampleView extends View {
private Movie mMovie;
private Bitmap mBitmap;
private Canvas mCanvas;
private long mMovieStart;
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();
}
public SampleView(Context context) {
super(context);
setFocusable(true);
InputStream is;
is = context.getResources().openRawResource(R.drawable.aa_gif);
if (true) {
mMovie = Movie.decodeStream(is);
} else {
byte[] array = streamToBytes(is);
mMovie = Movie.decodeByteArray(array, 0, array.length);
}
int w = mMovie.width();
int h = mMovie.height();
Log.i("-------width-------", w+"");
Log.i("-------height-------", h+"");
// int[] pixels = new int[w*h];
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFCCCCCC);
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);
mCanvas.drawColor(0xFFCCCCCC);
mMovie.draw(mCanvas, 0, 0);
Bitmap bitmap = Bitmap.createScaledBitmap(mBitmap, 300, 300, false);
mBitmap.createScaledBitmap(mBitmap, 200, 300, false);
canvas.drawBitmap(bitmap, 100, 200, null);
invalidate();
}
}
}
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SampleView sampleView = new SampleView(this);
setContentView(sampleView);
}
}好了,这样就能让android支持gif图片格式了。
Posted by
android_boy @
2011-7-6 22:13:23
阅读(2478)
评论(1)
上一篇:如何在Android下开发耳机HOOK键功能
下一篇:Android设计模式之适配器模式(Adapter Pattern)
上一篇:如何在Android下开发耳机HOOK键功能
下一篇:Android设计模式之适配器模式(Adapter Pattern)
Feedback
你还可以输入600/600个字符
发表评论
回复 2012-1-1 20:28:23 by 开心男孩
看不懂啊
怎么用啊