public class YanghangCreditCircle extends View { private static final String TAG = "YanghangCreditCircle"; private int resid = R.drawable.credit_report_turntable; public YanghangCreditCircle(Context context) { super(context); init(); } public YanghangCreditCircle(Context context, AttributeSet attrs) { super(context, attrs); TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.YanghangCreditCircle); resid = t.getResourceId(R.styleable.YanghangCreditCircle_itembackground,R.drawable.credit_report_turntable); init(); } private void init() { progressPen = new Paint(); progressPen.setAntiAlias(true); progressPen.setStyle(Paint.Style.STROKE); progressPen.setColor(Color.WHITE); progressPen.setStrokeWidth(margin);//画笔框的宽度margin,画笔从中间画 circleBmp = BitmapFactory.decodeResource(getResources(), resid); bmpPaint.setAntiAlias(true); } // private float outsidePenSize; private Paint progressPen; private Bitmap circleBmp; private float degress = 0; float margin = UIUtil.INSTANCE.DipToPixels(10); private Paint bmpPaint = new Paint(); @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { float width = circleBmp.getWidth() + (margin) * 2; float height = circleBmp.getHeight() + (margin)*2; widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) width, MeasureSpec.EXACTLY); heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height, MeasureSpec.EXACTLY); setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); } public void setProgress(float degress){ this.degress = 5f + 205f*degress; postInvalidate();//刷新onDraw } public void setdegressProgress(float degress) { this.degress = degress; postInvalidate(); } // ( 0,700] E // (700,840] D // (840,890] C // (890,930] B // (930,1000] A @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); Rect rect = new Rect(); getDrawingRect(rect); int bmpWidth = (int) (circleBmp.getWidth() / 2.0f); int bmpHeight = (int) (circleBmp.getHeight() / 2.0f); Rect dstRect = new Rect(); dstRect.set(rect.centerX() - bmpWidth, rect.centerY() - bmpHeight, rect.centerX() + bmpWidth, rect.centerY() + bmpHeight);//canvas上的目标区域 RectF outsideCircleRect = new RectF(dstRect.left + margin/2, dstRect.top + margin/2, dstRect.left + dstRect.width() - margin/2, dstRect.top + dstRect.width() - margin/2); canvas.drawBitmap(circleBmp, null, dstRect, bmpPaint);//显示整个图片在特定的矩形区域内 progressPen.setAlpha(100); canvas.drawArc(outsideCircleRect, 160f, degress, false, progressPen); float tmpDegress = degress; canvas.save(); } }
时间: 2024-10-03 22:49:28