Glide4-입문 강좌-2-자리 표시자 사용(placeholder,error,fallback)

4065 단어 glide
디렉토리:
1, Glide4-입문 강좌-1- 환경 구성 및 기본 사용
2, Glide4-입문 강좌-2-자리 표시자 사용(placeholder,error,fallback)
1. 소개
이 절은 주로 Glide4의 비트맵 사용 방법을 말하는데 (placeholder,error,fallback) 세 가지 비트맵을 포함한다
2. 자리 표시자 정의 및 유형
1, 정의:
자리 표시자는 요청이 실행 중일 때 표시되는 Drawable입니다.요청이 성공적으로 완료되면 자리 표시자는 요청한 자원으로 대체됩니다.요청된 자원이 메모리에서 불러오면 자리 표시자가 아예 표시되지 않을 수도 있습니다.
2, 유형:
1) placeholder//그림을 요청할 때 보여주는 그림
2) error//요청이 실패했을 때 보여 주는 그림 (설정이 없으면placeholder의 자리 표시자를 보여 줍니다)
3) fallback//요청한 URL/모델이null일 때 보여주는 그림 (설정이 없으면placeholder의 자리 표시자)
3. 용법
두 가지 방법으로 설정할 수 있는데 하나는 Request Optios의 설정(중점 소개)이고, 다른 하나는 Glide App을 이용하여 직접 보여주는 것이다.아래의 실례와 같다
1,placeholder
자리 표시자는 요청이 실행 중일 때 표시되는 Drawable입니다.요청이 성공적으로 완료되면 자리 표시자는 요청한 자원으로 대체됩니다.요청된 자원이 메모리에서 불러오면 자리 표시자가 아예 표시되지 않을 수도 있습니다.요청이 실패하고 error Drawable를 설정하지 않으면 자리 표시자가 계속 표시됩니다.이와 유사하게 요청한 URL/모델이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);
}

아니면 Glide App (아래 두 가지는 Glide App 방식을 소개하지 않겠습니다.)
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/모델이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);
}

좋은 웹페이지 즐겨찾기