- CompoundButton 源码分析
- LinearLayout 源码分析
- SearchView 源码解析
- LruCache 源码解析
- ViewDragHelper 源码解析
- BottomSheets 源码解析
- Media Player 源码分析
- NavigationView 源码解析
- Service 源码解析
- Binder 源码分析
- Android 应用 Preference 相关及源码浅析 SharePreferences 篇
- ScrollView 源码解析
- Handler 源码解析
- NestedScrollView 源码解析
- SQLiteOpenHelper/SQLiteDatabase/Cursor 源码解析
- Bundle 源码解析
- LocalBroadcastManager 源码解析
- Toast 源码解析
- TextInputLayout
- LayoutInflater 和 LayoutInflaterCompat 源码解析
- TextView 源码解析
- NestedScrolling 事件机制源码解析
- ViewGroup 源码解析
- StaticLayout 源码分析
- AtomicFile 源码解析
- AtomicFile 源码解析
- Spannable 源码分析
- Notification 之 Android 5.0 实现原理
- CoordinatorLayout 源码分析
- Scroller 源码解析
- SwipeRefreshLayout 源码分析
- FloatingActionButton 源码解析
- AsyncTask 源码分析
- TabLayout 源码解析
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
分析
// 选中和未选中的状态 private boolean mChecked; private boolean mBroadcasting; private Drawable mButtonDrawable; private ColorStateList mButtonTintList = null; // 就是水波纹和背景颜色混合的方式 private PorterDuff.Mode mButtonTintMode = null; private boolean mHasButtonTint = false; private boolean mHasButtonTintMode = false; // 状态监听 private OnCheckedChangeListener mOnCheckedChangeListener; private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
这是一些局部变量,在后面的分析会用到。
我们先来看看 CompoundButton 自定义控件有哪些属性 \data\res\values\attrs.xml
<declare-styleable name="CompoundButton">
<!-- 设置状态 true: 选中; false: 未选中 -->
<attr name="checked" format="boolean" />
<!-- 绘制按钮图形,一般为 Drawable 资源 (e.g. checkbox, radio button, etc). -->
<attr name="button" format="reference" />
<!-- 对绘制的按钮图形着色 -->
<attr name="buttonTint" format="color" />
<!-- 对着色设置模式 -->
<attr name="buttonTintMode">
<enum name="src_over" value="3" />
...
</attr>
</declare-styleable>然后再来看看怎么绘制,先来看看构造方法
public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
// 这里的获取自定义的 CompoundButton 就是上面的定义的 CompoundButton
final TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
// 用于绘制按钮图形
final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
if (d != null) {
setButtonDrawable(d);
}
// 对绘制的按钮图形着色设置模式
if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) {
mButtonTintMode = Drawable.parseTintMode(a.getInt(
R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
mHasButtonTintMode = true;
}
// 对绘制的按钮图形着色
if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
mHasButtonTint = true;
}
// 设置状态
final boolean checked = a.getBoolean(
com.android.internal.R.styleable.CompoundButton_checked, false);
setChecked(checked);
a.re cycle();
applyButtonTint();
}在构造方法中,获取自定义属性的各个属性,
- button:用于绘制按钮图形,然后调用 setButtonDrawable() 来绘制。
- buttonTint:绘制的按钮着色。使用一个 boolean 标识符来设置的,然后会在 applyButtonTint() 中统一处理。它们两个分别用作给和
- buttonTintMode:和设置着色模式。设置方式和 buttonTint 几乎一样。不过它的一些属性,参考这篇文章来看看具体不同的着色模式效果是怎么样的。
- checked:是设置选中状态,在 setChecked() 中设置。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论