package com.vekaco.runabledraw;


import android.app.Activity;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.os.Bundle;

import android.os.Handler;

import android.util.DisplayMetrics;

import android.view.View;


public class MainActivity extends Activity {

// private float initX = 0;

// private float initY=0;

private float changeX1 = 5;

private float changeX2 = 7;

private float initLeft1 = 0;

private float initRight1 = 60;

private float initLeft2 = 0;

private float initRight2 = 60;

private int windowWidth;

// private int windowHeight;

private Handler handler;

private boolean isRunning = true;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

MyView myview = new MyView(this);

DisplayMetrics window = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(window);

windowWidth = window.widthPixels;

setContentView(myview);

}


private class MyView extends View implements Runnable {


public MyView(Context context) {

super(context);

// TODO Auto-generated constructor stub

handler = new Handler();

new Thread(this).start();

}


@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

Paint paint = new Paint();

paint.setColor(Color.BLUE);

paint.setStyle(Paint.Style.FILL);

canvas.drawRect(initLeft1, 10, initRight1, 100, paint);


paint.setColor(Color.RED);

canvas.drawRect(initLeft2, 110, initRight2, 200, paint);

}


@Override

public void run() {

// TODO Auto-generated method stub

while (isRunning) {

initLeft1 += changeX1;

initRight1 += changeX1;

initLeft2 += changeX2;

initRight2 += changeX2;

if ((int) initLeft1 > windowWidth) {

initRight1 = 0;

initLeft1 = -60;

}

if ((int) initLeft2 > windowWidth) {

initRight2 = 0;

initLeft2 = -60;

}

handler.post(new Runnable() {


@Override

public void run() {

// TODO Auto-generated method stub

invalidate();

}

});

try {

Thread.sleep(100);

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

}

}