zoukankan      html  css  js  c++  java
  • 安卓图片可以加载任意一个角圆角

    多的不说直接上干货:

    对于Glide加载圆角图片,需要自定义一个Transformation

    /**
     * Created by 磊磊tua on 2019/5/6/006.
     * Glide加载圆角图上下左右
     */
    public class CornerTransform implements Transformation<Bitmap> {
    
        private BitmapPool mBitmapPool;
    
        private float radius;
    
        private boolean exceptLeftTop, exceptRightTop, exceptLeftBottom, exceptRightBotoom;
    
        /**
         * 除了那几个角不需要圆角的
         *
         * @param leftTop
         * @param rightTop
         * @param leftBottom
         * @param rightBottom
         */
        public void setExceptCorner(boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) {
            this.exceptLeftTop = leftTop;
            this.exceptRightTop = rightTop;
            this.exceptLeftBottom = leftBottom;
            this.exceptRightBotoom = rightBottom;
        }
    
        public CornerTransform(Context context, float radius) {
            this.mBitmapPool = Glide.get(context).getBitmapPool();
            this.radius = radius;
        }
    
        @Override
        public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
            Bitmap source =  resource.get();
            int finalWidth, finalHeight;
            float ratio; //输出目标的宽高或高宽比例
            if (outWidth > outHeight) { //输出宽度>输出高度,求高宽比
                ratio = (float) outHeight / (float) outWidth;
                finalWidth = source.getWidth();
                finalHeight = (int) ((float) source.getWidth() * ratio); //固定原图宽度,求最终高度
                if (finalHeight > source.getHeight()) { //求出的最终高度>原图高度,求宽高比
                    ratio = (float) outWidth / (float) outHeight;
                    finalHeight = source.getHeight();
                    finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
                }
            } else if (outWidth < outHeight) { //输出宽度 < 输出高度,求宽高比
                ratio = (float) outWidth / (float) outHeight;
                finalHeight = source.getHeight();
                finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
                if (finalWidth > source.getWidth()) { //求出的最终宽度 > 原图宽度,求高宽比
                    ratio = (float) outHeight / (float) outWidth;
                    finalWidth = source.getWidth();
                    finalHeight = (int) ((float) source.getWidth() * ratio);
                }
            } else { //输出宽度=输出高度
                finalHeight = source.getHeight();
                finalWidth = finalHeight;
            }
    
            //修正圆角
            this.radius *= (float) finalHeight / (float) outHeight;
            Bitmap outBitmap = this.mBitmapPool.get(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
            if (outBitmap == null) {
                outBitmap = Bitmap.createBitmap(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
            }
    
            Canvas canvas = new Canvas(outBitmap);
            Paint paint = new Paint();
            //关联画笔绘制的原图bitmap
            BitmapShader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            //计算中心位置,进行偏移
            int width = (source.getWidth() - finalWidth) / 2;
            int height = (source.getHeight() - finalHeight) / 2;
            if (width != 0 || height != 0) {
                Matrix matrix = new Matrix();
                matrix.setTranslate((float) (-width), (float) (-height));
                shader.setLocalMatrix(matrix);
            }
    
            paint.setShader(shader);
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
            canvas.drawRoundRect(rectF, this.radius, this.radius, paint); //先绘制圆角矩形
    
            if (exceptLeftTop) { //左上角不为圆角
                canvas.drawRect(0, 0, radius, radius, paint);
            }
            if (exceptRightTop) {//右上角不为圆角
                canvas.drawRect(canvas.getWidth() - radius, 0, canvas.getWidth(), radius, paint);
            }
    
            if (exceptLeftBottom) {//左下角不为圆角
                canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint);
            }
    
            if (exceptRightBotoom) {//右下角不为圆角
                canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint);
            }
    
            return BitmapResource.obtain(outBitmap, this.mBitmapPool);
        }
    
    
        @Override
        public String getId() {
            return this.getClass().getName();
        }
    }
    

      项目中的使用方法(四周圆角最后四个布尔值是控制圆角的false就是圆)

    GlideUtils.getInstance().glideLoadCornerTransform(mContext, modelImageAttach.getAttach_middle(), imageView,
                                    R.drawable.default_yulin, 6, false, false, false, false);
    

      如果你是自己的项目就这样实现(四周圆角最后四个布尔值是控制圆角的false就是圆)

       CornerTransform transformation = new CornerTransform(this, dip2px(this, 15));
            //只是绘制左上角和右上角圆角
            transformation.setExceptCorner(false, false, true, true);
         //glide3版本就使用这个方法
            Glide.with(this).
                    load(url).
                    asBitmap().
                    skipMemoryCache(true).
                    diskCacheStrategy(DiskCacheStrategy.NONE).
                    transform(transformation).
                    into(imageView);
            
            /**Glide4.0+上的使用
            RequestOptions options = new RequestOptions()
                    .error(R.drawable.ic_default)
                    .placeholder(R.drawable.ic_default)
                    .transforms(transformation);
          
            Glide.with(imageAware.getContext()).asBitmap().load(url).apply(options).into(imageView);  **/
    

      非常好用嵌入没有边距的圆角父布局也完美实现

    leileitua

  • 相关阅读:
    Excel技巧--按内容分列与合并
    Excel技巧--分类求和与空白批量填充
    Excel技巧--空白处补零
    Excel技巧--批量生成指定名称的文件夹
    Excel技巧--漏斗图让转化率直观明了
    Excel技巧--时尚的圆环比例图
    Excel技巧--让折线图带面积更直观生动
    Excel技巧--图表添加平均线为指标
    Excel技巧--使用温度计图让目标与实际对比更明显
    Linux之facl----设置文件访问控制列表(详解)
  • 原文地址:https://www.cnblogs.com/widgetbox/p/11956874.html
Copyright © 2011-2022 走看看