| | |
| | | package com.moral.yunfushao.ui; |
| | | |
| | | import android.animation.ValueAnimator; |
| | | import android.content.Context; |
| | | import android.content.res.Resources; |
| | | import android.graphics.Bitmap; |
| | |
| | | private int mmHeight; |
| | | //绘制渐变圆弧的画笔 |
| | | private Paint mShaderPaint; |
| | | private int[] mshaderColor = new int[]{0xfff70e17,0xfff70e17,0xfff70e17,0xffff5c92,0xff00c44a,0xff00d062,0xff00f5af,0xff00f6cb,0xff00ccff,0xff179fff,0xffa054ff,0xffa53aff,0xffd74bff,0xfff64da5,0xfff70e17}; |
| | | private int[] mshaderRedColor = new int[]{0xfff70e17,0xffff5c92}; |
| | | private int[] mshaderZiseColor = new int[]{0xffa53aff,0xffd74bff,0xfff64da5}; |
| | | private int[] mshaderBlueColor = new int[]{0xff00f6cb,0xff00ccff,0xff179fff,0xffa054ff}; |
| | | private int[] mshaderGreenColor = new int[]{0xff00c44a,0xff00d062,0xff00f5af}; |
| | | private int[] mshaderColor = new int[]{0xfff70e17, 0xfff70e17, 0xfff70e17, 0xffff5c92, 0xff00c44a, 0xff00d062, 0xff00f5af, 0xff00f6cb, 0xff00ccff, 0xff179fff, 0xffa054ff, 0xffa53aff, 0xffd74bff, 0xfff64da5, 0xfff70e17}; |
| | | private int[] mshaderRedColor = new int[]{0xfff70e17, 0xffff5c92}; |
| | | private int[] mshaderZiseColor = new int[]{0xffa53aff, 0xffd74bff, 0xfff64da5}; |
| | | private int[] mshaderBlueColor = new int[]{0xff00f6cb, 0xff00ccff, 0xff179fff, 0xffa054ff}; |
| | | private int[] mshaderGreenColor = new int[]{0xff00c44a, 0xff00d062, 0xff00f5af}; |
| | | //环形渐变色值渲染 |
| | | private SweepGradient mSweepGradient; |
| | | private int mShaderWidth = 70; |
| | |
| | | private Paint drawTextPaint; |
| | | private int textColor = Color.GRAY; |
| | | private int textSize = 36; |
| | | private String[] level = {"安全","轻微","中度","严重"}; |
| | | private String[] level = {"安全", "建议回避", "及时闪躲", "紧急撤离"}; |
| | | |
| | | //内进度圆 |
| | | private Paint mCirclePaint; |
| | |
| | | private Bitmap mBitmap; |
| | | |
| | | private float percent = 0;//百分比数据 |
| | | private float percent2 = (float) 0.27;//百分比数据 |
| | | |
| | | |
| | | private String statusStr = "未连接"; |
| | | private ValueAnimator anim; |
| | | |
| | | public void setStatusStr(String statusStr){ |
| | | public void setStatusStr(String statusStr) { |
| | | this.statusStr = statusStr; |
| | | postInvalidate(); |
| | | } |
| | | |
| | | public void setPercent(float percent){ |
| | | public void setPercent(float percent) { |
| | | this.percent = percent; |
| | | // this.percent2 = percent; |
| | | postInvalidate(); |
| | | // if (anim.isStarted() || anim.isRunning()) { |
| | | // anim.end(); |
| | | // anim.cancel(); |
| | | // } |
| | | // anim.start(); |
| | | } |
| | | |
| | | public PanelView(Context context) { |
| | |
| | | private void init(Context mContext) { |
| | | this.mContext = mContext; |
| | | //初始化 |
| | | mShaderWidth = dip2px(mContext,24); |
| | | line1Length = dip2px(mContext,14); |
| | | line2Length = dip2px(mContext,22); |
| | | lineWidth = dip2px(mContext,2); |
| | | widthInner = dip2px(mContext,40); |
| | | paddingKedu = dip2px(mContext,10); |
| | | mCircleWidth = dip2px(mContext,24); |
| | | innerPadding = dip2px(mContext,8); |
| | | textSize = dip2px(mContext,12); |
| | | mShaderWidth = dip2px(mContext, 24); |
| | | line1Length = dip2px(mContext, 14); |
| | | line2Length = dip2px(mContext, 22); |
| | | lineWidth = dip2px(mContext, 2); |
| | | widthInner = dip2px(mContext, 40); |
| | | paddingKedu = dip2px(mContext, 10); |
| | | mCircleWidth = dip2px(mContext, 24); |
| | | innerPadding = dip2px(mContext, 8); |
| | | textSize = dip2px(mContext, 12); |
| | | |
| | | //渐变圆环 |
| | | mShaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| | |
| | | mShaderPaint.setStrokeCap(Paint.Cap.ROUND); |
| | | paintGapLine = new Paint(Paint.ANTI_ALIAS_FLAG); |
| | | paintGapLine.setColor(Color.WHITE); |
| | | paintGapLine.setStrokeWidth(dip2px(mContext,3)); |
| | | paintGapLine.setStrokeWidth(dip2px(mContext, 3)); |
| | | //刻度 |
| | | mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| | | mLinePaint.setStyle(Paint.Style.STROKE); |
| | |
| | | mBitPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| | | mBitPaint.setFilterBitmap(true); |
| | | mBitPaint.setDither(true); |
| | | mBitmap = ((BitmapDrawable)mContext.getResources().getDrawable(R.mipmap.zhizhen)).getBitmap(); |
| | | mBitmap = ((BitmapDrawable) mContext.getResources().getDrawable(R.mipmap.zhizhen)).getBitmap(); |
| | | // startAnimation(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } else { |
| | | mmHeight = dip2px(mContext, 300); |
| | | } |
| | | mWidth = mmWidth<mmHeight?mmWidth:mmHeight; |
| | | mWidth = mmWidth < mmHeight ? mmWidth : mmHeight; |
| | | setMeasuredDimension(mmWidth, mmHeight); |
| | | } |
| | | |
| | | // private void startAnimation() { |
| | | // anim = ValueAnimator.ofObject(new CustomPointEvaluator(),(float)0, percent2); |
| | | // anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| | | // @Override |
| | | // public void onAnimationUpdate(ValueAnimator animation) { |
| | | // percent = (float) animation.getAnimatedValue(); |
| | | // System.out.println("chenqi rotato " + percent + " all -> " + percent2 * 300); |
| | | //// invalidate(); |
| | | // } |
| | | // }); |
| | | // anim.setDuration(3000L); |
| | | // anim.setRepeatCount(Animation.INFINITE); |
| | | // } |
| | | // |
| | | // private class CustomPointEvaluator implements TypeEvaluator<Float> { |
| | | // @Override |
| | | // public Float evaluate(float fraction, Float startValue, Float endValue) { |
| | | // System.out.println("chenqi2 rotato " + fraction + " all -> " + endValue); |
| | | // float y = fraction * endValue; |
| | | // return y; |
| | | // } |
| | | // } |
| | | |
| | | |
| | | @Override |
| | | protected void onDraw(Canvas canvas) { |
| | | super.onDraw(canvas); |
| | | //画刻度 |
| | | //画刻度 |
| | | drawKedu(canvas); |
| | | //画刻度文字 |
| | | drawTextKedu(canvas); |
| | | //画渐变圆环 |
| | | drawJianbianCircle(canvas); |
| | | //画内圆 |
| | | RectF cirlcleRect = new RectF(mShaderWidth + widthInner+mCircleWidth+innerPadding, mShaderWidth + widthInner+mCircleWidth+innerPadding, mWidth - mShaderWidth - widthInner-mCircleWidth-innerPadding, mWidth - mShaderWidth - widthInner-mCircleWidth-innerPadding); |
| | | RectF cirlcleRect = new RectF(mShaderWidth + widthInner + mCircleWidth + innerPadding, mShaderWidth + widthInner + mCircleWidth + innerPadding, mWidth - mShaderWidth - widthInner - mCircleWidth - innerPadding, mWidth - mShaderWidth - widthInner - mCircleWidth - innerPadding); |
| | | canvas.drawArc(cirlcleRect, 0, 360, false, mCirclePaint); |
| | | //画指针 |
| | | float circleR = (cirlcleRect.right - cirlcleRect.left)/2; |
| | | float circleR = (cirlcleRect.right - cirlcleRect.left) / 2; |
| | | float bitH = circleR; |
| | | float bitW = bitH*0.448f; |
| | | float bitW = bitH * 0.448f; |
| | | canvas.save(); |
| | | canvas.rotate(percent*300-150,mWidth/2,mWidth/2); |
| | | RectF btmRect = new RectF((float) (cirlcleRect.left+bitH-bitW*0.535), (float) (cirlcleRect.top+mCircleWidth*0.68),(float) (cirlcleRect.left+bitH+bitW*0.465), (float) (cirlcleRect.top+bitH+mCircleWidth*0.68)); |
| | | canvas.rotate(percent * 300 - 150, mWidth / 2, mWidth / 2); |
| | | RectF btmRect = new RectF((float) (cirlcleRect.left + bitH - bitW * 0.535), (float) (cirlcleRect.top + mCircleWidth * 0.68), (float) (cirlcleRect.left + bitH + bitW * 0.465), (float) (cirlcleRect.top + bitH + mCircleWidth * 0.68)); |
| | | // Log.d("haijiang","left="+btmRect.left); |
| | | // Log.d("haijiang","right="+btmRect.right); |
| | | // Log.d("haijiang","top="+btmRect.top); |
| | | // Log.d("haijiang","bottom="+btmRect.bottom); |
| | | // Log.d("haijiang","宽高比例="+(btmRect.right-btmRect.left)/(btmRect.bottom-btmRect.top)); |
| | | canvas.drawBitmap(mBitmap,null,btmRect,mBitPaint); |
| | | canvas.drawBitmap(mBitmap, null, btmRect, mBitPaint); |
| | | canvas.restore(); |
| | | |
| | | drawTextPaint.setColor(line3Color); |
| | | drawTextPaint.setTextSize(textSize); |
| | | int statusStrWidth = (int) drawTextPaint.measureText(this.statusStr); |
| | | canvas.drawText(this.statusStr,mWidth / 2-statusStrWidth/2, mWidth/2+(btmRect.bottom-btmRect.top)/2,drawTextPaint); |
| | | if(percent>0) |
| | | canvas.drawArc(new RectF(mShaderWidth + widthInner+mCircleWidth+innerPadding, mShaderWidth + widthInner+mCircleWidth+innerPadding, mWidth - mShaderWidth - widthInner-mCircleWidth-innerPadding, mWidth - mShaderWidth - widthInner-mCircleWidth-innerPadding), 120, 300*percent, false, mProgressPaint); |
| | | canvas.drawText(this.statusStr, mWidth / 2 - statusStrWidth / 2, mWidth / 2 + (btmRect.bottom - btmRect.top) / 2, drawTextPaint); |
| | | if (percent > 0) |
| | | canvas.drawArc(new RectF(mShaderWidth + widthInner + mCircleWidth + innerPadding, mShaderWidth + widthInner + mCircleWidth + innerPadding, mWidth - mShaderWidth - widthInner - mCircleWidth - innerPadding, mWidth - mShaderWidth - widthInner - mCircleWidth - innerPadding), |
| | | 120, 300 * percent, false, mProgressPaint); |
| | | } |
| | | |
| | | /** |
| | | * 画刻度 |
| | | * |
| | | * @param canvas |
| | | */ |
| | | private void drawKedu(Canvas canvas){ |
| | | private void drawKedu(Canvas canvas) { |
| | | //画右边刻度 |
| | | for (int i = 0; i <= 50; i++) { |
| | | canvas.save(); |
| | |
| | | |
| | | /** |
| | | * 画刻度文字 |
| | | * |
| | | * @param canvas |
| | | */ |
| | | private void drawTextKedu(Canvas canvas){ |
| | | for(int i=0;i<level.length;i++){ |
| | | private void drawTextKedu(Canvas canvas) { |
| | | for (int i = 0; i < level.length; i++) { |
| | | float textWidth = drawTextPaint.measureText(level[i]); |
| | | if(percent<0.25){ |
| | | if(i==0){ |
| | | if (percent < 0.25) { |
| | | if (i == 0) { |
| | | drawTextPaint.setColor(line3Color); |
| | | }else{ |
| | | } else { |
| | | drawTextPaint.setColor(Color.GRAY); |
| | | } |
| | | canvas.save(); |
| | | if(i==0){ |
| | | canvas.rotate(-120,mWidth/2,mWidth/2); |
| | | }else if(i==1){ |
| | | canvas.rotate(-45,mWidth/2,mWidth/2); |
| | | }else if(i==2){ |
| | | canvas.rotate(45,mWidth/2,mWidth/2); |
| | | }else if(i==3){ |
| | | canvas.rotate(120,mWidth/2,mWidth/2); |
| | | if (i == 0) { |
| | | canvas.rotate(-120, mWidth / 2, mWidth / 2); |
| | | } else if (i == 1) { |
| | | canvas.rotate(-45, mWidth / 2, mWidth / 2); |
| | | } else if (i == 2) { |
| | | canvas.rotate(45, mWidth / 2, mWidth / 2); |
| | | } else if (i == 3) { |
| | | canvas.rotate(120, mWidth / 2, mWidth / 2); |
| | | } |
| | | canvas.drawText(level[i],mWidth / 2-textWidth/2, paddingKedu+line2Length+textSize,drawTextPaint); |
| | | canvas.drawText(level[i], mWidth / 2 - textWidth / 2, paddingKedu + line2Length + textSize, drawTextPaint); |
| | | canvas.restore(); |
| | | }else if(percent>=0.25&&percent<0.5){ |
| | | if(i==1){ |
| | | } else if (percent >= 0.25 && percent < 0.5) { |
| | | if (i == 1) { |
| | | drawTextPaint.setColor(line3Color); |
| | | }else{ |
| | | } else { |
| | | drawTextPaint.setColor(Color.GRAY); |
| | | } |
| | | canvas.save(); |
| | | // canvas.rotate(-120+i*75,mWidth/2,mWidth/2); |
| | | if(i==0){ |
| | | canvas.rotate(-120,mWidth/2,mWidth/2); |
| | | }else if(i==1){ |
| | | canvas.rotate(-45,mWidth/2,mWidth/2); |
| | | }else if(i==2){ |
| | | canvas.rotate(45,mWidth/2,mWidth/2); |
| | | }else if(i==3){ |
| | | canvas.rotate(120,mWidth/2,mWidth/2); |
| | | if (i == 0) { |
| | | canvas.rotate(-120, mWidth / 2, mWidth / 2); |
| | | } else if (i == 1) { |
| | | canvas.rotate(-45, mWidth / 2, mWidth / 2); |
| | | } else if (i == 2) { |
| | | canvas.rotate(45, mWidth / 2, mWidth / 2); |
| | | } else if (i == 3) { |
| | | canvas.rotate(120, mWidth / 2, mWidth / 2); |
| | | } |
| | | canvas.drawText(level[i],mWidth / 2-textWidth/2, paddingKedu+line2Length+textSize,drawTextPaint); |
| | | canvas.drawText(level[i], mWidth / 2 - textWidth / 2, paddingKedu + line2Length + textSize, drawTextPaint); |
| | | canvas.restore(); |
| | | }else if(percent>=0.5&&percent<0.75){ |
| | | if(i==2){ |
| | | } else if (percent >= 0.5 && percent < 0.75) { |
| | | if (i == 2) { |
| | | drawTextPaint.setColor(line3Color); |
| | | }else{ |
| | | } else { |
| | | drawTextPaint.setColor(Color.GRAY); |
| | | } |
| | | canvas.save(); |
| | | // canvas.rotate(-120+i*75,mWidth/2,mWidth/2); |
| | | if(i==0){ |
| | | canvas.rotate(-120,mWidth/2,mWidth/2); |
| | | }else if(i==1){ |
| | | canvas.rotate(-45,mWidth/2,mWidth/2); |
| | | }else if(i==2){ |
| | | canvas.rotate(45,mWidth/2,mWidth/2); |
| | | }else if(i==3){ |
| | | canvas.rotate(120,mWidth/2,mWidth/2); |
| | | if (i == 0) { |
| | | canvas.rotate(-120, mWidth / 2, mWidth / 2); |
| | | } else if (i == 1) { |
| | | canvas.rotate(-45, mWidth / 2, mWidth / 2); |
| | | } else if (i == 2) { |
| | | canvas.rotate(45, mWidth / 2, mWidth / 2); |
| | | } else if (i == 3) { |
| | | canvas.rotate(120, mWidth / 2, mWidth / 2); |
| | | } |
| | | canvas.drawText(level[i],mWidth / 2-textWidth/2, paddingKedu+line2Length+textSize,drawTextPaint); |
| | | canvas.drawText(level[i], mWidth / 2 - textWidth / 2, paddingKedu + line2Length + textSize, drawTextPaint); |
| | | canvas.restore(); |
| | | }else if(percent>=0.75){ |
| | | if(i==3){ |
| | | } else if (percent >= 0.75) { |
| | | if (i == 3) { |
| | | drawTextPaint.setColor(line3Color); |
| | | }else{ |
| | | } else { |
| | | drawTextPaint.setColor(Color.GRAY); |
| | | } |
| | | canvas.save(); |
| | | // canvas.rotate(-105+i*75,mWidth/2,mWidth/2); |
| | | if(i==0){ |
| | | canvas.rotate(-120,mWidth/2,mWidth/2); |
| | | }else if(i==1){ |
| | | canvas.rotate(-45,mWidth/2,mWidth/2); |
| | | }else if(i==2){ |
| | | canvas.rotate(45,mWidth/2,mWidth/2); |
| | | }else if(i==3){ |
| | | canvas.rotate(120,mWidth/2,mWidth/2); |
| | | if (i == 0) { |
| | | canvas.rotate(-120, mWidth / 2, mWidth / 2); |
| | | } else if (i == 1) { |
| | | canvas.rotate(-45, mWidth / 2, mWidth / 2); |
| | | } else if (i == 2) { |
| | | canvas.rotate(45, mWidth / 2, mWidth / 2); |
| | | } else if (i == 3) { |
| | | canvas.rotate(120, mWidth / 2, mWidth / 2); |
| | | } |
| | | canvas.drawText(level[i],mWidth / 2-textWidth/2, paddingKedu+line2Length+textSize,drawTextPaint); |
| | | canvas.drawText(level[i], mWidth / 2 - textWidth / 2, paddingKedu + line2Length + textSize, drawTextPaint); |
| | | canvas.restore(); |
| | | } |
| | | } |
| | |
| | | |
| | | /** |
| | | * 画渐变圆环 |
| | | * |
| | | * @param canvas |
| | | */ |
| | | private void drawJianbianCircle(Canvas canvas){ |
| | | private void drawJianbianCircle(Canvas canvas) { |
| | | mSweepGradient = new SweepGradient(mWidth / 2, mWidth / 2, mshaderColor, null); |
| | | mShaderPaint.setShader(mSweepGradient); |
| | | canvas.drawArc(new RectF(mShaderWidth + widthInner, mShaderWidth + widthInner, mWidth - mShaderWidth - widthInner, mWidth - mShaderWidth - widthInner), 120, 300, false, mShaderPaint); |
| | | //画圆环分隔 |
| | | canvas.save(); |
| | | canvas.rotate(75, mWidth / 2, mWidth / 2); |
| | | canvas.drawLine(mWidth / 2, line2Length + paddingKedu*3, mWidth / 2, (float) (line2Length + paddingKedu*0.65+mShaderWidth*2), paintGapLine); |
| | | canvas.drawLine(mWidth / 2, line2Length + paddingKedu * 3, mWidth / 2, (float) (line2Length + paddingKedu * 0.65 + mShaderWidth * 2), paintGapLine); |
| | | canvas.restore(); |
| | | canvas.save(); |
| | | canvas.rotate(0, mWidth / 2, mWidth / 2); |
| | | canvas.drawLine(mWidth / 2, line2Length + paddingKedu*3, mWidth / 2, (float) (line2Length + paddingKedu*0.65+mShaderWidth*2), paintGapLine); |
| | | canvas.drawLine(mWidth / 2, line2Length + paddingKedu * 3, mWidth / 2, (float) (line2Length + paddingKedu * 0.65 + mShaderWidth * 2), paintGapLine); |
| | | canvas.restore(); |
| | | canvas.save(); |
| | | canvas.rotate(-75, mWidth / 2, mWidth / 2); |
| | | canvas.drawLine(mWidth / 2, line2Length + paddingKedu*3, mWidth / 2, (float) (line2Length + paddingKedu*0.65+mShaderWidth*2), paintGapLine); |
| | | canvas.drawLine(mWidth / 2, line2Length + paddingKedu * 3, mWidth / 2, (float) (line2Length + paddingKedu * 0.65 + mShaderWidth * 2), paintGapLine); |
| | | canvas.restore(); |
| | | } |
| | | |