【问题标题】:Multiple rectangles not drawing on canvas多个矩形不在画布上绘制
【发布时间】:2017-01-19 00:09:36
【问题描述】:

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

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

DrawView.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 中添加多个视图,其方向为水平方向,如下所示:

  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;
  }

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

【问题讨论】:

  • 您的画布应该在视图内,您可以通过 onDraw() 方法在画布上绘制所有内容。您不应该在画布上需要多个视图。每当您必须更新画布时,您都可以发出 invalidate() 调用,该调用将触发 onDraw()。
  • 我认为两个矩形都是在相同的 xy 上绘制的。你能检查一下,或者使一个视图矩形比另一个大,并且矩形有不同的颜色,只是为了检查两个矩形是否都被绘制跨度>
  • 不要在onDraw中打电话给你的init()

标签: android android-canvas drawrect family-tree


【解决方案1】:

我尝试处理您的项目,但它过于宽泛,无法在答题纸上进行编辑。我必须建议看看这些:

  1. Multiple rect.
  2. Rectangle with view

【讨论】:

    【解决方案2】:
      if (count1 < 2) {
          x = dirButton.getX();
          y = dirButton.getY();
      }
    

    从上面的代码行中,您设置了执行该行的条件。

    并使用 if 语句。

         int count1 = 1;  //Count was initialized to 1
    

    这使得代码在第一次调用时进入 if 语句

        count1++;
    

    这一行将 count 的值增加到 2,因此 if 块不再执行...

    并且y值永远不会改变导致覆盖。

    你可能错过的是 y 的常规增量

       y+=something;
    

    希望有帮助

    【讨论】:

      【解决方案3】:

      请检查我是如何做到的, 你可以从here form myapp 查看它是如何工作的

      //我如何调用绘制矩形

      这是 SDV.class

      public static boolean isDrawing = false;
      
      public static float mStartX;
      public static float mStartY;
      
      public static float mx;
      public static float my;
      
      public static void Shape14(float x, float y, float radius) {
          Path path = new Path();
          y -= 2 * radius;
          radius *= 2;
          path.moveTo(x + radius, y + radius);
          path.lineTo(x - radius, y + radius);
          path.lineTo(x, y);
          path.lineTo(x + radius, y + radius);
          float div = (2 * radius) / 5;
          float top = y + radius;
          RectF rect1 = new RectF(x + radius / 4, y, x + radius / 1.9f, y
                  + radius);
          RectF rect2 = new RectF(x + div / 2, top, x + div / 2 + div, top + div
                  * 2);
          RectF rect3 = new RectF(x - div / 2 - div, top, x - div / 2, top + div
                  * 2);
          RectF rect4 = new RectF(x - div / 2, top, x + div / 2, top + div);
      
          HashMap<String, Object> hm = new HashMap<String, Object>();
          hm.put("type", shape14);
          hm.put("paint", new Paint(DrawingView.mColorPaint));
          hm.put("path", path);
      
          hm.put("rect1", rect1);
          hm.put("rect2", rect2);
          hm.put("rect3", rect3);
          hm.put("rect4", rect4);
          al.add(hm);
          Gmap.mDrawingView.invalidate();
      }
      

      这是我们的观点,

      public class DrawingView extends View {
      
      public static Paint mPaint;
      public static int mCurrentShape;
      Point p1, p2, p3, p4;
      
      public static Paint mDotedPaint;
      public static Paint mColorPaint;
      
      GoogleMap googleMap;
      SeekBar sbWidth;
      public static float sx, sy;
      
      public DrawingView(Context context, GoogleMap googleMap, SeekBar sbWidth) {
          super(context);
          this.googleMap = googleMap;
          this.sbWidth = sbWidth;
      
          mPaint = new Paint(Paint.DITHER_FLAG);
          mPaint.setAntiAlias(true);
          mPaint.setDither(true);
          mPaint.setColor(SDV.colorChoosen);
          mPaint.setStyle(Paint.Style.STROKE);
          mPaint.setStrokeJoin(Paint.Join.ROUND);
          mPaint.setStrokeCap(Paint.Cap.ROUND);
          mPaint.setStrokeWidth(SDV.width);
      
          mDotedPaint = new Paint(Paint.DITHER_FLAG);
          mDotedPaint.setAntiAlias(true);
          mDotedPaint.setDither(true);
          mDotedPaint.setColor(SDV.colorChoosen);
      
          mDotedPaint.setStyle(Paint.Style.STROKE);
          mDotedPaint.setStrokeJoin(Paint.Join.ROUND);
          mDotedPaint.setStrokeCap(Paint.Cap.ROUND);
          mDotedPaint.setStrokeWidth(SDV.width);
      
          mColorPaint = new Paint(Paint.DITHER_FLAG);
          mColorPaint.setAntiAlias(true);
          mColorPaint.setDither(true);
          mColorPaint.setFilterBitmap(true);
          mColorPaint.setStyle(Paint.Style.FILL);
          mColorPaint.setStrokeWidth(SDV.width);
          mColorPaint.setColor(SDV.colorChoosen);
      }
      
      @Override
      protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);
          sx = super.getWidth() * 0.5f;
          sy = super.getHeight() * 0.5f;
      
          if (SDV.isDrawing) {
              new OnGoingDrawings().HandleAllOnGoingDrawings(mCurrentShape,
                      canvas);
          } else {
              new ShapeDrawer().DrawEverything(canvas, googleMap, sbWidth);
          }
      }
      
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          SDV.mx = event.getX();
          SDV.my = event.getY();
          switch (mCurrentShape) {
          case SDV.shape14:
              TouchEvents.Shape14(event);
              break;
      
          return true;
      }
      

      }

      这里是触摸监听器,

       public static void Shape14(MotionEvent event) {
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              SDV.isDrawing = true;
              SDV.mStartX = SDV.mx;
              SDV.mStartY = SDV.my;
              Gmap.mDrawingView.invalidate();
              break;
          case MotionEvent.ACTION_MOVE:
              Gmap.mDrawingView.invalidate();
              break;
          case MotionEvent.ACTION_UP:
              SDV.isDrawing = false;
              float x = SDV.mStartX,
              y = SDV.mStartY;
              float DifX = Math.abs(SDV.mx - SDV.mStartX);
              float DifY = Math.abs(SDV.my - SDV.mStartY);
              float radius;
              if (DifY > DifX)
                  radius = Math.abs(SDV.my - SDV.mStartY);
              else
                  radius = Math.abs(SDV.mx - SDV.mStartX);
              SDV.Shape14(x, y, radius);
              break;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-25
        • 1970-01-01
        • 1970-01-01
        • 2015-10-22
        • 1970-01-01
        相关资源
        最近更新 更多