创建一个自定义的View,它将绘制人脸框,并重写 onDraw 方法以绘制矩形。 下面是一个示例代码:

public class FaceBoundsView extends View {

private List faceBounds;

private Paint rectPaint;

public FaceBoundsView(Context context) {

super(context);

init();

}

public FaceBoundsView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public FaceBoundsView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

private void init() {

rectPaint = new Paint();

rectPaint.setColor(Color.GREEN);

rectPaint.setStyle(Paint.Style.STROKE);

rectPaint.setStrokeWidth(5);

}

public void setFaceBounds(List faceBounds) {

this.faceBounds = faceBounds;

invalidate();

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

if (faceBounds != null) {

for (Rect rect : faceBounds) {

canvas.drawRect(rect, rectPaint);

}

}

}

}

在Activity或Fragment中,您可以按如下方式使用此视图:

FaceBoundsView faceBoundsView = findViewById(R.id.face_bounds_view);

faceBoundsView.setFaceBounds(faceBounds);

在布局中,您可以按如下方式添加此视图

android:id="@+id/face_bounds_view"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

精彩文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: