• Android 图片设置圆角


      Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片

           方法一:

      通过第三方框架Glide实现图片显示有圆角,有三种写法如下:

      1.1,第一种实现:

      RequestOptions options = new RequestOptions().error(R.drawable.img_load_failure).bitmapTransform(new RoundedCorners(30));//图片圆角为30

      Glide.with(this).load(URL) //图片地址

      .apply(options)

      .into(ImagView);

      1.2,第二种实现:

      RequestOptions requestOptions = new RequestOptions();

      requestOptions.placeholder(R.drawable.ic_launcher_background);

      requestOptions.circleCropTransform();

      requestOptions.transforms( new RoundedCorners(30));

      Glide.with(this).load(URL) //图片地址

      .apply(options)

      .into(ImagView);

      1.3,第三种实现:

      RequestOptions options = new RequestOptions().centerCrop() .transform(new RoundTransform(this,30));

      Glide.with(this).load(URL) //图片地址

      .apply(options)

      .into(ImagView);

      public class RoundTransform extends BitmapTransformation {

      private static float radius = 0f;

      public RoundTransform(Context context) {

      this(context, 4);

      }

      public RoundTransform(Context context, int dp) {

      super(context);

      this.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);

      } 无锡人流多少钱 http://www.bhnfkyy.com

      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) {

      }

      }

  • 相关阅读:
    ArcGIS为面要素生成邻接矩阵
    图片整理备份
    导出CityGML
    [ML] 数据处理
    微信公众号开发之access_token的全局共用
    DataReader转Dictionary数据类型之妙用
    标准化接口系统改造
    利用通用权限管理系统底层解决数据从不同库的导入导出问题
    通用权限管理系统中数据权限功能开发及使用说明
    常用API接口签名验证参考
  • 原文地址:https://www.cnblogs.com/djw12333/p/10900137.html
Copyright © 2020-2023  润新知