仿瓜子二手车价格选择控件(3)

这样说可能还是不太好理解,我们把每个圆用不同的颜色来区分开,如下图所示 [原文来自:www.11jj.com]

仿瓜子二手车价格选择控件(3)

[转载出处:www.11jj.com]

这样再看是不是很直观了。之前对于自定义 View 也写了很多文章了,如果有兴趣的话可以到我的 CSDN 博客中去了解下,其实一般的自定义控件都需要这几步,首先,对需求仔细研究思考并且在自己的本子上画画草图进行结构分析(很有用),然后就是自定义属性了,假如你打算开源的话,那么自定义属性是必不可少的,这样便于使用者根据自己需要进行灵活设置,接着就是测量了,确定控件的宽高,紧接着就可以进行绘制了,当然了如果是继承 ViewGroup 的话则还需要去确定子 View 的位置,如果涉及到手势滑动等操作,最后还需要对手势滑动事件进行一系列的处理等等。当然了,这只是一个大致的步骤,因人而异吧,那么我们就按这个大致的步骤一点点的分析吧。

自定义属性<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RangeBarView">
        <attr name="rect_line_height" format="dimension"/>
        <attr name="rect_line_default_color" format="color"/>
        <attr name="rect_line_checked_color" format="color"/>
        <attr name="circle_radius" format="dimension"/>
        <attr name="circle_stroke_width" format="dimension"/>
        <attr name="left_circle_solid_color" format="color"/>
        <attr name="left_circle_stroke_color" format="color"/>
        <attr name="right_circle_solid_color" format="color"/>
        <attr name="right_circle_stroke_color" format="color"/>
        <attr name="range_text_size" format="dimension"/>
        <attr name="range_text_color" format="color"/>
        <attr name="view_text_space" format="dimension"/>
        <attr name="rect_price_desc_dialog_width" format="dimension"/>
        <attr name="rect_price_desc_dialog_color" format="color"/>
        <attr name="rect_price_desc_dialog_corner_radius" format="dimension"/>
        <attr name="rect_price_desc_text_size" format="dimension"/>
        <attr name="rect_price_desc_text_color" format="color"/>
        <attr name="rect_price_desc_space_to_progress" format="dimension"/>
    </declare-styleable>
</resources>

属性有点多哈,具体的就不再详细说了,对着上面的效果图再加上在下这拙劣的英文相信大家都能见(委)文(屈)识(各)意(位)了。

仿瓜子二手车价格选择控件(3)

接着我们看下如何去获取这些自定义的属性

