玩轉Android Bitmap

2022-01-26 架構師必備
玩轉Android Bitmap1. 初識Bitmap

Bitmap是一個final類,因此不能被繼承。Bitmap只有一個構造方法,且該構造方法是沒有任何訪問權限修飾符修飾,也就是說該構造方法是friendly,但是谷歌稱Bitmap的構造方法是private(私有的),感覺有點不嚴謹。不管怎樣,一般情況下,我們不能通過構造方法直接新建一個Bitmap對象。
Bitmap是Android系統中的圖像處理中最重要類之一。Bitmap可以獲取圖像文件信息,對圖像進行剪切、旋轉、縮放,壓縮等操作,並可以以指定格式保存圖像文件。

2. 創建Bitmap對象

既然不能直接通過構造方法創建Bitmap,那怎樣才能創建Bitmap對象。通常我們可以利用Bitmap的靜態方法createBitmap()和BitmapFactory的decode系列靜態方法創建Bitmap對象。

3. Bitmap的顏色配置信息與壓縮方式信息

Bitmap中有兩個內部枚舉類:Config和CompressFormat,Config是用來設置顏色配置信息的,CompressFormat是用來設置壓縮方式的。


4. Bitmap對圖像進行操作1. Bitmap裁剪圖像

Bitmap裁剪圖像有兩種方式:

Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)

根據源Bitmap對象source,創建出source對象裁剪後的圖像的Bitmap。x,y分別代表裁剪時,x軸和y軸的第一個像素,width,height分別表示裁剪後的圖像的寬度和高度。
注意:x+width要小於等於source的寬度,y+height要小於等於source的高度。

Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height,Matrix m, boolean filter)

這個方法只比上面的方法多了m和filter這兩個參數,m是一個Matrix(矩陣)對象,可以進行縮放,旋轉,移動等動作,filter為true時表示source會被過濾,僅僅當m操作不僅包含移動操作,還包含別的操作時才適用。其實上面的方法本質上就是調用這個方法而已。

2. Bitmap縮放,旋轉,移動圖像

Bitmap縮放,旋轉,移動,傾斜圖像其實就是通過Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height,Matrix m, boolean filter)方法實現的,只是在實現這些功能的同時還可以實現圖像的裁剪。

Matrix的postScale和postRotate方法還有多帶兩個參數的重載方法postScale(float sx, float sy, float px, float py)和postRotate(float degrees, float px, float py),後兩個參數px和py都表示以該點為中心進行操作。
注意:雖然Matrix還可以調用postSkew方法進行傾斜操作,但是卻不可以在此時創建Bitmap時使用。

3. Bitmap保存圖像與釋放資源

5. BitmapFactory通過BitmapFactory.Options對圖像進行操作

BitmapFactory是通過BitmapFactory.Options對圖像進行操作的,然後將操作後的圖像生成Bitmap對象或者將操作後的圖像用已經存在的Bitmap保存,當不能用之保存時會返回null。
BitmapFactory.Options中常用的欄位有:


inBitmap:如果設置將會將生成的圖像內容加載到該Bitmap對象中。

inDensity:給Bitmap對象設置的密度,如果inScaled為true(這是默認的),而若inDensity與inTargetDensity不匹配,那麼就會在Bitmap對象返回前將其縮放到匹配inTargetDensity。

inDither:是否對圖像進行抖動處理,默認值是false。

inJustDecodeBounds:如果設置成true,表示獲取Bitmap對象信息,但是不將其像素加載到內存。

inPreferredConfig:Bitmap對象顏色配置信息,默認是Bitmap.Config.ARGB_8888。

inSampleSize:對圖像進行壓縮,設置的值為2的整數次冪或者接近2的整數次冪,當次設置為2時,寬和高為都原來的1/2,圖像所佔空間為原來的1/4。

inScaled:設置是否縮放。

inTargetDensity:繪製到目標Bitmap上的密度。

outHeight:Bitmap對象的高度。

outWidth:Bitmap對象的寬度。

6. 使用Bitmap時防止OOM的有效方法1. 高效壓縮圖片

2. 使用緩存

常用的緩存有內存緩存LruCache和磁碟緩存DiskLruCache。

Android界面性能調優手冊

https://androidtest.org/android-graphics-performance-pattens/?utm_medium=email&utm_source=gank.io

