Android开发-通过Drawable对象获取Bitmap对象

1.简介:

在Android程序开发中,经常需要处理图像,有时,需要将Drawable对象转换为Bitmap对象进行处理,本篇介绍两种方式,实现“通过Drawable对象获取Bitmap对象”的目的。

2. 方式一:通过BitmapDrawable函数进行获取:

 //获取
 public Bitmap drawableToBitamp(Drawable drawable)
 {
     BitmapDrawable bd = (BitmapDrawable) drawable;
     Bitmap bitmap = bd.getBitmap();
     return bitmap
 }

说明:

先将Drawable对象转换为

BitmapDrawable

对象,再通过getBitmap来获取Bitmap对象。

3. 方式二:通过Bitmap.createBitmap来创建一个新的Bitmap对象:

      public Bitmap drawableToBitamp(Drawable drawable)
      {
          int w = drawable.getIntrinsicWidth();
          int h = drawable.getIntrinsicHeight();
          System.out.println("Drawable转Bitmap");

          //设置参数
          Bitmap.Config config = 
                  drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                          : Bitmap.Config.RGB_565;
         //创建bitmap对象
         Bitmap bitmap = Bitmap.createBitmap(w,h,config);
         Canvas canvas = new Canvas(bitmap);   
         drawable.setBounds(0, 0, w, h);   
         drawable.draw(canvas);
         return bitmap;
      }

说明:

 通过Bitmap.Config设置bitmap对象的参数,再通过调用Bitmap.createBitmap来创建bitmap,这种方式经常被使用。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值