android - 多个矩形不在 Canvas 上绘制

标签 android android-canvas drawrect family-tree

我正在尝试在 android 中创建类似结构的家谱。我正在使用 Canvas 绘制家庭成员姓名和连接线的矩形和线。

我在 link 的帮助下通过以下方法绘制矩形和直线

绘图 View .java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

public class DrawView  extends View {
    Paint paint = new Paint();
    float mx,  my,  mdensity;
    Paint mBGPaint, mTXTPaint,mLINEPaint,mBRDPaint;
    String text;
    public DrawView(Context context, float x, float y, float density, String text) {
        super(context);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(8);
        paint.setStyle(Paint.Style.STROKE);

        mx = x;
        my = y;
        mdensity = density;
        this.text = text;
    }
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        init();

        mLINEPaint.setStrokeWidth(8);

        //draw rect border
        canvas.drawRect(100, 100, 200, 200, mBRDPaint);
//        //draw text
        canvas.drawText(text, 150, 150, mTXTPaint);
//        //draw line

        float x = mx+150;

        canvas.drawLine(x, 10, x, 100, mLINEPaint);

    }
    public void init() {

        //rectangle background
        mBGPaint = new Paint();
        mBGPaint.setColor(Color.parseColor("#80123456"));

        //your text
        mTXTPaint = new Paint();
        mTXTPaint.setColor(Color.parseColor("#123456"));

        //your line
        mLINEPaint = new Paint();
        mLINEPaint.setColor(0xFFFF00FF);

        //rectangle border
        mBRDPaint = new Paint();
        mBRDPaint.setStyle(Paint.Style.STROKE);
        mBRDPaint.setStrokeWidth(10);
        mBRDPaint.setColor(Color.parseColor("#80123456"));
    }
}

现在我想在 LinearLayout 中添加多个 View ,水平方向如下:

  float density = getApplicationContext().getResources().getDisplayMetrics().density;
  DrawView drawView;

  float x = 100, y = 200;
  int count1 = 1;
  int id;
  LinearLayout  layout2 = new LinearLayout(this);

  layout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  layout2.setOrientation(LinearLayout.HORIZONTAL);

  main_layout.addView(layout2);

  DrawView drawView1;
  CircleView circleView;
  for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {

      String key = entry.getKey();
      if (count1 < 2) {
          x = dirButton.getX();
          y = dirButton.getY();
      }
      drawView1 = new DrawView(this, x, y, density, key);
      drawView1.setId(butId++);
      drawView1.setLayoutParams(params);
      layout2.addView(drawView1);

      count1++;
      x = x + 100;
  }

但是当我这样做时,只有一个 View 被添加到 Canvas 中,而其他 View 则不可见。我没有在 android 中使用 Canvas 的经验,如果有人能指导我解决这个问题,我会很高兴。

最佳答案

我试过你的项目,但它太宽泛了,无法在答卷上编辑。我必须建议看看这些:

  1. Multiple rect.
  2. Rectangle with view

关于android - 多个矩形不在 Canvas 上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39435327/

相关文章:

ios - 重写drawRect(_ :) in UITableViewHeaderFooterView results in black background

objective-c - 将按钮(UIButton)添加到使用 drawRect : 绘制的自定义 tableview 单元格

java - 设置适配器空指针

java - 进入 Debug模式后 Eclipse 会阻止编辑

android - 第二台显示器上的 AVD 崩溃

android - 一般Android半透明bug

Android - ListView - 默认列表项(状态)背景颜色

android - 如何在 Kotlin 中绘制矩形?

java - Canvas 如何确定其裁剪范围?

iOS - 无法更改 View 原点