美文网首页android内存管理
glide加载圆角图片内存抖动

glide加载圆角图片内存抖动

作者: 朱_c713 | 来源:发表于2020-05-08 17:32 被阅读0次

    用了一个网络上的圆角图片加载的工具加载圆角,事实上证明这种方式很坑。

    /**

    • glide Round corner function have conflict with the centercrop attribute

    • @author Administrator
      */
      public class GlideRoundTransform extends BitmapTransformation {

      private static float radius = 0f;

      public GlideRoundTransform() {
      this(4);
      }

      public GlideRoundTransform(int dp) {

       radius = Resources.getSystem().getDisplayMetrics().density * dp;
      

      }

      @Override
      protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
      Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
      return roundCrop(pool, 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);
       if (result == null) {
           result = Bitmap.createBitmap(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;
      

      }

      public String getId() {
      return getClass().getName() + Math.round(radius);
      }

      @Override
      public void updateDiskCacheKey(MessageDigest messageDigest) {

      }

    }

    正确的方案:采用控件定义的方式。
    可以看到这种方式创建了很多bitmap对象。尤其是当加载图片放在adapter的convert相关的方法里面的时候。

    相关文章

      网友评论

        本文标题:glide加载圆角图片内存抖动

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