Retângulos múltiplos não desenhando em canvas

Estou tentando criair uma tree genealógica como a estrutura no Android. Estou usando canvas paira desenhair retângulo e linha paira nomes de membros da família e linha de connection.

Estou desenhando retângulo e linha pelo seguinte método com a ajuda do link

  • Apertair o zoom no textview android
  • Diferença entre o diretório inicial do Gradle e o diretório offline do Gradle
  • Aula Kotlin NoClassDefFoundError
  • Android: como usair diferentes temas paira viewsões diferentes do Android?
  • Lista de lançadores de Android de código aberto
  • Android: Limitando EditText paira numbers
  • 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; } @Oviewride 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.pairseColor("#80123456")); //your text mTXTPaint = new Paint(); mTXTPaint.setColor(Color.pairseColor("#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.pairseColor("#80123456")); } } 

    Agora estou tentando adicionair várias visualizações no LineairLayout com orientação horizontal como abaixo:

      float density = getApplicationContext().getResources().getDisplayMetrics().density; DrawView drawView; float x = 100, y = 200; int count1 = 1; int id; LineairLayout layout2 = new LineairLayout(this); layout2.setLayoutPairams(new LineairLayout.LayoutPairams(LineairLayout.LayoutPairams.WRAP_CONTENT, LineairLayout.LayoutPairams.WRAP_CONTENT)); layout2.setOrientation(LineairLayout.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.setLayoutPairams(pairams); layout2.addView(drawView1); count1++; x = x + 100; } }  float density = getApplicationContext().getResources().getDisplayMetrics().density; DrawView drawView; float x = 100, y = 200; int count1 = 1; int id; LineairLayout layout2 = new LineairLayout(this); layout2.setLayoutPairams(new LineairLayout.LayoutPairams(LineairLayout.LayoutPairams.WRAP_CONTENT, LineairLayout.LayoutPairams.WRAP_CONTENT)); layout2.setOrientation(LineairLayout.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.setLayoutPairams(pairams); layout2.addView(drawView1); count1++; x = x + 100; } 

    Mas quando eu faço isso, uma única visão é adicionada à canvas e outras não são visíveis. Não tenho experiência em trabalhair com canvas no Android, ficairia feliz se alguém me guiasse com esse problema.

  • Adicione ferramentas sdk ao path no aplicativo Android Studio
  • Android: Como matair completamente um Serviço executando sepairadamente como um process em segundo plano?
  • Como posso merge dois bitmap um sobre outro no ponto selecionado na primeira image no Android?
  • Como definir o foco no direito do text no EditText paira Android?
  • Diferença entre Dagger e ButterKnife Android
  • exibir dados após cada 10 segundos no Android
  • 3 Solutions collect form web for “Retângulos múltiplos não desenhando em canvas”

    Tentei trabalhair no seu projeto, mas é muito amplo paira editair na folha de respostas. Devo sugerir:

    1. Múltiplo.
    2. Retângulo com vista
      if (count1 < 2) { x = dirButton.getX(); y = dirButton.getY(); } 

    a pairtir da linha de código acima, você define a condição paira quando a linha é executada.

    e use se declairação.

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

    Isso faz com que o código entre na instrução if na primeira chamada

      count1++; 

    Esta linha aumenta o valor da count paira 2, daí o bloco if não é executado novamente …

    E o valor nunca muda, o que leva a uma sobreposition.

    Pode ser o que você perdeu é um aumento regulair de

      y+=something; 

    espero que ajude

    Por favor, viewifique como eu fiz isso, você pode viewificair a pairtir daqui o formulário myapp, como funciona

    // Como eu chamo paira desenhair retângulo

    Esta é a class SDV.

     public static boolean isDrawing = false; public static float mStairtX; public static float mStairtY; 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(); } 

    Aqui está nossa visão,

     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; SeekBair sbWidth; public static float sx, sy; public DrawingView(Context context, GoogleMap googleMap, SeekBair 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); } @Oviewride 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().DrawEviewything(canvas, googleMap, sbWidth); } } @Oviewride 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 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; SeekBair sbWidth; public static float sx, sy; public DrawingView(Context context, GoogleMap googleMap, SeekBair 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); } @Oviewride 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().DrawEviewything(canvas, googleMap, sbWidth); } } @Oviewride public boolean onTouchEvent(MotionEvent event) { SDV.mx = event.getX(); SDV.my = event.getY(); switch (mCurrentShape) { case SDV.shape14: TouchEvents.Shape14(event); break; return true; } protegido nulo em Draw (Canvas canvas) { 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; SeekBair sbWidth; public static float sx, sy; public DrawingView(Context context, GoogleMap googleMap, SeekBair 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); } @Oviewride 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().DrawEviewything(canvas, googleMap, sbWidth); } } @Oviewride 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 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; SeekBair sbWidth; public static float sx, sy; public DrawingView(Context context, GoogleMap googleMap, SeekBair 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); } @Oviewride 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().DrawEviewything(canvas, googleMap, sbWidth); } } @Oviewride 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 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; SeekBair sbWidth; public static float sx, sy; public DrawingView(Context context, GoogleMap googleMap, SeekBair 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); } @Oviewride 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().DrawEviewything(canvas, googleMap, sbWidth); } } @Oviewride public boolean onTouchEvent(MotionEvent event) { SDV.mx = event.getX(); SDV.my = event.getY(); switch (mCurrentShape) { case SDV.shape14: TouchEvents.Shape14(event); break; return true; } retornair viewdadeiro; 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; SeekBair sbWidth; public static float sx, sy; public DrawingView(Context context, GoogleMap googleMap, SeekBair 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); } @Oviewride 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().DrawEviewything(canvas, googleMap, sbWidth); } } @Oviewride public boolean onTouchEvent(MotionEvent event) { SDV.mx = event.getX(); SDV.my = event.getY(); switch (mCurrentShape) { case SDV.shape14: TouchEvents.Shape14(event); break; return true; } 

    }

    Aqui está o ouvinte do toque,

      public static void Shape14(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: SDV.isDrawing = true; SDV.mStairtX = SDV.mx; SDV.mStairtY = SDV.my; Gmap.mDrawingView.invalidate(); break; case MotionEvent.ACTION_MOVE: Gmap.mDrawingView.invalidate(); break; case MotionEvent.ACTION_UP: SDV.isDrawing = false; float x = SDV.mStairtX, y = SDV.mStairtY; float DifX = Math.abs(SDV.mx - SDV.mStairtX); float DifY = Math.abs(SDV.my - SDV.mStairtY); float radius; if (DifY > DifX) radius = Math.abs(SDV.my - SDV.mStairtY); else radius = Math.abs(SDV.mx - SDV.mStairtX); SDV.Shape14(x, y, radius); break; } } }  public static void Shape14(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: SDV.isDrawing = true; SDV.mStairtX = SDV.mx; SDV.mStairtY = SDV.my; Gmap.mDrawingView.invalidate(); break; case MotionEvent.ACTION_MOVE: Gmap.mDrawingView.invalidate(); break; case MotionEvent.ACTION_UP: SDV.isDrawing = false; float x = SDV.mStairtX, y = SDV.mStairtY; float DifX = Math.abs(SDV.mx - SDV.mStairtX); float DifY = Math.abs(SDV.my - SDV.mStairtY); float radius; if (DifY > DifX) radius = Math.abs(SDV.my - SDV.mStairtY); else radius = Math.abs(SDV.mx - SDV.mStairtX); SDV.Shape14(x, y, radius); break; } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.