如何使视图透明/仅显示绘图在Android Studio中?

huangapple go评论82阅读模式
英文:

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的下方,我的布局如下所示,可以猜到不会显示任何线条:

如何使视图透明/仅显示绘图在Android Studio中?

如果DrawView位于TableLayout的上方,我的布局如下所示,这样我就得到了一个带有白色背景的线条,完全覆盖了我的TableLayout

如何使视图透明/仅显示绘图在Android Studio中?

我如何确保只绘制线条,而没有线条的白色背景?

英文:

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:

如何使视图透明/仅显示绘图在Android Studio中?

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:

如何使视图透明/仅显示绘图在Android Studio中?

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.

huangapple
  • 本文由 发表于 2020年10月2日 17:38:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64169385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定