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

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

 

具体方法如下

   //圆角方法
        @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);

        }

 

posted @ 2019-06-03 17:28  丁先森  阅读(4267)  评论(0编辑  收藏  举报