将背景颜色更改为视图的有限区域 - Android
Change background color to a limited area of a view - Android
我正在尝试使用 Google 播放服务开发一个 BarcodeReader,我想要一个像 Zxing 示例一样的相机表面背景。
实际上我用黑色透明背景和白色 Rect Canvas 查看,我希望这个 Rect 是透明的,所以我的问题是是否可以在没有这个 Rect 边界的情况下更改视图背景。
这是我的自定义视图,我在其中画了一些线
public class MyView extends View {
private Paint paint;
private Path path;
private int width, height;
Canvas canvas;
private Rect rectangle;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
this.setBackgroundColor(getResources().getColor(R.color.black60));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
drawMiddleLine();
drawLeftUpCorner();
drawLeftDownCorner();
drawRightUpCorner();
drawRightDownCorner();
drawRectSample();
}
private void drawRectSample() {
int x = width / 6;
int y = height / 4;
int widthLength = x*5;
int heightLenght = y* 3;
Paint paint = new Paint();
// create a rectangle that we'll draw later
rectangle = new Rect(x, y, widthLength, heightLenght);
paint.setStyle(Paint.Style.FILL);
paint.setColor(getResources().getColor(R.color.white50));
canvas.drawRect(rectangle, paint);
}
private void drawLeftUpCorner() {
if (width != 0 && height != 0) {
int startX = width / 6;
int startY = height / 4;
int endX = startX * 2;
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
endX = startX;
endY += startX;
canvas.drawLine(startX, startY, endX, endY, paint);
}
}
private void drawLeftDownCorner() {
if (width != 0 && height != 0) {
int startX = width / 6;
int startY = height / 4 * 3;
int endX = startX * 2;
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
endX = startX;
endY -= startX;
canvas.drawLine(startX, startY, endX, endY, paint);
}
}
private void drawRightUpCorner() {
if (width != 0 && height != 0) {
int startX = (width / 6) * 4;
int startY = height / 4;
int endX = startX + (width / 6);
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
startX = endX;
endY = startY + (width / 6);
canvas.drawLine(startX, startY, endX, endY, paint);
}
}
private void drawRightDownCorner() {
if (width != 0 && height != 0) {
int startX = (width / 6) * 4;
int startY = (height / 4) * 3;
int endX = startX + (width/ 6);
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
startX = endX;
endY -= width / 6;
canvas.drawLine(startX, startY, endX, endY, paint);
}
}
private void drawMiddleLine() {
if (width != 0 && height != 0) {
int startX = width / 3;
int startY = height / 2;
int endX = startX * 2;
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
paint.setColor(Color.GREEN);
}
}
public void setBounds(int width, int height) {
this.width = width;
this.height = height;
}
}
这是我的 xml,其中 CameraSourePreview 和 MyView(自定义视图只是为了绘制屏幕线)是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
>
<t_systems.qrlabs.camera.CameraSourcePreview
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</t_systems.qrlabs.camera.CameraSourcePreview>
<t_systems.qrlabs.barcode.MyView
android:layout_width="match_parent"
android:id="@+id/myView"
android:layout_centerInParent="true"
android:layout_height="match_parent" />
</RelativeLayout>
有什么帮助吗?谢谢。
我用反填充路径解决了
private void drawBlackBackgorund() {
int x = width / 6;
int y = height / 4;
int widthLength = x*5;
int heightLenght = y* 3;
RectF rect = new RectF(x,y,widthLength,heightLenght);
path.addRect(rect,Path.Direction.CW);
path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
canvas.clipPath(path);
canvas.drawColor(getResources().getColor(R.color.black60));
}
我正在尝试使用 Google 播放服务开发一个 BarcodeReader,我想要一个像 Zxing 示例一样的相机表面背景。
实际上我用黑色透明背景和白色 Rect Canvas 查看,我希望这个 Rect 是透明的,所以我的问题是是否可以在没有这个 Rect 边界的情况下更改视图背景。
这是我的自定义视图,我在其中画了一些线
public class MyView extends View {
private Paint paint;
private Path path;
private int width, height;
Canvas canvas;
private Rect rectangle;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
this.setBackgroundColor(getResources().getColor(R.color.black60));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
drawMiddleLine();
drawLeftUpCorner();
drawLeftDownCorner();
drawRightUpCorner();
drawRightDownCorner();
drawRectSample();
}
private void drawRectSample() {
int x = width / 6;
int y = height / 4;
int widthLength = x*5;
int heightLenght = y* 3;
Paint paint = new Paint();
// create a rectangle that we'll draw later
rectangle = new Rect(x, y, widthLength, heightLenght);
paint.setStyle(Paint.Style.FILL);
paint.setColor(getResources().getColor(R.color.white50));
canvas.drawRect(rectangle, paint);
}
private void drawLeftUpCorner() {
if (width != 0 && height != 0) {
int startX = width / 6;
int startY = height / 4;
int endX = startX * 2;
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
endX = startX;
endY += startX;
canvas.drawLine(startX, startY, endX, endY, paint);
}
}
private void drawLeftDownCorner() {
if (width != 0 && height != 0) {
int startX = width / 6;
int startY = height / 4 * 3;
int endX = startX * 2;
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
endX = startX;
endY -= startX;
canvas.drawLine(startX, startY, endX, endY, paint);
}
}
private void drawRightUpCorner() {
if (width != 0 && height != 0) {
int startX = (width / 6) * 4;
int startY = height / 4;
int endX = startX + (width / 6);
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
startX = endX;
endY = startY + (width / 6);
canvas.drawLine(startX, startY, endX, endY, paint);
}
}
private void drawRightDownCorner() {
if (width != 0 && height != 0) {
int startX = (width / 6) * 4;
int startY = (height / 4) * 3;
int endX = startX + (width/ 6);
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
startX = endX;
endY -= width / 6;
canvas.drawLine(startX, startY, endX, endY, paint);
}
}
private void drawMiddleLine() {
if (width != 0 && height != 0) {
int startX = width / 3;
int startY = height / 2;
int endX = startX * 2;
int endY = startY;
canvas.drawLine(startX, startY, endX, endY, paint);
paint.setColor(Color.GREEN);
}
}
public void setBounds(int width, int height) {
this.width = width;
this.height = height;
}
}
这是我的 xml,其中 CameraSourePreview 和 MyView(自定义视图只是为了绘制屏幕线)是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
>
<t_systems.qrlabs.camera.CameraSourcePreview
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</t_systems.qrlabs.camera.CameraSourcePreview>
<t_systems.qrlabs.barcode.MyView
android:layout_width="match_parent"
android:id="@+id/myView"
android:layout_centerInParent="true"
android:layout_height="match_parent" />
</RelativeLayout>
有什么帮助吗?谢谢。
我用反填充路径解决了
private void drawBlackBackgorund() {
int x = width / 6;
int y = height / 4;
int widthLength = x*5;
int heightLenght = y* 3;
RectF rect = new RectF(x,y,widthLength,heightLenght);
path.addRect(rect,Path.Direction.CW);
path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
canvas.clipPath(path);
canvas.drawColor(getResources().getColor(R.color.black60));
}