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

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

How to make your view transparent / display only the drawing in Android Studio?

问题

我有一个名为DrawView的类,它继承自View,并位于一个TableLayout的上方。我使用这个类来使用drawLine()方法绘制一条简单的线。我的TableLayout被覆盖,因为我的DrawView类位于其上方。我如何防止这种情况发生?我的线条需要位于TableLayout的上方,但该线条并未使用整个画布,所以我认为将背景设置为透明会解决问题,但事实并非如此。

  1. import android.content.Context;
  2. import android.graphics.Canvas;
  3. import android.graphics.Color;
  4. import android.graphics.Paint;
  5. import android.util.AttributeSet;
  6. import android.view.View;
  7. public class DrawView extends View {
  8. // ... (代码省略,与上方内容相同)
  9. }

这个视图在调用下面的方法之前也是不可见的:

  1. public void displayResult(int result) {
  2. // TODO: 修复 DrawView 覆盖了 TableLayout 的问题
  3. DrawView drawView = new DrawView(this);
  4. //drawView.setBackgroundColor(Color.TRANSPARENT);
  5. drawView.setVisibility(View.VISIBLE);
  6. /*...*/
  7. }

但它仍然会覆盖我的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.

  1. import android.content.Context;
  2. import android.graphics.Canvas;
  3. import android.graphics.Color;
  4. import android.graphics.Paint;
  5. import android.util.AttributeSet;
  6. import android.view.View;
  7. public class DrawView extends View {
  8. private final Paint paint = new Paint();
  9. private static int orientation;
  10. private static int pos;
  11. public static final int HORIZONTAL = 1;
  12. public static final int VERTICAL = 2;
  13. public static final int DIAGONAL = 3;
  14. public static final int TOP_LEFT = 4;
  15. public static final int TOP_RIGHT = 5;
  16. private void init() {
  17. paint.setColor(Color.BLACK);
  18. paint.setStrokeWidth(25f);
  19. paint.setAntiAlias(true);
  20. }
  21. public DrawView(Context context) {
  22. super(context);
  23. init();
  24. }
  25. public DrawView(Context context, AttributeSet attrs) {
  26. super(context, attrs);
  27. init();
  28. }
  29. public DrawView(Context context, AttributeSet attrs, int defStyle) {
  30. super(context, attrs, defStyle);
  31. init();
  32. }
  33. public static void setOrientation(int newOrientation) {
  34. orientation = newOrientation;
  35. }
  36. public static void setPosition(int newPosition) {
  37. pos = newPosition;
  38. }
  39. @Override
  40. public void onDraw(Canvas canvas) {
  41. final int INDENT = 147;
  42. canvas.drawColor(Color.TRANSPARENT);
  43. switch (orientation) {
  44. case HORIZONTAL:
  45. switch (pos) {
  46. case 0:
  47. canvas.drawLine(10, INDENT, getWidth() - 10, INDENT, paint);
  48. break;
  49. case 1:
  50. canvas.drawLine(10, INDENT * 3, getWidth() - 10, INDENT * 3, paint);
  51. break;
  52. case 2:
  53. canvas.drawLine(10, INDENT * 5, getWidth() - 10, INDENT * 5, paint);
  54. break;
  55. }
  56. break;
  57. case VERTICAL:
  58. switch (pos) {
  59. case 0:
  60. canvas.drawLine(INDENT, 10, INDENT, getHeight() - 10, paint);
  61. break;
  62. case 1:
  63. canvas.drawLine(INDENT * 3, 10, INDENT * 3, getHeight() - 10, paint);
  64. break;
  65. case 2:
  66. canvas.drawLine(INDENT * 5, 10, INDENT * 5, getHeight() - 10, paint);
  67. break;
  68. }
  69. break;
  70. case DIAGONAL:
  71. if (pos == TOP_LEFT) {
  72. canvas.drawLine(10, 10, getWidth() - 10, getHeight() - 10, paint);
  73. } else if (pos == TOP_RIGHT) {
  74. canvas.drawLine(10, getHeight() - 10, getWidth() - 10, 10, paint);
  75. }
  76. break;
  77. }
  78. }
  79. }

This view is also invisible, until I call this method:

  1. public void displayResult(int result) {
  2. // TODO: Fix board being overdrawn by DrawView
  3. DrawView drawView = new DrawView(this);
  4. //drawView.setBackgroundColor(Color.TRANSPARENT);
  5. drawView.setVisibility(View.VISIBLE);
  6. /*...*/
  7. }

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:

确定