2011.07.18(4)——— android 播放gif

参考:APIDemos——graphics——BitmapDecode

Android API中用来解码图像的类主要有BitmapFactory (静态图像PNG或是JPEG)和Movie 解码动画(gif动画等)。

对图像或动画解码,数据源可以说byte 数组,InputStream ,资源ID,或者指定文件名。对于BitmapFactory来说,还可以通过BitmapFactory.Options 指定解码时的一些设置。

opts.inJustDecodeBounds = true

表示解码时只想获取被解码图像的长度和宽度,此时bm返回值为null, 而opts.outWidth, opts.outHeight中返回了图像的宽度和长度

opts.inSampleSize = 4; 

代码将采样大小设为4,相当于每隔4个像素采样一次,结果是解码后的图像为原图的四分之一(具体的采用算法由平台决定,并非简单的隔4个像素取其中一个像素值)。



android.graphics.Movie对应可以用来解码.gif动画资源,从数组或是直接从InputStream中解码:R.drawable.animated_gif 为一飘动的旗帜动画。
private Movie mMovie;            .....            is = context.getResources().openRawResource(R.drawable.animated_gif);            if (true) {                mMovie = Movie.decodeStream(is);            } else {                byte[] array = streamToBytes(is);                mMovie = Movie.decodeByteArray(array, 0, array.length);            }            ....           private static 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();        }


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);                mMovie.draw(canvas, getWidth() - mMovie.width(),                            getHeight() - mMovie.height());                invalidate();            }




更多相关文章

  1. android视图切换动画:ViewAnimator类及其子类
  2. 2011.07.18(2)——— android Animation的另一种运行
  3. 2011.07.18(2)——— android Animation的另一种运行
  4. Android(安卓)实现 按钮从两边移到中间动画效果
  5. Android(安卓)Media Framework 总纲
  6. android 关于截屏
  7. Android之一种很有趣的界面跳动提示动画
  8. android 动画之水波纹效果ripple
  9. Android控件之ImageView,Button, ImageButton

随机推荐

  1. android Application类的详细介绍
  2. Android NDK报错(Eclipse)及解决方法
  3. 使用迅雷代替SDK Manager快速下载Android
  4. 《转载》Android(安卓)AlertDialog 方法s
  5. 【Android】16.5 Android内置的系统服务
  6. android 加载动态库
  7. Android 中文api (88)――SharedPreferen
  8. Android(安卓)Model正确使用姿势——Auto
  9. android中调用接口发送短信
  10. Android(安卓)sensor 实用篇