4.Glide使用之圖片佔位及過渡動畫
阿新 • • 發佈:2019-02-10
重點內容
為了更好的使用者體驗,我們在開發的時候一般會給圖片放置一張載入過程的圖片。一般載入圖片大部分都是網路圖片,載入速度取決於使用者所在的環境,最好的方式是載入時顯示一個佔位圖片,成功之後展示正確的圖片。
Glide使用圖片佔位placeholder()(placeholder()的引數是drawable/mipmap資源的引用)
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.placeholder(R.mipmap.ic_launcher) // can also be a drawable
.into (imageViewPlaceholder);
當然,你不能設定一個網路圖片的URL作為佔位符,因為那也需要載入,應用的資源和drawable要保證是可用可訪問的。當然,不能載入或載入失敗也可以設定佔位圖
Glide使用錯誤圖片佔位error() (注:error()引數為drawable/mipmap資源)
Glide
.with(context)
.load("http://futurestud.io/non_existing_image.png")
.placeholder(R.mipmap.ic_launcher) // can also be a drawable
.error (R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
.into(imageViewError);
Glide載入圖片使用漸入動畫crossFade() 預設使用漸變動畫
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.placeholder(R.mipmap.ic_launcher) // can also be a drawable
.error (R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
.crossFade()
.into(imageViewFade);
crossFade()方法還有一個過載方法:crossFade(int duration),毫秒單位的淡入時間,Glide預設的漸入動畫時間為300毫秒
Glide載入圖片不使用過渡動畫dontAnimate()
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.placeholder(R.mipmap.ic_launcher) // can also be a drawable
.error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
.dontAnimate()
.into(imageViewFade);
不建議這樣做,除非你有充足的理由,以上方法呼叫沒有依賴關係,你可以任意設定呼叫