为满足业务场景,需要新增附加特性:可自定义按钮内元素相对布局
附加特性会随着业务需求变化而变化,可以用模版方法模式
将这层变化封装起来:由Selector
定义初始化算法框架,将真正界面初始化延后到子类进行。
- 虽然这次业务场景中,单选按钮元素的布局是:图片在上,文字在下。下次换了咋办?所以定义元素布局应该作为一个抽象函数交给
Selector
子类实现。 - 为了实现选中的渐变效果,
Selector
需提供选项变更的时机。 - 按钮包含一些基本的属性,比如按钮名称,按钮图标,将这些属性写成自定义属性并传递给子类解析,代码如下:
public abstract class Selector extends FrameLayout implements View.OnClickListener {
public Selector(Context context) {
super(context);
initView(context, null);
}
public Selector(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public Selector(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
//模版方法
private void initView(Context context, AttributeSet attrs) {
//读取自定义属性
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Selector);
int tagResId = typedArray.getResourceId(R.styleable.Selector_tag, 0);
tag = context.getString(tagResId);
//将自定义属性传递给子类
onObtainAttrs(typedArray);
typedArray.recycle();
} else {
tag = “default tag”;
}
//构建按钮视图
View view = onCreateView();
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.addView(view, params);
this.setOnClickListener(this);
}
public void onObtainAttrs(TypedArray typedArray) {}
//子类实现该函数以定义单选按钮元素布局
protected abstract View onCreateView();
@Override
public void onClick(View v) {
boolean isSelect = switchSelector();
}
public boolean switchSelector() {
boolean isSelect = this.isSelected();
this.setSelected(!isSelect);
//当选项变更时
onSwitchSelected(!isSelect);
return !isSelect;
}
//选项变更
protected abstract void onSwitchSelected(boolean isSelect);
}
因为Selector
是抽象类,所以必须由子类实现它的抽象,下面的代码即是demo中年龄单选按钮的实现:
public class AgeSelector extends Selector {
//声明按钮包含的控件
private TextView tvTitle;
private ImageView ivIcon;
private ImageView ivSelector;
private ValueAnimator valueAnimator;
@Override
public void onObtainAttrs(TypedArray typedArray) {
//解析自定义属性
text = typedArray.getString(R.styleable.Selector_text);
iconResId = typedArray.getResourceId(R.styleable.Selector_img, 0);
indicatorResId = typedArray.getResourceId(R.styleable.Selector_indicator, 0);
textColor = typedArray.getColor(R.styleable.Selector_text_color, Color.parseColor("#FF222222"));
textSize = typedArray.getInteger(R.styleable.Selector_text_size, 15);
}
private void onBindView(String text, int iconResId, int indicatorResId, int textColor, int textSize) {
//将自定义属性绑定到控件
if (tvTitle != null) {
tvTitle.setText(text);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
tvTitle.setTextColor(textColor);
}
if (ivIcon != null) {
ivIcon.setImageResource(iconResId);
}
if (ivSelector != null) {
ivSelector.setImageResource(indicatorResId);
ivSelector.setAlpha(0);
}
}
@Override
protected View onCreateView() {
//构建自定义按钮布局
View view = LayoutInflater.from(this.getContext()).inflate(R.layout.age_selector, null);
tvTitle = view.findViewById(R.id.tv_title);
ivIcon = view.findViewById(R.id.iv_icon);
ivSelector = view.findViewById(R.id.iv_selector);
onBindView(text, iconResId, indicatorResId, textColor, textSize);
return view;
}
@Override
protected void onSwitchSelected(boolean isSelect) {
//单选按钮状态变化时做动画
if (isSelect) {
playSelectedAnimation();
} else {
playUnselectedAnimation();
}
}
private void playUnselectedAnimation() {
if (ivSelector == null) {
return;
}
if (valueAnimator != null) {
valueAnimator.reverse();
}
}
private void playSelectedAnimation() {
if (ivSelector == null) {
return;
}
valueAnimator = ValueAnimator.ofInt(0, 255);
valueAnimator.setDuration(800);
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ivSelector.setAlpha((int) animation.getAnimatedValue());
}
});
valueAnimator.start();
}
}
其中单选按钮的布局文件如下:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
</android.support.constraint.ConstraintLayout>
如何定义单选组这个抽象?
等等,好像有点不太对劲!如果运行上述代码,你会发现每个Selector
都运行良好(选中状态发生变化时有渐变动画),但多个Selector
可以同时被选中,他们并没有实现互斥选中。。。
其他重要知识点
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。有**【Android架构视频+BATJ面试专题PDF+核心笔记等资料+源码+思维导图】。希望能够帮助到大家提升技术。如果大家想要获取的话,可以私信我【666】免费获取哦~**
高级进阶篇——高级UI,自定义View(部分展示)
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
- 面试题部分合集
展示)**
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
[外链图片转存中…(img-ggYTIaiP-1646390024315)]
- 面试题部分合集
[外链图片转存中…(img-x9mgjgzi-1646390024315)]