public RangeBarView(Context context) {
        this(context, null);
    }

    public RangeBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RangeBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化属性值,同时将每一个属性的默认值设置在styles.xml文件中
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RangeBarView, 0, R.style.default_range_bar_value);
        int count = typedArray.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.RangeBarView_rect_line_default_color:
                    rectLineDefaultColor = typedArray.getColor(attr, ContextCompat.getColor(context, R.color.color_cdcd));
                    break;
                case R.styleable.RangeBarView_rect_line_checked_color:
                    rectLineCheckedColor = typedArray.getColor(attr, ContextCompat.getColor(context, R.color.color_275D9D));
                    break;
                case R.styleable.RangeBarView_rect_line_height:
                    rectLineHeight = typedArray.getDimensionPixelSize(attr, getResources().getDimensionPixelSize(R.dimen.rect_line_height));
                    break;
                case R.styleable.RangeBarView_circle_radius:
                    circleRadius = typedArray.getDimensionPixelSize(attr, getResources().getDimensionPixelSize(R.dimen.circle_radius));
                    break;
                case R.styleable.RangeBarView_circle_stroke_width:
                    circleStrokeWidth = typedArray.getDimensionPixelSize(attr, getResources().getDimensionPixelSize(R.dimen.circle_stroke_width));
                    break;
                case R.styleable.RangeBarView_left_circle_solid_color:
                    leftCircleSolidColor = typedArray.getColor(attr, ContextCompat.getColor(context, R.color.color_fff));
                    break;
                case R.styleable.RangeBarView_left_circle_stroke_color:
                    leftCircleStrokeColor = typedArray.getColor(attr, ContextCompat.getColor(context, R.color.color_cdcd));
                    break;
                case R.styleable.RangeBarView_right_circle_solid_color:
                    rightCircleSolidColor = typedArray.getColor(attr, ContextCompat.getColor(context, R.color.color_fff));
                    break;
                case R.styleable.RangeBarView_right_circle_stroke_color:
                    rightCircleStrokeColor = typedArray.getColor(attr, ContextCompat.getColor(context, R.color.color_cdcd));
                    break;
                case R.styleable.RangeBarView_range_text_size:
                    textSize = typedArray.getDimensionPixelSize(attr, getResources().getDimensionPixelSize(R.dimen.item_text_size));
                    break;
                case R.styleable.RangeBarView_range_text_color:
                    textColor = typedArray.getColor(attr, ContextCompat.getColor(context, R.color.color_333));
                    break;
                case R.styleable.RangeBarView_view_text_space:
                    spaceDistance = typedArray.getDimensionPixelSize(attr, getResources().getDimensionPixelSize(R.dimen.view_and_text_space));
                    break;
                case R.styleable.RangeBarView_rect_price_desc_dialog_width:
                    rectDialogWidth = typedArray.getDimensionPixelSize(attr, getResources().getDimensionPixelSize(R.dimen.rect_dialog_width));
                    break;
                case R.styleable.RangeBarView_rect_price_desc_dialog_color:
                    rectDialogColor = typedArray.getColor(attr, ContextCompat.getColor(context, R.color.color_275D9D));
                    break;
                case R.styleable.RangeBarView_rect_price_desc_dialog_corner_radius:
                    rectDialogCornerRadius = typedArray.getDimensionPixelSize(attr, getResources().getDimensionPixelSize(R.dimen.rect_dialog_corner_radius));
                    break;
                case R.styleable.RangeBarView_rect_price_desc_text_size:
                    rectDialogTextSize = typedArray.getDimensionPixelSize(attr, getResources().getDimensionPixelSize(R.dimen.rect_dialog_text_size));
                    break;
                case R.styleable.RangeBarView_rect_price_desc_text_color:
                    rectDialogTextColor = typedArray.getColor(attr, ContextCompat.getColor(context, R.color.color_fff));
                    break;
                case R.styleable.RangeBarView_rect_price_desc_space_to_progress:
                    rectDialogSpaceToProgress = typedArray.getDimensionPixelSize(attr, getResources().getDimensionPixelSize(R.dimen.rect_dialog_space_to_progress));
                    break;
            }
        }
        typedArray.recycle();
        //初始化画笔
        initPaints();
    }

自媒体 微信号:11jj 扫描二维码关注公众号
爱八卦,爱爆料。

小编推荐

  1. 1

    数字易经0到9代表什么卦(数字易经测算)

    大家好,小伟今天来为大家解答数字易经0到9代表什么卦以下问题,数字易经测算很多人还不知道,现在让我们一起来看看吧!1、数字1代表坎水、数

  2. 2

    苹果手机呼叫转移怎么设置(苹果手机呼叫转移怎么设置无法接通)

    大家好,小乐今天来为大家解答苹果手机呼叫转移怎么设置以下问题,苹果手机呼叫转移怎么设置无法接通很多人还不知道,现在让我们一起来看看

  3. 3

    中国红十字会标志简笔画(中国红十字会标志简笔画)

    大家好,小豪今天来为大家解答中国红十字会标志简笔画以下问题,中国红十字会标志简笔画很多人还不知道,现在让我们一起来看看吧!1、保护性

  4. 4

    八年级下册语文书人教版电子书(八年级下册语文书人教版电子书2022)

    大家好,小美今天来为大家解答八年级下册语文书人教版电子书以下问题,八年级下册语文书人教版电子书2022很多人还不知道,现在让我们一起来看

  5. 5

    古伊尔(魔兽古伊尔)

    大家好,小丽今天来为大家解答古伊尔以下问题,魔兽古伊尔很多人还不知道,现在让我们一起来看看吧!1、古伊尔是魔兽世界里面部落的一个酋长

  6. 6

    唯一极值点问题

    在高档数学的进修中,我们经常会碰着独一驻点的问题,在非常宽松的前提下,这个独一的驻点也就是极值点。今天我们稍微改变一下前提,商量如

  7. 7

    三公九卿制是什么(三公九卿制是什么朝代的制度)

    大家好,小乐今天来为大家解答三公九卿制是什么以下问题,三公九卿制是什么朝代的制度很多人还不知道,现在让我们一起来看看吧!1、三公九卿

  8. 8

    订房网哪个平台好(订房什么网最便宜)

    大家好,小娟今天来为大家解答订房网哪个平台好以下问题,订房什么网最便宜很多人还不知道,现在让我们一起来看看吧!1、携程、美团、艺龙、

Copyright 2024.依依自媒体,让大家了解更多图文资讯!