相關焦點

  • 最新 Android 熱門開源項目公布
    使用起來極為便捷,只需在 build.gradle 中引入依賴:dependencies {debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-2'}LeakCanary 會自動檢測 debug build
  • Android應用中使用百度地圖API定位自己的位置
    ><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >   <com.baidu.mapapi.map.MapView
  • APP測試必備技能-玩轉monkey(一)
    Monkey工具存在Android系統中,使用Java語言寫成,jar包在Android文件系統中的存放路徑是:/system/framework/monkey.jar;Monkey.jar程序是由一個名為「monkey」的Shell腳本來啟動執行,shell腳本在Android文件系統中的存放路徑是:/system/bin/monkey;monkey
  • Android系統的進程和線程
    manifest 文件中的每種組件元素(<activity>、 <service>、 <receiver> 和 <provider>)都支持 android:process 屬性,用於指定組件所屬運行的進程。
  • Android一個包含表格的圖標庫
    2.水平多柱狀圖2.1 xml布局 <wellijohn.org.varchart.hor_bar_with_line_chart.ChartLine        android:id="@+id/chartline"        android:layout_width="wrap_content"        android:layout_height
  • 玩轉Android10源碼開發定製(16)LineageOS中編譯user模式的系統
    一、安卓系統編譯選項簡介android編譯的時候可以選擇編譯選項 eng、user 和 userdebug。1.eng編譯選項(1).[上一篇]玩轉Android10源碼開發定製(15)實現跳過開機嚮導、插電源線不休眠等默認配置專注安卓系統、安卓ndk開發、安卓應用安全和逆向分析相關等IT知識分享,系統定製、frida、xposed(sandhook、edxposed)系統集成、加固、脫殼等等。
  • Android開發截屏截圖方法匯總
    view.getMeasuredWidth(), view.getMeasuredHeight());        view.setDrawingCacheEnabled(false);        view.destroyDrawingCache();        return bitmap
  • Android開發人員不得不收集的代碼
    outputStream與byteArr互轉 outputStream2Bytes、bytes2OutputStreaminputStream與string按編碼互轉 inputStream2String、string2InputStreamoutputStream與string按編碼互轉 outputStream2String、string2OutputStreambitmap
  • 【資源大全】Android開源項目庫匯總二
    - 媒體管理和圖片加載框架picasso★13268 - 安卓圖片緩存庫fresco★12543 - 在Android應用中顯示圖片PhotoView★9843 - 簡單可用的放大安卓ImageView實現CircleImageView★6779 - 圓形介紹頭像uCrop★4983 - 極限且靈活的圖像裁剪體驗android-crop
  • OpenCV3.2集成Android Studio2.2開發配置
    然後就可以加載圖像資源文件為Bitmap對象,轉換為灰度,代碼實現如下:// 獲取資源文件對應的圖像文件Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);// 傳給灰度轉換方法private void convertGray(Bitmap bitmap) {    Mat src = new Mat();    Mat dst = new Mat();    Utils.bitmapToMat(bitmap, src
  • Android Shader 實戰 各種炫酷效果的基石
    有一點要注意的是BitmapShader通過構造函數初始化設置bitmap時候,默認這個著色背景為當前bitmap的大小,可以通過setLocalMatrix去重新設置著色背景的形狀/範圍效果圖我們講一下思路,首先我們需要設置2張bitmap,一張是正序圖片,另一張是倒影的圖片(都為同張圖片),關鍵是倒影圖片設置完之後,我們需要調節一下圖片的透明度,這裡需要用到Xfermoder的知識(http://blog.csdn.net
  • Android App Shortcuts
    :name=".MainActivity"    android:label="@string/app_name"    android:screenOrientation="portrait">    <intent-filter>        <action android:name="android.intent.action.MAIN"/>        <
  • 還在用 android.support?谷歌強推 AndroidX 啦!
    :1.0.0com.android.support:designcom.google.android.material:material:1.0.0-rc01com.android.support:drawerlayoutandroidx.drawerlayout:drawerlayout:1.0.0com.android.support:gridlayout-v7androidx.gridlayout
  • Android OpenCV(二十):高斯濾波
    source.release()        noise.release()        gaussian.release()        result.release()    }    private fun showMat(view: ImageView, source: Mat) {        val bitmap
  • Android 模擬器學習
    模擬器 androidAndroid仿真器在您的計算機上模擬Android設備,這樣您就可以在各種設備和Android
  • Android 8.0 自適應圖標
    ><vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="108dp" android:height="108dp" android:viewportHeight="108" android:viewportWidth
  • Android Things 開發入門
    可以看出ats對標準android framework的支持還是挺多的,這也就保證了app開發者們可以很輕鬆的做ats的開發。刷寫完成後通電,插入顯示器,不出什麼意外就可以正常開機了至此刷寫rom的工作就完成了這裡推薦另一種更簡便的方式:使用官方提供工具的:android-things-setup-utility下載地址:https://partner.android.com/things/console/#/tools解壓完了以後如圖:
  • Android樹狀圖(checkbox)
    ><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:descendantFocusability