• 自定义View 圆角的ImageView


    第一步:复制下边的Java文件到你的项目

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Path;
    import android.os.Build;
    import android.util.AttributeSet;
    import android.util.TypedValue;
    import android.view.View;
    
    import androidx.appcompat.widget.AppCompatImageView;public class RoundImageView extends AppCompatImageView {
    
        private float width, height;
    
        public RoundImageView(Context context) {
            this(context, null);
        }
    
        public RoundImageView(Context context, int defultRadiusDp) {
            this(context, null);
            this.radius = dip2px(context, defultRadiusDp);
        }
    
        public RoundImageView(Context context, float defultRadius) {
            this(context, null);
            this.radius = defultRadius;
        }
    
        public RoundImageView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            if (Build.VERSION.SDK_INT < 18) {
                setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            }
            getAttribute(context, attrs, defStyleAttr);
        }
    
        private float radius = 12;
    
        private void getAttribute(Context context, AttributeSet attrs, int defStyleAttr) {
            //获取自定义属性。
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
            if (typedArray != null) {
                radius = typedArray.getDimension(R.styleable.RoundImageView_round_img_radius, 12);
                typedArray.recycle();
                return;
            }
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
            width = getWidth();
            height = getHeight();
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
    
            if (width > radius && height > radius) {
                Path path = new Path();
                path.moveTo(radius, 0);
                path.lineTo(width - radius, 0);
                path.quadTo(width, 0, width, radius);
                path.lineTo(width, height - radius);
                path.quadTo(width, height, width - radius, height);
                path.lineTo(radius, height);
                path.quadTo(0, height, 0, height - radius);
                path.lineTo(0, radius);
                path.quadTo(0, 0, radius, 0);
                canvas.clipPath(path);
            }
    
            super.onDraw(canvas);
        }
    
        public void setRadius(float radiusPx) {
            this.radius = radius;
            this.requestLayout();
        }
    
        private int dip2px(Context context, int dp) {
            float pxDimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
            return (int) pxDimension;
        }
    
    }

    第二步:在 “res/values/attrs”目录下写入代码:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        
        <declare-styleable name="RoundImageView" >
            <attr name="round_img_radius" format="dimension"/>
        </declare-styleable>
    
    </resources>

    第三步:如何使用

     第1种最常用的:

    <com.z.z.view.RoundImageView
            android:id="@+id/main_rv_item_img"
            android:layout_width="170dp"
            android:layout_height="170dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/list_item_one"
            app:round_img_radius="2dp"/>

    第2种:方式

     RoundImageView imageView = new RoundImageView(parent.getContext(),4px);

    第3种:动态修改圆角

    RoundImageView.setRadius(4px)
  • 相关阅读:
    1074 食物链 (并查集)
    2832 6个朋友
    病毒 (拓扑)
    4735 烦人的幻灯片 (拓扑)
    JavaScript中变量的LHS引述和RHS引用
    td自动换行
    SQL Server 中的 NOLOCK 到底是什么意思?
    jQuery中遇到的坑
    jQuery中attr()函数 VS prop()函数
    Javascript刷新页面的几种方法
  • 原文地址:https://www.cnblogs.com/Oldz/p/13825742.html
Copyright © 2020-2023  润新知