java – How to properly push the canvas onto the stream?

Question:

There is a code that paints the image with blue squares. This happens all at once when you open the application. How to make these squares gradually paint over before our eyes with some delay? That is, you probably need to put the cycle into 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);
    }
}

Answer:

  1. In the DrawView class, remove the loops with squares from the onDraw method; then. the method will be called 1 time and draw only the background and picture.
  2. Create another DrawViewRect class for drawing squares; in it, the onDraw method will be called as many times as there are squares. Here we place the content from the cicos with squares.
  3. In the place where the first class with the onDraw method was originally called from, we call it, and then we build the loops, and inside the loops we call the second class with the onDraw method. Before calling, add the line Thread.currentThread (). Sleep (200);
Scroll to Top
AllEscort