Glide4-入門教程-2-佔位符的使(placeholder、error、fallback)

目錄:

1、Glide4-入門教程-1-環境配置和基礎使用

2、Glide4-入門教程-2-佔位符的使(placeholder、error、fallback)

一、簡介

  這一節主要是講,Glide4中佔位圖的使用方法,包括(placeholder, error,fallback)三種佔位圖

二、佔位符定義和類型

   1、定義:

   佔位符是當請求正在執行時被展示的 Drawable 。當請求成功完成時,佔位符會被請求到的資源替換。如果被請求的資源是從內存中加載出來的,那麼佔位符可能根本不會被顯示。

  2、類型:

   1)placeholder // 正在請求圖片的時候展示的圖片

   2)error // 如果請求失敗的時候展示的圖片 (如果沒有設置,還是展示placeholder的佔位符)

   3)fallback // 如果請求的url/model爲 null 的時候展示的圖片 (如果沒有設置,還是展示placeholder的佔位符)

三、用法

  有兩種方法設置,一是RequestOptios中設置(重點介紹),二是利用GlideApp直接展示。如下面的實例

1、placeholder

  佔位符是當請求正在執行時被展示的 Drawable 。當請求成功完成時,佔位符會被請求到的資源替換。如果被請求的資源是從內存中加載出來的,那麼佔位符可能根本不會被顯示。如果請求失敗並且沒有設置 error Drawable ,則佔位符將被持續展示。類似地,如果請求的url/model爲 null ,並且 error Drawable 和 fallback 都沒有設置,那麼佔位符也會繼續顯示。

  在RequestOption設置

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     
    String imageUrl  = "https://www.niwoxuexi.com/statics/images/nougat_bg.png";
    ImageView imageView = findViewById(R.id.image_view);
    RequestOptions options = new RequestOptions()
                            .placeholder(R.drawable.ic_launcher)
                         //   .placeholder(new ColorDrawable(Color.BLACK))   // 或者可以直接使用ColorDrawable
                            ;
    Glide.with(this)
            .load(imageUrl)
            .apply(options)
            .into(imageView);
}

或者 GlideApp (下面兩種就不介紹GlideApp方式了)

GlideApp.with(this)
        .load(url)
        .placeholder(R.drawable.ic_launcher)
        //.placeholder(new ColorDrawable(Color.BLACK))        // 或者可以直接使用ColorDrawable
        .into(view);

2、error

  error Drawable 在請求永久性失敗時展示。error Drawable 同樣也在請求的url/model爲 null ,且並沒有設置 fallback Drawable 時展示。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        String imageUrl  = "https://www.niwoxuexi.com/statics/images/nougat_bg.png";
        ImageView imageView = findViewById(R.id.image_view);
        RequestOptions options = new RequestOptions()
//                .error(new ColorDrawable(Color.BLUE))   // 用ColorDrawable或者下面的資源文件
                .error(R.drawable.ic_error);
        Glide.with(this)
                .load(imageUrl)
                .apply(options)
                .into(imageView);
         
    }

3、fallback

  fallback Drawable 在請求的url/model爲 null 時展示。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        String imageUrl  = "https://www.niwoxuexi.com/statics/images/nougat_bg.png";
        ImageView imageView = findViewById(R.id.image_view);
        RequestOptions options = new RequestOptions()
//                .fallback(new ColorDrawable(Color.BLUE))   // 用ColorDrawable或者下面的資源文件
                .fallback(R.drawable.ic_fallback);
        Glide.with(this)
                .load(imageUrl)
                .apply(options)
                .into(imageView);
    }

4、最後來段三種佔位符同時用的代碼

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    String imageUrl  = "https://www.niwoxuexi.com/statics/images/nougat_bg.png";
    ImageView imageView = findViewById(R.id.image_view);
    RequestOptions options = new RequestOptions()
            .placeholder(R.drawable.ic_launcher)
            .error(R.drawable.ic_error)
            .fallback(R.drawable.ic_fallback);
    Glide.with(this)
            .load(imageUrl)
            .apply(options)
            .into(imageView);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章