自定义控件是件很有乐趣的事,慢慢的会爱上它,哈哈哈。废话不多说先上设计图
接下来我们我们要先分析一下页面中需要到哪些东西
1.一条无限长的底线;
2.一根在屏幕中间的红色标记线,代表当前位置;
3.一个半透明的蓝色背景,改背景超过部分才会被展现;
4.刻度 整数高一点刻度 非整第一点的刻度
5.一个大刻度上都会标的数值
6.滚动
接下来进入正题,让我们开始绘制:
首先我们先给控件定义它的属性,根据刚才的分析我给他定义了一下的属性
接下来在控件的构造方法中获取这些值,里面还涉及到一些需要初始化的值都有备注
public RulerView(Context context, AttributeSet attrs) {
super(context, attrs);
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RulerView);
maxValue = typedArray.getDimensionPixelOffset(R.styleable.RulerView_max_value, 200000);
minValue = typedArray.getDimensionPixelOffset(R.styleable.RulerView_min_value, 0);
scaleTextSize = typedArray.getDimensionPixelOffset(R.styleable.RulerView_scale_text_size, 24);
scaleTextColor = typedArray.getColor(R.styleable.RulerView_scale_text_color, Color.parseColor("#d8d8d8"));
scaleSelectColor = typedArray.getColor(R.styleable.RulerView_scale_select_color, Color.parseColor("#76e4ff"));
scaleSelectBackgroundColor = typedArray.getColor(R.styleable.RulerView_scale_select_background_color, Color.parseColor("#6676e4ff"));
scaleUnSelectColor = typedArray.getColor(R.styleable.RulerView_scale_unselect_color, Color.parseColor("#d8d8d8"));
cursorColor = typedArray.getColor(R.styleable.RulerView_tag_col