Android banner配合glide设置圆角无效及缩放问题

    implementation 'com.youth.banner:banner:1.4.9'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
private void initView(){
      Banner  banner = findViewById(R.id.banner);
      banner.setImageLoader(new MyLoader());
      banner.setDelayTime(3000);
      banner.isAutoPlay(true);
      banner.setImages(bannerList);
      banner.start();
}              


private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            HomeBannerInfo info = (HomeBannerInfo) path;
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            CornerTransform transformation = new CornerTransform(mContext, DensityUtil.dip2px(10, mContext));
            transformation.setExceptCorner(false, false, false, false);
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.placeholder(R.drawable.loading_banner);
            requestOptions.error(R.drawable.load_fail_banner);
            requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
            Glide.with(mContext)
                    .load(new mGlideUrl(info.getImageUrl()))
                    .apply(requestOptions)
                    .into(imageView);
        }
    }

    public class mGlideUrl extends GlideUrl {

        private String mUrl;

        // 构造函数里 传入 带有token参数的图片Url地址
        public mGlideUrl(String url) {
            super(url);
            mUrl = url;
        }

        // 2. 重写getCacheKey()
        @Override
        public String getCacheKey() {
            return mUrl.replace(deleteToken(), "");
            // 通过 deleteToken() 从 带有token参数的图片Url地址中 去掉 token参数
            // 最终返回一个没有token参数、初始的图片URL地址
            // ->>分析1
        }

        // 分析1:deleteToken()
        private String deleteToken() {
            String tokenParam = "";
            int tokenKeyIndex = mUrl.indexOf("?token=") >= 0 ? mUrl.indexOf("?token=") : mUrl.indexOf("&token=");
            if (tokenKeyIndex != -1) {
                int nextAndIndex = mUrl.indexOf("&", tokenKeyIndex + 1);
                if (nextAndIndex != -1) {
                    tokenParam = mUrl.substring(tokenKeyIndex + 1, nextAndIndex + 1);
                } else {
                    tokenParam = mUrl.substring(tokenKeyIndex);
                }
            }
            return tokenParam;
        }

    }

你可能感兴趣的:(Android banner配合glide设置圆角无效及缩放问题)