先上两张图压压惊:
如图,带虚拟键的华为手机,图片内容是用SurfaceView画出的背景和一个大圆,一个小圆(转盘),调出虚拟键进入打开正常如下图1,关闭虚拟键如下图2
关闭虚拟键进入打开如下图1,打开虚拟键如下图2
接下来是代码,个人感觉没什么问题
//初始化盘块绘制的范围 mRange = new RectF(mPadding, getMeasuredHeight()/2-mRadius/2, mPadding + mRadius, getMeasuredHeight()/2+mRadius/2);
/** * 绘制背景 */ private void drawBg() { mCanvas.drawColor(Color.TRANSPARENT); mCanvas.drawBitmap(mBackgoundBitmap,0,0,null); mCanvas.drawBitmap(mBgBitmap, null, new Rect(mPadding/3, (getMeasuredHeight()/2-(getMeasuredWidth()-2*mPadding/3)/2), getMeasuredWidth() - mPadding/3, (getMeasuredHeight()/2+(getMeasuredWidth()-2*mPadding/3)/2)), null); }
public void draw() { try { mCanvas = mHolder.lockCanvas(); if (mCanvas != null) { //绘制背景 drawBg(); //绘制盘块 float tmpAngle = mStartAngle; float sweepAngle = 360 / mItemCount; for (int i = 0; i < mItemCount; i++) { mArcPaint.setColor(mColors[i]); //绘制盘块 mCanvas.drawArc(mRange, tmpAngle, sweepAngle, true, mArcPaint); //绘制文本 drawText(tmpAngle, sweepAngle, mStrs[i]); //绘制Icon drawIcon(tmpAngle, mImgsBitmap[i]); tmpAngle += sweepAngle; }
疑问:draw()方法是只要SurfaceView没被销毁就一直在执行不断重绘的,从图片可以看出,当把虚拟键关闭的时候,drawBg()画背景和大圆盘的方法是执行了的,但是下面画转盘的代码为什么没执行?该如何解决?
你的mRange对象一直没变化,即你的转盘坐标并没有重新计算,你应该在onDraw方法里调你那段初始化转盘的代码,重新计算盘块绘制的范围,封装一下方法,就像你的drawBg()方法一样。
??????