国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

android圖片類型之間相互轉(zhuǎn)換實(shí)現(xiàn)代碼

2020-04-11 11:23:00
字體:
供稿:網(wǎng)友

本文實(shí)例講述了android圖片類型之間相互轉(zhuǎn)換實(shí)現(xiàn)代碼。分享給大家供大家參考。具體如下:

android在處理一寫圖片資源的時(shí)候,會進(jìn)行一些類型的轉(zhuǎn)換,現(xiàn)在有空整理一下:

1、Drawable → Bitmap

Java代碼如下:

public static Bitmap drawableToBitmap(Drawable drawable) {  Bitmap bitmap = Bitmap    .createBitmap(      drawable.getIntrinsicWidth(),      drawable.getIntrinsicHeight(),      drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888        : Bitmap.Config.RGB_565);  Canvas canvas = new Canvas(bitmap);  //canvas.setBitmap(bitmap);  drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  drawable.draw(canvas);  return bitmap; } public static Bitmap drawableToBitmap(Drawable drawable) {  Bitmap bitmap = Bitmap    .createBitmap(      drawable.getIntrinsicWidth(),      drawable.getIntrinsicHeight(),      drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888        : Bitmap.Config.RGB_565);  Canvas canvas = new Canvas(bitmap);  //canvas.setBitmap(bitmap);  drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  drawable.draw(canvas);  return bitmap; } 

2、從資源中獲取Bitmap

Java代碼如下:

Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); 

3、Bitmap → byte[]

Java代碼如下:

private byte[] Bitmap2Bytes(Bitmap bm){  ByteArrayOutputStream baos = new ByteArrayOutputStream();  bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  return baos.toByteArray(); } private byte[] Bitmap2Bytes(Bitmap bm){  ByteArrayOutputStream baos = new ByteArrayOutputStream();  bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  return baos.toByteArray(); } 

4、 byte[] → Bitmap

Java代碼如下:

private Bitmap Bytes2Bimap(byte[] b){   if(b.length!=0){   return BitmapFactory.decodeByteArray(b, 0, b.length);   }   else {   return null;   } } private Bitmap Bytes2Bimap(byte[] b){   if(b.length!=0){   return BitmapFactory.decodeByteArray(b, 0, b.length);   }   else {   return null;   } }

以上是我在實(shí)踐中遇到的一些轉(zhuǎn)換,以后遇到類似的就不用到處找了。

希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 瓦房店市| 铁力市| 北宁市| 河北区| 博湖县| 澳门| 阜阳市| 霞浦县| 西华县| 论坛| 镇远县| 隆尧县| 闵行区| 长兴县| 弋阳县| 伊春市| 高淳县| 玛曲县| 屏南县| 白水县| 华安县| 昌都县| 太湖县| 青冈县| 新邵县| 安达市| 闵行区| 牙克石市| 晋州市| 沙湾县| 紫云| 永德县| 开平市| 惠水县| 云南省| 买车| 牡丹江市| 汉寿县| 南川市| 正宁县| 北宁市|