日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Android Bitmap开发之旅--基本操作

發(fā)布時間:2025/3/15 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Bitmap开发之旅--基本操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1 Bitmap加載方式

在介紹Bitmap--OOM 異常時,首先介紹一下Bitmap有哪幾種加載方式。通常Bitmap的加載方式有Resource資源加載、本地(SDcard)加載、網(wǎng)絡(luò)加載等加載方式。

1.1 Resource資源加載

  • Assets資源加載方式:
    [java]?view plaincopy
  • AssetManager?am?=?getAssets();??
  • InputStream?is?=?am.open("high_pixel_img.jpg");??
  • Bitmap?bitmap?=?BitmapFactory.decodeStream(is);??
  • ?Res資源加載方式:
    [java]?view plaincopy
  • Bitmap?bitmap?=?BitmapFactory.decodeResource(getResources(),?R.drawable.high_pixel_img);??
  • 1.2 本地(SDcard)加載

    [java]?view plaincopy
  • String?file_name?=?Environment.getExternalStorageDirectory().toString()+"/"+"high_pixel_img.jpg";??
  • nbsp;Bitmap?bitmap?=?BitmapFactory.decodeFile(file_name);??
  • 文件描述符

    1.3 網(wǎng)絡(luò)加載

    注意:網(wǎng)絡(luò)加載圖片的時候必須在非主線成中操作

    [java]?view plaincopy
  • String?website?=?"http://www.baidu.com/img/baidu_sylogo1.gif";??
  • URL?image_url?=?new?URL(website);??
  • HttpURLConnection?conn?=?(HttpURLConnection)?image_url.openConnection();??
  • conn.connect();??
  • InputStream?is?=?conn.getInputStream();??
  • bitmap?=?BitmapFactory.decodeStream(is);??
  • 2 Bitmap | Drawable | InputStream | Byte[ ] 之間進行轉(zhuǎn)換

    2.1 Drawable轉(zhuǎn)化成Bitmap

    [java]?view plaincopy
  • Drawable?drawable?=?getResources().getDrawable(R.drawable.ic_launcher);??
  • 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);??
  • 2. 2 Bitmap轉(zhuǎn)換成Drawable

    ?

    [java]?view plaincopy
  • Drawable?drawable?=?new?BitmapDrawable(bitmap);??
  • ?

    2.3 Bitmap轉(zhuǎn)換成byte[]

    [java]?view plaincopy
  • Bitmap?bitmap?=?BitmapFactory.decodeResource(getResources(),?R.drawable.high_pixel_img);??
  • ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream();??
  • bitmap.compress(Bitmap.CompressFormat.PNG,?100,?baos);??
  • byte[]?info?=?baos.toByteArray();??
  • 2.4 byte[]轉(zhuǎn)換成Bitmap

    ?

    [java]?view plaincopy
  • Bitmap?bitmap?=?BitmapFactory.decodeByteArray(byte,?0,?b.length);??
  • 2.5 InputStream轉(zhuǎn)換成Bitmap

    [java]?view plaincopy
  • InputStream?is??=?getResources().openRawResource(id);??
  • Bitmap?bitmap?=?BitmaoFactory.decodeStream(is);??
  • 2.6 InputStream轉(zhuǎn)換成byte[]

    ?

    [java]?view plaincopy
  • InputStream?is?=?getResources().openRawResource(id);//也可以通過其他方式接收一個InputStream對象??
  • ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream();????
  • byte[]?b?=?new?byte[1024*2];????
  • int?len?=?0;????
  • while?((len?=?is.read(b,?0,?b.length))?!=?-1)????
  • {????
  • ???baos.write(b,?0,?len);????
  • ???baos.flush();????
  • }????
  • byte[]?bytes?=?baos.toByteArray();????
  • 3 轉(zhuǎn)換Bitmap大小

    ?

    [java]?view plaincopy
  • Bitmap?bitmap?=?BitmapFactory.decodeResource(getResources(),?R.drawable.high_pixel_img);??
  • Bitmap?target?=?Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);??
  • Canvas?canvas?=?new?Canvas(tartget);??
  • canvas.scale(scale,scale);??
  • Paint?paint?=?new?Pain(Paint.FILTER_BITMAP_FLAG?|?Paint.DITHER_FLAG);??
  • canvas.drawBitmap(bitmap,0,0,paint);??
  • bitmap.recycle();??
  • ?

    ?

    4 將Bitmap保存為本地文件

    ?

    [java]?view plaincopy
  • String?filename?=?"save.jpg";??
  • File?file?=?new?File(Environmnet.getExternalStorageDirectory,filename);??
  • try{??
  • ????OutputStream?os??=??new?FileOutputString(file);??
  • ????bitmap.compress(CompressFormat.JPEG,100,os);??
  • }catch(FileNotFoundException?e){??
  • ???e.printStackTrace();??
  • }??

  • 以上是關(guān)于Bitmap的相關(guān)操作,如果大家在閱讀中發(fā)現(xiàn)有什么問題,請在評論中留言。

    轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/5544986.html

    總結(jié)

    以上是生活随笔為你收集整理的Android Bitmap开发之旅--基本操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。