|
mSet.setDuration(mAnimationTime);
mLine.startAnimation(mSet);
}
计算修改下标的位置
/**
*/
public void setChanger(int position) {
//改为默认字体颜色修改选中字体颜色
resetColor();
mTextList.get(position).setTextColor(mText_Press);
mTextList.get(position).setTextSize(mTextPress);
mSelected = position;
int mWeight;
if (isFull) {
mWeight = mContWidth;
} else {
mWeight = (int) (setLineLength(mListTitle.get(mSelected)) * mBai);
if (mWeight > mContWidth) {
mWeight = mContWidth;
}
}
/**
*/
int way = mContWidth * mSelected + (mContWidth - mWeight) / 2;
LayoutParams mLayoutParams = new LayoutParams(mWeight, mHeight);
mLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mLine.setLayoutParams(mLayoutParams);
setAnimation(mEndAddress, way);
mEndAddress = way;
this.mNowPosition = position;
}
线的颜色 : indicatorColor (默认red)
textview显示数组 :array
字体默认颜色:text_nomal_color (默认gray)
字体选中颜色:text_press_color (默认black)
字体默认大小:text_press_color (默认12)
字体选中大小:text_press_size (默认13)
默认选中:selected (默认0)
线是否铺满:isFull (默认false)
移动速度:speed (默认300)
线是字体的倍数:multiply (默认1.2)
线的高度: multiply (默认5)
<declare-styleable name="Indicator">
<attr name="indicatorColor" format="color"/>
<attr name="array" format="integer"/>
<attr name="text_nomal_color" format="color"/>
<attr name="text_press_color" format="color"/>
<attr name="text_nomal_size" format="integer"/>
<attr name="text_press_size" format="integer"/>
<attr name="selected" format="integer"/>
<attr name="isFull" format="boolean"/>
<attr name="speed" format="integer"/>
<attr name="multiply" format="float"/>
<attr name="line_hegith" format="integer"/>
</declare-styleable>
[]( )自定义控件代码献上
====================================================================
/**
- Created by huangbo on 17/1/22.
- 指示器
*/
public class MyIndicator extends RelativeLayout {
/**
*/
private int mColor;
/**
*/
private int mHeight;
/**
*/
private String[] mList;
/**
*/
private List<TextView> mTextList = new ArrayList<>();
/**
*/
private List<String> mListTitle = new ArrayList<>();
/**
*/
private int mTextNomal;
/**
*/
private int mTextPress;
/**
*/
private int mText_Nomal;
/**
*/
private int mText_Press;
/**
*/
private int mContWidth;
/**
*/
private int mSelected;
/**
*/
private View mLine;
/**
*/
private boolean mIsCheck;
/**
*/
private float mBai;
/**
*/
private int mEndAddress;
/**
*/
private int mProhibitPisition = -1;
/**
*/
private int mAnimationTime;
/**
*/
private int mNowPosition;
/**
*/
private boolean isFull;
public MyIndicator(Context context) {
this(context, null);
}
public MyIndicator(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setGravity(Gravity.CENTER_VERTICAL);
this.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Indicator, 0, 0);
mColor = array.getColor(R.styleable.Indicator_indicatorColor, Color.RED);
mList = getContext().getResources().getStringArray(array.getResourceId(R.styleable.Indicator_array, 0));
mTextNomal = array.getInteger(R.styleable.Indicator_text_nomal_size, 12);
mTextPress = array.getInteger(R.styleable.Indicator_text_press_size, 13);
mText_Nomal = array.getColor(R.styleable.Indicator_text_nomal_color, Color.GRAY);
mText_Press = array.getColor(R.styleable.Indicator_text_press_color, Color.BLACK);
mSelected = array.getInteger(R.styleable.Indicator_selected, 0);
isFull = array.getBoolean(R.styleable.Indicator_isFull, false);
mAnimationTime = array.getInteger(R.styleable.Indicator_speed, 300);
mBai = array.getFloat(R.styleable.Indicator_multiply, (float) 1.2);
mHeight = array.getInteger(R.styleable.Indicator_line_hegith, 5);
for (int i = 0; i < mList.length; i++) {
mListTitle.add(mList);
}
array.recycle();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (!mIsCheck) {
mIsCheck = true;
initView();
}
}
/**
*/
private void initView() {
measure(0, 0);
//获取每个textview布局所占的宽度
mContWidth = getWidth() / mListTitle.size();
//添加线
mLine = new View(getContext());
addView(mLine);
int mWeight;
if (isFull) {
mWeight = mContWidth;
} else {
mWeight = (int) (setLineLength(mListTitle.get(mSelected)) * mBai);
if (mWeight > mContWidth) {
mWeight = mContWidth;
}
}
//设置线的基本属性
LayoutParams mLayoutParams1 = new LayoutParams(mWeight, mHeight);
mLayoutParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mLine.setLayoutParams(mLayoutParams1);
mLine.setBackgroundColor(mColor);
//获取上一次所在位置
mEndAddress = mContWidth * mSelected + (mContWidth - mWeight) / 2;
//添加textview
for (int i = 0; i < mListTitle.size(); i++) {
addTextView(i);
}
//初始化点击事件
setListener();
}
/**
*/
private void addTextView(int i) {
//初始化textview
TextView textView = new TextView(getContext());
textView.setText(mListTitle.get(i));
//设置textview基本属性
LayoutParams mLayoutParams = new LayoutParams(mContWidth, LayoutParams.MATCH_PARENT);
mLayoutParams.leftMargin = mContWidth * i;
mLayoutParams.addRule(CENTER_VERTICAL);
textView.setLayoutParams(mLayoutParams);
textView.setGravity(Gravity.CENTER);
if (i == mSelected) {
textView.setTextColor(mText_Press);
textView.setTextSize(mTextPress);
} else {
textView.setTextColor(mText_Nomal);
textView.setTextSize(mTextNomal);
}
//添加textview到布局
mTextList.add(textView);
addView(textView);
}
/**
*/
private void resetColor() {
for (int i = 0; i < mTextList.size(); i++) {
mTextList.get(i).setTextColor(getContext().getResources().getColor(R.color.text_hint));
}
}
/**
*/
private void setListener() {
setAnimation(0, mEndAddress);
for (int i = 0; i < mTextList.size(); i++) {
final int finalI = i;
mTextList.get(i).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (finalI != mSelected && mProhibitPisition != finalI) {
setChanger(finalI);
}
if (mOnIndiacatorClickListener != null)
mOnIndiacatorClickListener.onClick(finalI, v);
}
});
}
}
/**
- 动画
- @param statX 开始位置
- @param endX 结束位置
*/
private void setAnimation(int statX, int endX) {
AnimationSet mSet = new AnimationSet(true);
TranslateAnimation translate1 = new TranslateAnimation(
statX, endX, 0, 0);
mSet.addAnimation(translate1);
mSet.setFillAfter(true);
mSet.setDuration(mAnimationTime);
mLine.startAnimation(mSet);
}
private OnIndiacatorClickListener mOnIndiacatorClickListener;
/**
*/
private int setLineLength(String mTextView) {
return ConvertUtils.dp2px(mTextView.length() * mTextPress);
}
/**
*/
public void add(CharSequence charSequence, int position) {
removeAllViews();
mTextList.clear();
if (mListTitle.size() == position)
mListTitle.set(position - 1, charSequence.toString());
else
mListTitle.add(charSequence.toString());
initView();
}
/**
*/
public void setFull() {
isFull = true;
}
/**
- 设置监听
- @param onIndiacatorClickListener
*/
public void setIndiacatorListener(OnIndiacatorClickListener onIndiacatorClickListener) {
if (onIndiacatorClickListener != null) {
this.mOnIndiacatorClickListener = onIndiacatorClickListener;
}
}
/
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的24套腾讯、字节跳动、阿里、百度2020-2021面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节**。
还有?高级架构技术进阶脑图、Android开发面试专题资料?帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
点击:
《Android架构视频+BAT面试专题PDF+学习笔记》即可免费获取~
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。
|
免责声明:
1. 本站所有资源来自网络搜集或用户上传,仅作为参考不担保其准确性!
2. 本站内容仅供学习和交流使用,版权归原作者所有!© 查看更多
3. 如有内容侵害到您,请联系我们尽快删除,邮箱:kf@codeae.com
|