美文网首页
Glide4.8 使用TextView的drawableTop

Glide4.8 使用TextView的drawableTop

作者: suniney | 来源:发表于2018-12-13 17:54 被阅读42次

4.8以后SimpleTarget不推荐使用了,选择CustomVIewTarget,但是包含东西太多,自定义一个Target用于加载TextView的drawable

public class TextViewDrawableTarget extends CustomViewTarget<TextView, Drawable> {
    /**
     * Constructor that defaults {@code waitForLayout} to {@code false}.
     *
     * @param view
     */
    public TextViewDrawableTarget(@NonNull TextView view) {
        super(view);
    }

    @Override
    protected void onResourceCleared(@Nullable Drawable placeholder) {

    }

    @Override
    public void onLoadFailed(@Nullable Drawable errorDrawable) {
        //加载失败例如url=null,此时使用 fallback不生效
         view.setCompoundDrawablesWithIntrinsicBounds(null, mContext.getDrawable(R.mipmap.ic_boy), null, null);
    }

    @Override
    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
        view.setCompoundDrawablesWithIntrinsicBounds(null, resource, null, null);
    }
}

//使用
  GlideApp.with(this)
                .load(url)
                .into(new TextViewDrawableTarget(tvAssociateName));

相关文章

网友评论

      本文标题:Glide4.8 使用TextView的drawableTop

      本文链接:https://www.haomeiwen.com/subject/nuvbhqtx.html