How can you draw a dynamic square type in View Example: enter image description here

I have a view on activating, I get it

View myview = (View)findViewById(R.id.myview); 

Next, you need to draw such squares as in the screenshot and write to this view and not set setContentView for the entire activation. Prompt articles or classes that will help me in this.

Began to understand a little:

 public class OverlayView extends View { public OverlayView(Context context, AttributeSet attributeSet) { super(context, attributeSet); setWillNotDraw(false); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(this.Hall == null) return; Paint paint = new Paint(); paint.setColor(Color.YELLOW); canvas.drawRect(10, 10, 50, 50, paint); paint.setColor(Color.BLACK); canvas.drawRect(10+50+10, 10, 50, 50, paint); } } <applicationcinema.com.cinema.UI.OverlayView android:id="@+id/hall_plan_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:background="@color/standard_background" /> 

For some reason, the second black square is not drawn, it seems that the function drawRect can only be called once.

  • You can create a RecyclerView with the GridLayoutManager . - post_zeew
  • What is the difference ? - LorDo
  • If you need to achieve the result in the picture, then it will be easier to use RecyclerView than to draw by hand. - post_zeew
  • Is there any example that will help in my case for RecyclerView? Is RecyclerView not the same as GridView? - LorDo
  • For example, inducesmile.com/android/… . No, RecyclerView is not the same as GridView . - post_zeew

1 answer 1

The drawRect method takes the positions of the sides of the rectangle ( left, top, right, bottom ).
That is, the third and fourth parameters are the coordinates of the lower right corner, and not the width and height.
It will be more convenient to use the class Rect in this case:

  // создаём квадрат 50х50 Rect rect = new Rect(0, 0, 50, 50); // сдвигаем вправо на 10 rect.offset(10, 0); canvas.drawRect(rect, paint); paint.setColor(Color.BLACK); // сдвигаем дальше вправо на 50+10 rect.offset(50 + 10, 0); canvas.drawRect(rect, paint);