英文:
How to make your view transparent / display only the drawing in Android Studio?
问题
我有一个名为DrawView
的类,它继承自View
,并位于一个TableLayout
的上方。我使用这个类来使用drawLine()
方法绘制一条简单的线。我的TableLayout
被覆盖,因为我的DrawView
类位于其上方。我如何防止这种情况发生?我的线条需要位于TableLayout
的上方,但该线条并未使用整个画布,所以我认为将背景设置为透明会解决问题,但事实并非如此。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DrawView extends View {
// ... (代码省略,与上方内容相同)
}
这个视图在调用下面的方法之前也是不可见的:
public void displayResult(int result) {
// TODO: 修复 DrawView 覆盖了 TableLayout 的问题
DrawView drawView = new DrawView(this);
//drawView.setBackgroundColor(Color.TRANSPARENT);
drawView.setVisibility(View.VISIBLE);
/*...*/
}
但它仍然会覆盖我的TableLayout
。
如果DrawView
位于TableLayout
的下方,我的布局如下所示,可以猜到不会显示任何线条:
如果DrawView
位于TableLayout
的上方,我的布局如下所示,这样我就得到了一个带有白色背景的线条,完全覆盖了我的TableLayout
:
我如何确保只绘制线条,而没有线条的白色背景?
英文:
I have a class named DrawView
which extends View
and is on top of a TableLayout
. I'm using this class to draw 1 simple line using the drawLine()
method. My TableLayout
is being overdrawn because my DrawView
class is on top of it. How can I prevent this? My line needs to be on top of the TableLayout
, said line isn't using the whole canvas, so I thought setting the background to transparent would fix it, which isn't the case.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DrawView extends View {
private final Paint paint = new Paint();
private static int orientation;
private static int pos;
public static final int HORIZONTAL = 1;
public static final int VERTICAL = 2;
public static final int DIAGONAL = 3;
public static final int TOP_LEFT = 4;
public static final int TOP_RIGHT = 5;
private void init() {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(25f);
paint.setAntiAlias(true);
}
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public static void setOrientation(int newOrientation) {
orientation = newOrientation;
}
public static void setPosition(int newPosition) {
pos = newPosition;
}
@Override
public void onDraw(Canvas canvas) {
final int INDENT = 147;
canvas.drawColor(Color.TRANSPARENT);
switch (orientation) {
case HORIZONTAL:
switch (pos) {
case 0:
canvas.drawLine(10, INDENT, getWidth() - 10, INDENT, paint);
break;
case 1:
canvas.drawLine(10, INDENT * 3, getWidth() - 10, INDENT * 3, paint);
break;
case 2:
canvas.drawLine(10, INDENT * 5, getWidth() - 10, INDENT * 5, paint);
break;
}
break;
case VERTICAL:
switch (pos) {
case 0:
canvas.drawLine(INDENT, 10, INDENT, getHeight() - 10, paint);
break;
case 1:
canvas.drawLine(INDENT * 3, 10, INDENT * 3, getHeight() - 10, paint);
break;
case 2:
canvas.drawLine(INDENT * 5, 10, INDENT * 5, getHeight() - 10, paint);
break;
}
break;
case DIAGONAL:
if (pos == TOP_LEFT) {
canvas.drawLine(10, 10, getWidth() - 10, getHeight() - 10, paint);
} else if (pos == TOP_RIGHT) {
canvas.drawLine(10, getHeight() - 10, getWidth() - 10, 10, paint);
}
break;
}
}
}
This view is also invisible, until I call this method:
public void displayResult(int result) {
// TODO: Fix board being overdrawn by DrawView
DrawView drawView = new DrawView(this);
//drawView.setBackgroundColor(Color.TRANSPARENT);
drawView.setVisibility(View.VISIBLE);
/*...*/
}
But it still overdraws my TableLayout
.
This is how my layout looks if the DrawView is under the TableLayout, as one can guess no line will displayed:
And this is how my layout looks if the DrawView is above the TableLayout, this way I get a line with a white background which completely overdraws my TableLayout:
How can I make sure only the line is drawn without the white background of it?
答案1
得分: 1
对不起,您的要求是只返回翻译好的内容,所以我将仅翻译您提供的文本:
出于我的愧疚,布局的背景,其中包含我的视图(DrawView
),被设置为白色。因此,我的视图也有一个白色的画布,从而覆盖了我的 TableLayout
。
虽然这可能是一个令人失望的答案,但它可能会帮助将来遇到类似问题的人。
请检查您布局的背景。
英文:
To my shame, the background of the layout, in which my view (DrawView
) is, was set to white. Therefore my view also had a white canvas, thus overdrawing my TableLayout.
While this might be an underwhelming answer, it might help someone who comes along this in the future.
Check your layout's background.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论