给任意view设置圆角outline.setRoundRect()方法实现

效果图,给banner设置圆角,1没有直接修改imageView而是使用自带方法进行设置

给任意view设置圆角outline.setRoundRect()方法实现_第1张图片

 

具体方法如下

   //圆角方法
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public void setClipViewCornerRadius(View view, final int radius) {

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                //不支持5.0版本以下的系统
                return;
            }

            if (view == null) return;

            if (radius <= 0) {
                return;
            }
            view.setOutlineProvider(new ViewOutlineProvider() {

                @Override
                public void getOutline(View view, Outline outline) {
//                    outline.setRoundRect(20, 20, view.getWidth()-40,   view.getHeight()-40, radius);
                    outline.setRoundRect(40, 0, view.getWidth()-40,   view.getHeight(), radius);//这是设置圆角的关键设置


//                    Rect rect = new Rect();
//                    view.getGlobalVisibleRect(rect);
//                    int leftMargin = 40;
//                    int topMargin = 40;
//                    Rect selfRect = new Rect(leftMargin, topMargin,
//                            rect.right - rect.left - leftMargin, rect.bottom - rect.top - topMargin);
//                    outline.setRoundRect(selfRect, 30);
                }
            });
            view.setClipToOutline(true);

        }

 

转载于:https://www.cnblogs.com/dingxiansen/p/10968894.html

你可能感兴趣的:(给任意view设置圆角outline.setRoundRect()方法实现)