Android 代码切圆角

今天有个需求,控件的颜色是后台控制的,但是控件本身是个圆角,这时候就不能使用xml了

需要在代码中设置

先上主要代码

DisplayMetrics appDisplayMetrics = mContext.getResources().getDisplayMetrics();
float targetDensity = appDisplayMetrics.widthPixels / 375f;
int px=(int)(26*targetDensity);
float[] outerRadian = new float[]{px, px, px, px, px, px, px, px};
// 内部矩形与外部矩形的距离  
RectF inset = new RectF(100, 100, 50, 50);
// 内部矩形弧度  
float[] innerRadii = new float[] { 20, 20, 20, 20, 20, 20, 20, 20 };
RoundRectShape roundRectShape = new RoundRectShape(outerRadian, null, null);
ShapeDrawable drawable = new ShapeDrawable(roundRectShape);
drawable.getPaint().setColor(Color.parseColor("#b25ad8"));
drawable.getPaint().setStyle(Paint.Style.FILL);
holderView.tv_com.setBackgroundDrawable(drawable);

outerRadian 传的值是px,所以我们要自己计算,或者直接传控件的长宽值

里面的值是://左上x2,右上x2,右下x2,左下x2,注意顺序(顺时针依次设置)

猜你喜欢

转载自blog.csdn.net/qq_30711091/article/details/81565373