/**
* 初始化资源图片bitmap及相关绘制对象
* @param normal normals
* @param selected focus
*/
public final void init(int normal, int selected, int width, int height) {
this.mNormalIcon = createBitmap(normal);
this.mSelectedIcon = createBitmap(selected);
this.mNormalRect = new Rect(0, 0, width, height);
this.mSelectedRect = new Rect(0, 0, width, height);
this.mPaint = new Paint(1);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (this.mPaint == null) {
return;
}
this.mPaint.setAlpha(255 - this.mSelectedAlpha);
canvas.drawBitmap(this.mNormalIcon, null, this.mNormalRect, this.mPaint);
this.mPaint.setAlpha(this.mSelectedAlpha);
canvas.drawBitmap(this.mSelectedIcon, null, this.mSelectedRect, this.mPaint);
}
public void initContainer (String[] titles, int[][] iconsRes, int[] colors, boolean showTransitionColor) {
this.mTitles = titles;
this.mIconRes = iconsRes;
this.mTextNormalColor = getResources().getColor(colors[0]);
this.mTextSelectedColor = getResources().getColor(colors[1]);
this.mShowTransitionColor = showTransitionColor;
}
/**
* 设置布局文件及相关控件id
* @param layout layout布局文件 id
* @param iconId ImageView 控件 id id <=0 时不显示
* @param textId TextView 控件 id id <=0 时不显示
* @param width icon 宽度
* @param height icon 高度
*/
public void setContainerLayout (int layout, int iconId, int textId, int width, int height) {
mLayoutId = layout;
mTextViewId = textId;
mIconVIewId = iconId;
mIconWidth = width;
mIconHeight = height;
}
/**
* <p>添加tab view到当前容器</p>
*/
private void addTabViewToContainer() {
final PagerAdapter adapter = mViewPager.getAdapter();
mTabView = new View[adapter.getCount()]; //这里根据adapter判断底部要显示的tab总数
for (int index = 0, len = adapter.getCount(); index < len; index++) {
final View tabView = LayoutInflater.from(getContext()).inflate(mLayoutId, this, false); //加载tab布局
mTabView[index] = tabView;
/*tabIconView初始化*/
TabIconView iconView = null;
if (mIconVIewId > 0) { // 传入的图片资源文件ID不为0时,表示需要显示icon,然后初始化该View
iconView = (TabIconView) tabView.findViewById(mIconVIewId);
iconView.init(mIconRes[index][0], mIconRes[index][1], mIconWidth, mIconHeight); //这里调了自定义ImageView的init方法
}
/*tabTextView初始化*/
TextView textView = null;
if (mTextViewId > 0) {
textView = (TextView) tabView.findViewById(mTextViewId);
textView.setText(mTitles[index]);
}
/*设置宽度,等分container*/
LayoutParams lp = (LayoutParams) tabView.getLayoutParams();
lp.width = 0;
lp.weight = 1;
/*添加tab点击事件*/
addTabOnClickListener(tabView, index);
/*设置当前状态*/
if (index == mViewPager.getCurrentItem()) { //当先显示tab,设置初始状态为获得焦点状态
if (iconView != null) {
iconView.offsetChanged(0);
}
tabView.setSelected(true);
if (textView != null) {
textView.setTextColor(mTextSelectedColor);
}
}
addView(tabView);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
final int childCount = getChildCount();
if (childCount > 0) {
/*当发生偏移时,绘制渐变区域*/
if (mSelectionOffset > 0f && mSelectedPosition < (getChildCount() - 1) && mShowTransitionColor) {
/*获取当前tab和下一tab view */
View selectedTab = getChildAt(mSelectedPosition);
View nextTab = getChildAt(mSelectedPosition + 1);
/*显示tab icon时,刷新各自view 透明度*/
if (mIconVIewId > 0) {
View selectedIconView = selectedTab.findViewById(mIconVIewId);
View nextIconView = nextTab.findViewById(mIconVIewId);
//draw icon alpha
if (selectedIconView instanceof TabIconView && nextIconView instanceof TabIconView) {
((TabIconView) selectedIconView).offsetChanged(mSelectionOffset);
((TabIconView) nextIconView).offsetChanged(1 - mSelectionOffset);
}
}
/*显示tab text,刷新各自view 透明度*/
if (mTextViewId > 0) {
View selectedTextView = selectedTab.findViewById(mTextViewId);
View nextTextView = nextTab.findViewById(mTextViewId);
//draw text color
Integer selectedColor = (Integer) evaluate(mSelectionOffset, mTextSelectedColor, mTextNormalColor);
Integer nextColor = (Integer) evaluate(1 - mSelectionOffset, mTextSelectedColor, mTextNormalColor);
if (selectedTextView instanceof TextView && nextTextView instanceof TextView) {
((TextView) selectedTextView).setTextColor(selectedColor);
((TextView) nextTextView).setTextColor(nextColor);
}
}
}
}
}
private void initViews() { //得到apdater
TabFragmentAdapter mAdapter = new TabFragmentAdapter(getSupportFragmentManager(), fragments);
ViewPager mPager = (ViewPager) findViewById(R.id.tab_pager);
mPager.setAdapter(mAdapter);
//如果当前类需要对viewPager做监听
TabContainerView mTabLayout = (TabContainerView) findViewById(R.id.ll_tab_container);
mTabLayout.setOnPageChangeListener(this);
mTabLayout.initContainer(getResources().getStringArray(R.array.tab_main_title), ICONS_RES, TAB_COLORS, true);
int width = getResources().getDimensionPixelSize(R.dimen.tab_icon_width);
int height = getResources().getDimensionPixelSize(R.dimen.tab_icon_height);
mTabLayout.setContainerLayout(R.layout.tab_container_view, R.id.iv_tab_icon, R.id.tv_tab_text, width, height);
// mTabLayout.setSingleTextLayout(R.layout.tab_container_view, R.id.tv_tab_text);
// mTabLayout.setSingleIconLayout(R.layout.tab_container_view, R.id.iv_tab_icon);
mTabLayout.setViewPager(mPager);
mPager.setCurrentItem(getIntent().getIntExtra("tab", 0));
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有