View Canvas paint

android | 2016-01-10 09:25:02 | 阅读 1625 次 | 评论(0)
public class MyView extends View{
private int count=0;

	public MyView(Context context) {
		super(context);
		
	}
	public void onDraw(Canvas canvas){
		int color = 0;
		switch(count){
		case 0:
		color=Color.BLUE;
		break;
		case 1:
			color=Color.LTGRAY;
			break;
		case 2:
			color=Color.GREEN;
			break;
		case 3:
			color=Color.RED;
			break;
		case 4:
			color=Color.YELLOW;
			break;
		}
		count++;
		if(count>4){
			count=0;
	}
	
Paint mpaint=new Paint();
mpaint.setColor(color);
mpaint.setTextSize(50);
mpaint.setAntiAlias(true);
canvas.drawRGB(238,255,100);
canvas.drawText("www祝您新年快乐!", 100,100, mpaint);
	}
	
}
/*************************************************/
public class MainActivity extends Activity {
	private Button btn;
	private MyView mGameView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.mGameView = new MyView(this);
		setContentView(mGameView);

		// btn=(Button) findViewById(R.id.button1);
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {

					while (true) {
						Message msg = new Message();
						viewHandler.sendMessage(msg);
						Thread.sleep(1000);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}).start();

	}

	

	Handler viewHandler = new Handler() {
		public void handleMessage(Message msg) {
		 mGameView.invalidate();
			super.handleMessage(msg);
		}
	};
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
文章评论,共0条
游客请输入验证码
浏览175763次
最新评论
  • zhouwenyuan:博主可以开发房产APP吗?
  • qunxingw:结合附件,在分表A或B...实验一下宏就理解了
  • qunxingw:这仅是小范围的一种思路,此题是指数级的数据。