處理點陣圖

試用 Compose
Jetpack Compose 是 Android 推薦的 UI 工具包。瞭解如何在 Compose 中顯示圖形。

在 Android 應用程式載入點陣圖較為困難的原因有很多:

  • 點陣圖非常容易用光應用程式的記憶體預算。舉例來說,Pixel 手機的相機最高可拍攝 4048 x 3036 像素 (1, 200 萬像素) 的相片。 如果點陣圖設定是 ARGB_8888,則 Android 2.3 (API 級別 9) 以上版本的預設值,將單張相片載入記憶體約需 48 MB 的記憶體 (4048*3036*4 位元組)。這種龐大的記憶體需求可能會立即用盡應用程式可用的所有記憶體。
  • 在 UI 執行緒載入點陣圖可能會降低應用程式效能,進而導致回應速度變慢,甚至出現 ANR 訊息。因此,使用點陣圖時必須妥善管理執行緒。
  • 如果應用程式正在將多個點陣圖載入記憶體,您必須巧妙管理記憶體和磁碟快取。否則應用程式 UI 的回應速度和流暢度可能會受到影響。

大多數情況下,我們都建議您使用 Glide 程式庫在應用程式中擷取、解碼,並顯示點陣圖。Glide 在處理上述作業,以及其他在 Android 中使用點陣圖和其他圖片的相關任務時,大多能將複雜之處化繁為簡。如要瞭解如何使用和下載 Glide,請造訪 GitHub 的 Glide 存放區

您也可以選擇直接使用 Android 架構內建較低層級的 API。詳情請參閱「有效率地載入大型點陣圖」、「快取點陣圖」和「管理點陣圖記憶體」。

其他資源

Understand the internals of JPG to get the most out of this file format.

One of the interesting parts about building an Android application is all the awesome configurations of screen sizes and form factors. And if you’re a developer who prides themselves on being available on the most devices possible, then chances are

Learn about WebP, which can give you even smaller image sizes than JPEG.

Keep your JPGs lean by stripping those unnecessary bits.