Android自定义View研究(三) -- 获得Bitmap的三种方法

来源:互联网 发布:梦幻西游手游mac版 编辑:程序博客网 时间:2024/05/22 12:01

是不是开始摩拳擦掌了,哈哈,有感觉了吧,有感觉了就加油,开始下一步学习,一步一步始终会学到东西,这章不解释,上面有完整注释

 

一、        使用BitmapFactory解析图片

      

// --> 使用BitmapFactory解析图片

           publicvoid myUseBitmapFactory(Canvas canvas){

           //定义画笔

              Paint paint = new Paint();

           //获取资源流

              Resources rec = getResources();

              InputStream in = rec.openRawResource(R.drawable.haha);

           //设置图片

              Bitmap bitmap =BitmapFactory.decodeStream(in);

           //绘制图片

              canvas.drawBitmap(bitmap, 0,20, paint);         

           }

二、        使用BitmapDrawable解析图片

 

 

       // -->使用BitmapDrawable解析图片

           publicvoid myUseBitmapDrawable(Canvas canvas){

           //定义画笔

              Paint paint = new Paint();

           //获得资源

              Resources rec = getResources();

           // BitmapDrawable

              BitmapDrawable bitmapDrawable = (BitmapDrawable) rec.getDrawable(R.drawable.haha);

           //得到Bitmap

              Bitmap bitmap = bitmapDrawable.getBitmap();

           //在画板上绘制图片

              canvas.drawBitmap(bitmap, 20,120,paint);

           }

 

三、        使用InputStreamBitmapDrawable绘制

 

       // -->使用InputStreamBitmapDrawable解析图片

           publicvoid myUseInputStreamandBitmapDrawable(Canvas canvas){

           //定义画笔

              Paint paint = new Paint();

           //获得资源

              Resources rec = getResources();

           // InputStream得到资源流

              InputStream in = rec.openRawResource(R.drawable.haha);

           // BitmapDrawable解析数据流

              BitmapDrawable bitmapDrawable = new BitmapDrawable(in);

           //得到图片

              Bitmap bitmap = bitmapDrawable.getBitmap();

           //绘制图片

              canvas.drawBitmap(bitmap, 100, 100,paint);

           }

 

                


原创粉丝点击