android 创建bitmap的多种方式

–Bitmap代表这一张位图,BitmapDrawable里封装的图片就是一个Bitmap对象。如果要将Bitmap对象封装成BitmapDrawable对象只需要用构造方法即可。
//讲bitmap对象包装成BitmapDrawable对象

BitmapDrawable drawable = new BitmapDrawable(bitmap);


//将BitmapDrawable对象转换为bitmap

Bitmap bitmap = drawable.getBitmap();

Bitmap对象提供了一系列静态方法来创建新的Bitmap对象

 

•createBitmap(Bitmap source, int x, int y, int width, int height):从原位图中指定坐标点(x,y)开始,从中挖取宽width、高height的一块出来,创建新的Bitmap对象。
•createScaledBitmap(Bitmap source, int dstWidth, int dstHeight, boolean filter):对源位图进行缩放,缩放成指定width、height大小的新位图对象。
•createBitmap(int width, int height, Bitmap.Config config):创建一个宽width、高height的新位图。


•createBitmap(Bitmap source, int x, int y, int width, int height, Matrix matrix, boolean filter):从原位图中指定坐标点(x,y)开始,从中挖取宽width、高height的一块出来,创建新的Bitmap对象。并按Matrix指定的规则进行变换。


BitmapFactory是一个工具类,它提供了大量的方法来用于从不同的数据源来解析、创建Bitmap对象。包含了如下方法

 

•decodeByteArray(byte[] data, int offset, int length):从指定的字节数组的offset位置开始,将长度为length的字节数据解析成Bitmap对象。
•decodeFile(String pathName):从pathName指定的文件中解析、创建Bitmap对象。
•decodeFileDescriptor(FileDescriptor fd):从FileDescriptor对应的文件中解析、创建Bitmap对象。
•decodeResource(Resources res, int id):根据给定的资源ID从指定资源中解析、创建Bitmap对象。
•decodeStream(InputStream is):从指定的输入流中解析、创建Bitmap对象。

如:

获取本地图片创建bitmap:

 bmp=BitmapFactory.decodeResource(this.getResources(),R.drawable.haha);

    当然可以获取网络图片创建bitmap:

 URL conurl = new URL(url);
      	HttpURLConnection con = (HttpURLConnection) conurl.openConnection();
	bmp = BitmapFactory.decodeStream(con.getInputStream());

此外附加学习:

android 获取资源文件 r.drawable中的图片转换为drawable、bitmap

1、

Resources resources = mContext.getResources();
Drawable drawable = resources.getDrawable(R.drawable.a);
imageview.setBackground(drawable);

2、

Resources r = this.getContext().getResources();
Inputstream is = r.openRawResource(R.drawable.my_background_image);
BitmapDrawable  bmpDraw = new BitmapDrawable(is);
Bitmap bmp = bmpDraw.getBitmap();

3、

Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);

Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 ); 

4、

InputStream is = getResources().openRawResource(R.drawable.icon);  

Bitmap mBitmap = BitmapFactory.decodeStream(is);


   转自http://www.2cto.com/kf/201307/226978.html

http://cfanz.cn/index.php?c=article&a=read&id=141146

保存学习记录

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值