首先绘制了一些图,然后再设置paint的宽度,得到的结果是设置前绘的图的宽度也改变了,求高手指导,
这是绘图的类的主要代码:
@Override public void onDraw (Canvas canvas) { //super.onDraw(canvas); //创建画笔 p = new Paint(); //设置画笔的颜色 p.setColor(Color.GRAY); /**画笔的类型**/ p.setStyle(Paint.Style.STROKE); /**设置画笔变为圆滑状**/ p.setStrokeCap(Paint.Cap.ROUND); /**设置线的宽度**/ p.setStrokeWidth(Integer.parseInt(m_width)); mPath.moveTo(currentX, currentY); canvas.drawPath(mPath , p); m_canvas=canvas; DrawBitmap(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { //当前组件的currentX、currentY两个属性 this.currentX = event.getX(); this.currentY = event.getY(); //返回true表明处理方法已经处理该事件 switch (event.getAction()) { case MotionEvent.ACTION_UP: upX = event.getX(); upY = event.getY(); p=null; break; case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); mPath.moveTo(downX, downY); break; case MotionEvent.ACTION_MOVE: upX = event.getX(); upY = event.getY(); mPath.quadTo(downX, downY,upX, upY ); //m_canvas.drawPath(mPath, paint); downX = upX; downY = upY; break; case MotionEvent.ACTION_CANCEL: break; } //通知改组件重绘 this.invalidate(); return true; }
修改宽度的代码就是button的click事件:
drawView1.m_width=20;主要代码
invalidate()的功能介绍如下:
Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas)
will be called at some point in the future。
应该是你执行这条指令 this.invalidate();之后,整个View进行了重绘,而此时的drawView1.m_width已经更改了。