Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Drawable 圖形資源:從 res 到 SD 卡圖片與桌布設定

編輯:關於 Android 程式設計

Drawable 是 Android 圖形資源的抽象概念,BitmapDrawable、AnimationDrawable 等都與它相關。最簡單的用法是把圖片放入 res/drawable,系統會自動產生 R 引用。

從 SD 卡載入

若圖片不在專案內,可使用 Drawable.createFromPath 建立 Drawable,再設定給 ImageView。

Drawable drawable = Drawable.createFromPath("/sdcard/a.jpg");
imageView.setBackgroundDrawable(drawable);

設定桌布

使用 BitmapFactory.decodeFile 解碼圖片,再呼叫 setWallpaper。需要 SET_WALLPAPER 權限。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved