289_自定义小进度条

来源:互联网 发布:软件系统验收规范 编辑:程序博客网 时间:2024/06/10 10:12






自定义小进度条




public DotView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;


        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);


        //小圆的半径
        radius = 5;


        //小圆的直径
        diameter = radius * 2;


        //小圆的间距
        gap = 5;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


        //假设来10个圆,那么宽就是10*(10*2+5)+5=255
        //高就给圆的直径就行了
        int width = 10 * (diameter + gap) + gap;
        int height = diameter;


        setMeasuredDimension(width, height);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        //第一个圆的xy是(10+5)*1-5,10,也就是(gap+radius)*1,radius
        //第二个圆的xy是(10+5)*2-5,10
        for (int i = 1; i <= 10; i++) {
            canvas.drawCircle((diameter + gap) * i - radius, radius, radius, paint);
        }
    }










public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;


        HorizontalScrollView hv = (HorizontalScrollView) View.inflate(context, R.layout.view_ll, this);


        LinearLayout view = (LinearLayout) hv.findViewById(R.id.ll);


        for (int i = 0; i < 5; i++) {
            ImageView imageView = new ImageView(context);
            view.addView(imageView);
            ViewGroup.LayoutParams layoutParams1 = imageView.getLayoutParams();
            layoutParams1.width = 30;
            layoutParams1.height = 30;
            imageView.setLayoutParams(layoutParams1);
            imageView.setImageResource(R.mipmap.ic_launcher);


            if (i != 4) {
                DotView dotView = new DotView(context);
                view.addView(dotView);
                LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) dotView.getLayoutParams();
                layoutParams2.gravity = Gravity.CENTER_VERTICAL;
            }
        }
    }





0 0
原创粉丝点击