android 动画学习系列(一)

老师今天开始讲android的游戏,讲到了view和surfaceview

下面是view的演示部分

 

private Paint paint;
	//游戏里很少用double,都是用long
	private int x=10,y=10;
	private Thread th;
	//线程控制
	private boolean flag;
	
	
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public MyView(Context context) {
		super(context);
		paint=new Paint();
		paint.setColor(Color.BLUE);
		
		setFocusable(true);
		setFocusableInTouchMode(true);
		
		flag=true;
		th=new Thread(this);
		th.start();
		
	}
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawText("点我", x, y, paint);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode==KeyEvent.KEYCODE_DPAD_DOWN) {
			System.out.println("down");
			y++;
		}
		//重绘方法
		invalidate();
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		//必须改为true,否则就会执行一次
		x=(int) event.getX();
		y=(int) event.getY();
		invalidate();
		return true;
	}
	@Override
	public void run() {
		
		while (flag) {
			System.out.println("run");
			x++;
			y++;
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			postInvalidate();
		}
	}
	

演示gif动画,暂无

 



 

posted on 2013-10-11 14:27  我的小人生  阅读(127)  评论(0编辑  收藏  举报