zoukankan      html  css  js  c++  java
  • android view的自定义

    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();
    
        }
    }
    

      

  • 相关阅读:
    组装query,query汇总,query字段
    POJ 1276, Cash Machine
    POJ 1129, Channel Allocation
    POJ 2531, Network Saboteur
    POJ 1837, Balance
    POJ 3278, Catch That Cow
    POJ 2676, Sudoku
    POJ 3126, Prime Path
    POJ 3414, Pots
    POJ 1426, Find The Multiple
  • 原文地址:https://www.cnblogs.com/zhengtu2015/p/5871991.html
Copyright © 2011-2022 走看看