Android Api Demos登顶之路(六十八)Graphics-->DecodeBitmap

本文详细探讨了在Android开发中如何使用Api Demos的Graphics模块进行Bitmap的解码操作,同时结合实例讲解了如何实现动态效果,如GIF动图的处理和移动动画。通过这篇文章,开发者可以深入了解Bitmap解码过程以及在图形绘制中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * 这个demo演示了对图像的解码方式。android中主要通过两个类来实现对图像的解码。
 * BitmapFactory主要是对静态图像(如jpg、png)进行解码,Move主要是对动画(如gif动画)进行解码。
 * 可以解析byte 数组,InputStream ,资源ID,或者指定文件名等多种形式的数据源。
 * 对于BitmapFactory来说,还可以通过BitmapFactory.Options 指定解码时的一些设置。
 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }

    private class SampleView extends View{

        //将此值设为false将使用二进制数据进行解码
        private static final boolean DECODE_STREAM = true;
        private Bitmap mBitmap1;
        private Bitmap mBitmap2;
        private Bitmap mBitmap3;
        private Bitmap mBitmap4;
        private Drawable mDrawable;
        private Movie mMovie;
        private long mMovieStart;

        public SampleView(Context context) {
            super(context);
            setFocusable(true);

            InputStream is;

            //根据指定的参数进行解码
            is=getResources().openRawResource(R.drawable.beach);
            BitmapFactory.Options opts=new BitmapFactory.Options();
            Bitmap bm;
            /*
             * 指定opts.inJustDecodeBounds = true,表示解码时只想获取被解码图像的长度和宽度,
             * 此时bm返回值为null, 而opts.outWidth, opts.outHeight中返回了图像的宽度和长度。
             * 这种用法解码器无需为被解码的图像分配内存而值是通过BitmapFactory.Options 
             * 的输出参数返回有关图像的一些信息。
             */
            opts.inJustDecodeBounds=true;
            bm=BitmapFactory.decodeStream(is, null, opts);
            //将参数改回false,获取真正的图像
            opts.inJustDecodeBounds=false;
            //采样比率为4表示将图像缩小为原来的四分之一
            opts.inSampleSize=4;
            mBitmap1=BitmapFactory.decodeStream(is, null, opts);

            //根据图像的像素值数据创建不同配置的图像
            is=getResources().openRawResource(R.drawable.frog);
            mBitmap2=BitmapFactory.decodeStream(is);            
            int w=mBitmap2.getWidth();
            int h=mBitmap2.getHeight();
            int[] pixels=new int[w*h];
            /*获取图像的颜色值存储在一个整形的数组中
             * 第一个参数:保存颜色值的数组
             * 第二个参数:数组的起始索引值
             * 第三个参数:第行的像素数,其绝对值必须大于或等于位图的宽度
             * 第四、五个参数:从位图中读取像素的起始坐标值
             * 第六、七个参数:从每行中读取像素的宽度和读取的行数*/
            mBitmap2.getPixels(pixels, 0, w, 0, 0, w, h);
            //使用不同的配置创建图像
            mBitmap3=Bitmap.createBitmap(pixels, w, h, Bitmap.Config.ARGB_8888);
            mBitmap4=Bitmap.createBitmap(pixels, w, h, Bitmap.Config.ARGB_4444);

            //直接使用Drawable对像
            mDrawable=getResources().getDrawable(R.drawable.button);
            mDrawable.setBounds(150, 20, 300, 100);

            //对gif动画进行解码
             is=getResources().openRawResource(R.drawable.animated_gif);
             if(DECODE_STREAM){
                 mMovie=Movie.decodeStream(is);
             }else{
                 byte[] array=streamToBytes(is);
                 mMovie=Movie.decodeByteArray(array, 0, array.length);
             }
        }

        //定义一个方法将流转换为字节数组
        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 (IOException e) {
                e.printStackTrace();
            }
            return os.toByteArray();
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.WHITE);
            /*Paint p=new Paint();
            p.setAntiAlias(true);*/

            //绘制bitmap位图
            canvas.drawBitmap(mBitmap1, 10, 10, null);
            canvas.drawBitmap(mBitmap2, 10, 170, null);
            canvas.drawBitmap(mBitmap3, 110, 170, null);
            canvas.drawBitmap(mBitmap4, 210, 170, null);

            //绘制drawable对象
            mDrawable.draw(canvas);

            //绘制gif动画
            //从开机到当前的毫秒数
            long now=SystemClock.uptimeMillis();
            if(mMovieStart==0){
                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();
            }

        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值