赞
踩
- @Override
- protected void onDraw(Canvas canvas) {
- // 这里我们直接使用canvas对象处理当前的画布,比如说使用Paint来选择要填充的颜色
- Paint paintBackground = new Paint();
- paintBackground.setColor(getResources().getColor(R.color.xxx)); // 从Res中找到名为xxx的color颜色定义
- canvas.drawRect(0, 0, getWidth(), getHeight(), paintBackground); // 设置当前画布的背景颜色为paintBackground中定义的颜色,以0,0作为为起点,以当前画布的宽度和高度为重点即整块画布来填充。 具体的后面会讲,在Canvas中我们可以实现画路径,图形,区域,线。而Paint作为绘画方式的对象可以设置颜色,大小,甚至字体的类型等等。
- }
- @Override
- protected Parcelable onSaveInstanceState() {
- Parcelable p = super.onSaveInstanceState();
- Bundle bundle = new Bundle();
- bundle.putInt("x", pX);
- bundle.putInt("y", pY);
- bundle.putParcelable("android_state", p);
- return bundle;
- }
- @Override
- protected void onRestoreInstanceState(Parcelable state) {
- Bundle bundle = (Bundle) state;
- dosomething(bundle.getInt("x"), bundle.getInt("y")); // 获取刚才存储的x和y信息
- r.onRestoreInstanceState(bundle.getParcelable("android_state"));
- return;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- Paint paintRed=new Paint();
- paintRed.setColor(Color.RED);
- canvas.drawPoint(11,3,paintRed); //在坐标11,3上画一个红点
- }
- @Override
- protected void onDraw(Canvas canvas) {
- Paint paintPath=new Paint();
- Paint paintText=new Paint();
- paintPath.setColor(Color.RED); // 路径的画刷为红色
- paintText.setColor(Color.BLUE); // 路径上的文字为蓝色
- Path path=new Path();
- Path.addCircle(10,10,50,Direction.CW); // 半径为50px,绘制的方向CW为顺时针
- canvas.drawPath(path,paintPath);
- canvas.drawTextOnPath("iwanghang - CWJ",path,0,15,paintText); // 在路径上绘制文字
- }
- public class MyView extends View {
- public MyView(Context context) {
- super(context);
- setFocusable(true); // 允许获得焦点
- setFocusableInTouchMode(true); // 获取焦点时允许触控
- }
- @Override
- protected Parcelable onSaveInstanceState() { // 处理窗口保存事件
- Parcelable pSaved = super.onSaveInstanceState();
- Bundle bundle = new Bundle();
- //dosomething
- return bundle;
- }
- @Override
- protected void onRestoreInstanceState(Parcelable state) { // 处理窗口还原事件
- Bundle bundle = (Bundle) state;
- //dosomething super.onRestoreInstanceState(bundle.getParcelable("cwj"));
- return;
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) // 处理窗口大小变化事件
- {
- super.onSizeChanged(w, h, oldw, oldh);
- }
- @Override
- protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
- {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec); //如果不让父类处理记住调用setMeasuredDimension
- }
- @Override
- protected void onLayout (boolean changed, int left, int top, int right, int bottom)
- {
- super.onLayout (changed,left,top, right,bottom) ;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- Paint bg = new Paint();
- bg.setColor(Color.RED);
- canvas.drawRect(0, 0, getWidth()/2, getHeight()/2, bg); // 将view的左上角四分之一填充为红色
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- return super.onTouchEvent(event); // 让父类处理屏幕触控事件
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) { // 处理按键事件,响应的轨迹球事件为 public boolean onTrackballEvent (MotionEvent event)
- switch (keyCode) {
- case KeyEvent.KEYCODE_DPAD_UP:
- break;
- case KeyEvent.KEYCODE_DPAD_DOWN:
- break;
- case KeyEvent.KEYCODE_DPAD_LEFT:
- break;
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- break;
- case KeyEvent.KEYCODE_DPAD_CENTER: // 处理中键按下
- break;
- default:
- return super.onKeyDown(keyCode, event);
- }
- return true;
- }
- }
- @Override
- protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
- {
- height = View.MeasureSpec.getSize(heightMeasureSpec);
- width = View.MeasureSpec.getSize(widthMeasureSpec);
- setMeasuredDimension(width,height); // 这里面是原始的大小,需要重新计算可以修改本行
- // dosomething
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。