There is a code that colors the image in blue squares. It all happens immediately when you open the application. How to make these squares gradually painted over with some kind of delay? That is, you probably need to put the cycle in the stream

MainActivity

public class MainActivity extends Activity { private View drawView, drawViewRect; int i, j; String Tag = "oh!"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawView = new DrawView(this); setContentView(drawView); for (i = 1; i <= 36; i++) { for (j = 48; j >= 1; j--) { drawViewRect = new DrawViewRect(this); setContentView(drawViewRect); } } } } 

Drawview

 public class DrawView extends View { Paint p; Rect rect; Bitmap bitmap; String Tag = "Oh!"; private int startX = 0; private int startY = 0; private int endX = 0; private int endY = 0; private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG) { { setDither(true); setColor(Color.RED); setStrokeWidth(20); } }; public DrawView(Context context) { super(context); p = new Paint(); //шаблон квадрата rect = new Rect(0,940,20,960); } public DrawView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //фоновая заливка canvas.drawARGB(80, 102, 204, 255); //фоновая картинка bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic); p.setColor(Color.RED); canvas.drawBitmap(bitmap, 0, 0, p); } } 

DrawViewRect

 public class DrawViewRect extends View { Paint p; Rect rect; Bitmap bitmap; String Tag = "Oh!"; //конструктор public DrawViewRect(Context context) { super(context); p = new Paint(); //шаблон квадрата rect = new Rect(0,940,20,960); } public DrawViewRect(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { p.setStyle(Paint.Style.FILL); p.setARGB(255, 0, 0, 255); canvas.drawRect(rect, p); canvas.translate(0, -20); } } 

    2 answers 2

    1. In the DrawView class , from the onDraw method , we remove cycles with squares; so the method will be called once and draw only the background and the image.
    2. Create another DrawViewRect class for drawing squares; in it the onDraw method will be called as many times as we have squares. Here we put the contents of the tsik with squares.
    3. In the place where we initially called the first class with the onDraw method, we call it, and then we build cycles, and inside the cycles we call the second class with the onDraw method. Before the call, add the line Thread.currentThread (). Sleep (200);
    • I tried, the image appears only after everything is completely painted over. Up to this point (while the cycle is on) the screen is empty - Sergey Gukov
    • Updated the answer .. - Drakonoved
    • Can you please see it or not. Corrected the issue. This code does not work, it draws only one square (and there is no background). - Sergey Gukov
    • You have a square drawn all the time in the same place. It is necessary to shift it. Add the coordinates of the square to the DrawViewRect constructor depending on the i, j points of the cycles, then the canvas.translate (0, -20) line; need to be removed, and do not forget to add Thread.currentThread (). sleep (200); , or use postInvalidateDelayed from another answer .. - Drakonoved
    • Is there no coordinate value in DrawViewRect? - Sergey Gukov

    Use postInvalidateDelayed() .

    Here is a simple example of how to draw a line gradually.

     public class DrawView extends View { private int startX = 0; private int startY = 0; private int endX = 0; private int endY = 0; private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG) { { setDither(true); setColor(Color.RED); setStrokeWidth(20); } }; public DrawView(Context context) { super(context); } public DrawView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawLine(startX, startY, endX, endY, paint); if (endX != 300 && endY != 300) { endY++; endX++; postInvalidateDelayed(1000 / 30); } } } 

    And don't do bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic); in onDraw (). It is better to bring the initialization to the constructor.