RecyclerView 刷新Item图片闪烁

RecyclerView 刷新Item图片闪烁

在项目开发过程中,我们可能会用到RecyclerView来加载列表数据。而数据又离不开需要加载图片。
我们在加载图片的框架时,常用的也是glide picasso等等。而且有些UI设计图为圆角的样式。

比如这样子的布局,左部分就是圆角的样式
在这里插入图片描述
对于这样的图片,我们一般情况下有几个解决方法
1、自定义ImageView来实现圆角效果
2、用图片加载控件来控制(本来是用Glide为例来说明)

说到Glide,网上已经有很多文章来说了,一般我们在实际项目中这个库也是用的比较多的

引用Glide库(用的版本是4.7.1)

   api 'com.github.bumptech.glide:glide:4.7.1'
   annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'

一般调用方法(此方法加载出来的图片为直角)

 Glide.with(context)
      .asBitmap()
      .load(imgUrl)
      .apply(new RequestOptions()
      .placeholder(R.mipmap.img_default_icon) 
      .error(R.mipmap.img_default_icon)
      .into(imageView);

重点是来做加载圆角的图片
相比于上面的方法多了一个transforms(),,这个方法就是来设置图片圆角

 Glide.with(context)
      .asBitmap()
      .load(imgUrl)
      .apply(new RequestOptions()
      .placeholder(R.mipmap.img_default_icon)
      .error(R.mipmap.img_default_icon)
      .transforms(new CenterCrop(), new RoundCornerTransform(10))) 
      .into(imageView);

备注:R.mipmap.img_default_icon 为图片资源,你们可以根据自己的项目图片来
如果不用的话,也可以隐藏掉这2行代码
.placeholder(R.mipmap.img_default_icon)
.error(R.mipmap.img_default_icon)

自定义RoundCornerTransform 继承自BitmapTransformation

public class RoundCornerTransform extends BitmapTransformation {

    //圆角弧度
    private static float radius = 8f;
    private static final String ID = "com.package.name.RoundCornerTransform";
    private static final byte[] ID_BYTES = ID.getBytes(CHARSET);

    public RoundCornerTransform() {
        this(8);
    }

    public RoundCornerTransform(int dp) {
        super();
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }

    /**
     * 将直角的Bitmap转换为圆角的Bitmap
     */
    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) {
            return null;
        }
        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }


    /*
     *最主要的是下面3个方法:
     * 一般我们写上面的部分就可以实现圆角的效果了,但是没有解决刷新item造成图片闪烁问题。
     * 我们也可以参考Glide已经写好的RoundedCorners类
     *
     *  equals
     *  hashCode
     *  updateDiskCacheKey
     **/

    @Override
    public boolean equals(Object o) {
        if (o instanceof RoundCornerTransform) {
            RoundCornerTransform other = (RoundCornerTransform) o;
            return radius == other.radius;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Util.hashCode(ID.hashCode(),
                Util.hashCode(radius));
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
        messageDigest.update(ID_BYTES);

        byte[] radiusData = ByteBuffer.allocate(4).putInt((int)radius).array();
        messageDigest.update(radiusData);
    }
}

另外我们网上看到的解决RecyclerView刷新闪烁方法

 ((SimpleItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
 ((SimpleItemAnimator)recyclerView.getItemAnimator()).setChangeDuration(0);
Vivado2023是一款集成开发环境软件,用于设计和验证FPGA(现场可编程门阵列)和可编程逻辑器件。对于使用Vivado2023的用户来说,license是必不可少的。 Vivado2023的license是一种许可证,用于授权用户合法使用该软件。许可证分为多种类型,包括评估许可证、开发许可证和节点许可证等。每种许可证都有不同的使用条件和功能。 评估许可证是免费提供的,让用户可以在一段时间内试用Vivado2023的全部功能。用户可以使用这个许可证来了解软件的性能和特点,对于初学者和小规模项目来说是一个很好的选择。但是,使用评估许可证的用户在使用期限过后需要购买正式的许可证才能继续使用软件。 开发许可证是付费的,可以永久使用Vivado2023的全部功能。这种许可证适用于需要长期使用Vivado2023进行开发的用户,通常是专业的FPGA设计师或工程师。购买开发许可证可以享受Vivado2023的技术支持和更新服务,确保软件始终保持最新的版本和功能。 节点许可证是用于多设备或分布式设计的许可证,可以在多个计算机上安装Vivado2023,并共享使用。节点许可证适用于大规模项目或需要多个处理节点进行设计的用户,可以提高工作效率和资源利用率。 总之,Vivado2023 license是用户在使用Vivado2023时必须考虑的问题。用户可以根据自己的需求选择合适的许可证类型,以便获取最佳的软件使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值