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

      

  • 相关阅读:
    数据结构 队列
    数据结构 堆栈
    UNP学习 广播
    UNP学习 路由套接口
    QTcpSocket发送结构体
    线性表及实现
    [转]理解WSRF之一 使用WS-ResourceProperties (整理自IBM网站)
    详解x86、IA-32、IA-64等CPU系列
    gsoap框架下的onvif程序流程分析
    【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)
  • 原文地址:https://www.cnblogs.com/zhengtu2015/p/5871991.html
Copyright © 2011-2022 走看看