android自定义组件segmentedgroup源码阅读笔记

来源:互联网 发布:怎么设计数据库的表 编辑:程序博客网 时间:2024/05/29 17:57

Android自定义组件SegmentedGroup源码阅读笔记

  • LayoutSelector(自定义): This class is used to provide the proper layout based on the view. Also provides the proper radius for corners. 根据view提供合适的布局, 同时也给四角提供了合适的半径
  • Resources: Class for accessing an application’s resources. This sits on top of the asset manager of the application (accessible through getAssets()) and provides a high-level API for getting typed data from the assets. 访问应用资源的类。这个类在AssetManager之上,为从asset中得到数据提供了高等级的API
  • ColorStateList: Lets you map View state sets to colors. ColorStateLists are created from XML resource files defined in the “color” subdirectory directory of an application’s resource directory. The XML file contains a single “selector” element with a number of “item” elements inside. 动态的color selector
  • *

获取自定义数据并解析 initAttrs()方法

  • TypedArray类似于键值对,可存储id,reference
  • 使用Resources.getDimension(int dimenId)得到defValue
  • 注意recycle
private void initAttrs(AttributeSet attrs) {        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(                attrs,                R.styleable.SegmentedGroup,                0, 0);        try {            mMarginDp = (int) typedArray.getDimension(                    R.styleable.SegmentedGroup_border_width,                    getResources().getDimension(R.dimen.radio_button_stroke_border));            mCornerRadius = typedArray.getDimension(                    R.styleable.SegmentedGroup_corner_radius,                    getResources().getDimension(R.dimen.radio_button_conner_radius));            mTintColor = typedArray.getColor(                    R.styleable.SegmentedGroup_tint_color,                    getResources().getColor(R.color.radio_button_selected_color));            mCheckedTextColor = typedArray.getColor(                    R.styleable.SegmentedGroup_checked_text_color,                    getResources().getColor(android.R.color.white));        } finally {            typedArray.recycle();        }    }

动态StateList

  • 动态设置按钮的selector, 二维数组[3][?] 对应一维数组[3]
ColorStateList colorStateList = new ColorStateList(new int[][]{                {android.R.attr.state_pressed},                {-android.R.attr.state_pressed, -android.R.attr.state_checked},                {-android.R.attr.state_pressed, android.R.attr.state_checked}},                new int[]{Color.GRAY, mTintColor, mCheckedTextColor});        ((Button) view).setTextColor(colorStateList);
  • 动态设置子view的背景
Drawable checkedDrawable = resources.getDrawable(checked).mutate();        Drawable uncheckedDrawable = resources.getDrawable(unchecked).mutate();        ((GradientDrawable) checkedDrawable).setColor(mTintColor);        ((GradientDrawable) checkedDrawable).setStroke(mMarginDp, mTintColor);        ((GradientDrawable) uncheckedDrawable).setStroke(mMarginDp, mTintColor);        //Set proper radius        ((GradientDrawable) checkedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));        ((GradientDrawable) uncheckedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));        //Create drawable        StateListDrawable stateListDrawable = new StateListDrawable();        stateListDrawable.addState(new int[]{-android.R.attr.state_checked}, uncheckedDrawable);        stateListDrawable.addState(new int[]{android.R.attr.state_checked}, checkedDrawable);        view.setBackground(stateListDrawable);

onFinishInflate()

Finalize inflating a view from XML. This is called as the last phase of inflation, after all child views have been added. 结束xml文件的inflate. 所有的view都已经被添加,最后阶段。

@Override    protected void onFinishInflate() {        super.onFinishInflate();        //Use holo light for default        updateBackground();    }

自定义组件思路

1. 使用TpyedArray从attr获取数据,使用Resources获取默认数据2. 加载父控件,此时不需要自定义操作override3. 当所有子控件都已经填入,使用自定义的方法进行修饰,需要通过封装类考虑横竖屏的情况,此时需要使用到StateList, 自定义selector

问题总结

  • context.getResources(), view.getResources()得到的Resources实例没有区别
  • Resources中常用方法getColor(int colorId), getDimension(int dimenId), 用来获取colors.xml dimens.xml的id值
0 0
原创粉丝点击