View Canvas paint

作者在 2016-01-10 09:25:02 发布以下内容
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;
	}

}
android | 阅读 2202 次
文章评论,共0条
游客请输入验证码
浏览232777次
最新评论