1: 参考:https://www.cnblogs.com/everhad/p/6161083.html
package kodulf.swiperefreshlayoutrecyclerviewdemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.LinearLayout;
/**
* Created by Kodulf on 2019/1/12.
*/
public class MySecondLinearLayout extends LinearLayout {
public MySecondLinearLayout(Context context) {
this(context, null);
}
public MySecondLinearLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MySecondLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
// 四个角的x,y半径
private float[] radiusArray = { 50f, 50f, 50f, 50f, 0f, 0f, 0f, 0f };
@Override
public void draw(Canvas canvas) {
//创建bitmap
final Bitmap composedBitmap;
final Bitmap originalBitmap;
//创建画布
final Canvas composedCanvas;
final Canvas originalCanvas;
final Paint paint;
final int height;
final int width;
width = getWidth();
height = getHeight();
//ARGB_4444 代表16位Alpha的位图
//ARGB_8888 代表32位ARGB位图
composedBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
originalBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
composedCanvas = new Canvas(composedBitmap);
originalCanvas = new Canvas(originalBitmap);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
super.draw(originalCanvas);
composedCanvas.drawARGB(0, 0, 0, 0);
Bitmap bitmapFrame= makeRoundRectFrame(width, height);
composedCanvas.drawBitmap(bitmapFrame, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
composedCanvas.drawBitmap(originalBitmap, 0, 0, paint);
canvas.drawBitmap(composedBitmap, 0, 0, new Paint());
}
private Bitmap makeRoundRectFrame(int w, int h) {
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
Path path = new Path();
path.addRoundRect(new RectF(0, 0, w, h), radiusArray, Path.Direction.CW);
Paint bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
bitmapPaint.setColor(Color.GREEN); // 颜色随意,不要有透明度。
c.drawPath(path, bitmapPaint);
return bm;
}
}
方法2: 参考:https://blog.csdn.net/qq_33453910/article/details/78616447
package kodulf.swiperefreshlayoutrecyclerviewdemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.LinearLayout;
/**
* Created by Kodulf on 2019/1/12.
*/
public class MyLinearLayout extends LinearLayout {
private final float density = getContext().getResources().getDisplayMetrics().density;
private float roundness;
private static final int DEFAULT_RECT_ROUND_RADIUS = 20;
public MyLinearLayout(Context context) {
this(context, null);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取attr文件下,名为RoundedCornerImageView
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.mView, defStyleAttr, 0);
//获取值
roundness = ta.getDimensionPixelSize(R.styleable.mView_round_radius, DEFAULT_RECT_ROUND_RADIUS);
ta.recycle();
}
@Override
public void draw(Canvas canvas) {
//创建bitmap
final Bitmap composedBitmap;
final Bitmap originalBitmap;
//创建画布
final Canvas composedCanvas;
final Canvas originalCanvas;
final Paint paint;
final int height;
final int width;
width = getWidth();
height = getHeight();
//ARGB_4444 代表16位Alpha的位图
//ARGB_8888 代表32位ARGB位图
composedBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
originalBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
composedCanvas = new Canvas(composedBitmap);
originalCanvas = new Canvas(originalBitmap);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
super.draw(originalCanvas);
composedCanvas.drawARGB(0, 0, 0, 0);
//指定RectF对象以及圆角半径来实现,该方法是绘制圆形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆形
//thi,roundness 分别是x,y方向的圆角半径
composedCanvas.drawRoundRect(new RectF(0, 0, width, height),
this.roundness, this.roundness, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
composedCanvas.drawBitmap(originalBitmap, 0, 0, paint);
canvas.drawBitmap(composedBitmap, 0, 0, new Paint());
}
}
values文件夹里面
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="mView">
<attr name="round_radius" format="dimension" />
<attr name="round_color" format="color" />
<attr name="text_color" format="color" />
<attr name="text_size" format="dimension" />
</declare-styleable>
</resources>
attr文件: