Skip to content

Commit

Permalink
BottomBarItem添加设置drawable的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
ccw committed Jan 21, 2019
1 parent 05db58e commit b771cd1
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 55 deletions.
8 changes: 4 additions & 4 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ android {
}

dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
api "com.android.support:appcompat-v7:$android_support_version"
api 'com.android.support.constraint:constraint-layout:1.0.1'
testImplementation 'junit:junit:4.12'
api project(':library')
implementation "com.android.support:appcompat-v7:$android_support_version"
implementation 'com.android.support.constraint:constraint-layout:1.0.1'
implementation project(':library')
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ public void onItemSelected(final BottomBarItem bottomBarItem, int previousPositi
//如果是第一个,即首页
if (previousPosition == currentPosition) {
//如果是在原来位置上点击,更换首页图标并播放旋转动画
bottomBarItem.setSelectedIconResourceId(R.mipmap.tab_loading);//更换成加载图标
bottomBarItem.setStatus(true);
if (mRotateAnimation != null && !mRotateAnimation.hasEnded()){
//如果当前动画正在执行
return;
}

bottomBarItem.setSelectedIcon(R.mipmap.tab_loading);//更换成加载图标 setResId

//播放旋转动画
if (mRotateAnimation == null) {
Expand All @@ -102,8 +106,7 @@ public void onItemSelected(final BottomBarItem bottomBarItem, int previousPositi
@Override
public void run() {
boolean tabNotChanged = mBottomBarLayout.getCurrentItem() == currentPosition; //是否还停留在当前页签
bottomBarItem.setSelectedIconResourceId(R.mipmap.tab_home_selected);//更换成首页原来选中图标
bottomBarItem.setStatus(tabNotChanged);//刷新图标
bottomBarItem.setSelectedIcon(R.mipmap.tab_home_selected);//更换成首页原来选中图标
cancelTabLoading(bottomBarItem);
}
}, 3000);
Expand All @@ -113,7 +116,7 @@ public void run() {

//如果点击了其他条目
BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
bottomItem.setSelectedIconResourceId(R.mipmap.tab_home_selected);//更换为原来的图标
bottomItem.setSelectedIcon(R.mipmap.tab_home_selected);//更换为原来的图标
cancelTabLoading(bottomItem);//停止旋转动画
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ public void onItemSelected(final BottomBarItem bottomBarItem, int previousPositi
//如果是第一个,即首页
if (previousPosition == currentPosition) {
//如果是在原来位置上点击,更换首页图标并播放旋转动画
bottomBarItem.setSelectedIconResourceId(R.mipmap.tab_loading);//更换成加载图标
bottomBarItem.setStatus(true);
if (mRotateAnimation != null && !mRotateAnimation.hasEnded()){
//如果当前动画正在执行
return;
}

bottomBarItem.setSelectedIcon(R.mipmap.tab_loading);//更换成加载图标

//播放旋转动画
if (mRotateAnimation == null) {
Expand All @@ -102,8 +106,7 @@ public void onItemSelected(final BottomBarItem bottomBarItem, int previousPositi
@Override
public void run() {
boolean tabNotChanged = mBottomBarLayout.getCurrentItem() == currentPosition; //是否还停留在当前页签
bottomBarItem.setSelectedIconResourceId(R.mipmap.tab_home_selected);//更换成首页原来选中图标
bottomBarItem.setStatus(tabNotChanged);//刷新图标
bottomBarItem.setSelectedIcon(R.mipmap.tab_home_selected);//更换成首页原来选中图标
cancelTabLoading(bottomBarItem);
}
}, 3000);
Expand All @@ -113,7 +116,7 @@ public void run() {

//如果点击了其他条目
BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
bottomItem.setSelectedIconResourceId(R.mipmap.tab_home_selected);//更换为原来的图标
bottomItem.setSelectedIcon(R.mipmap.tab_home_selected);//更换为原来的图标

cancelTabLoading(bottomItem);//停止旋转动画
}
Expand Down
12 changes: 4 additions & 8 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion min_sdk_version
targetSdkVersion target_sdk_version
versionCode 5
versionName "1.2.0"
versionCode 6
versionName "1.2.1"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand All @@ -22,10 +22,6 @@ android {
}

dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testImplementation 'junit:junit:4.12'
api "com.android.support:appcompat-v7:$android_support_version"
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "com.android.support:appcompat-v7:$android_support_version"
}
75 changes: 47 additions & 28 deletions library/src/main/java/com/chaychan/library/BottomBarItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
public class BottomBarItem extends LinearLayout {

private Context context;
private int normalIconResourceId;//普通状态图标的资源id
private int selectedIconResourceId;//选中状态图标的资源id
private Drawable normalIcon;//普通状态图标的资源id
private Drawable selectedIcon;//选中状态图标的资源id
private String title;//文本
private int titleTextSize = 12;//文字大小 默认为12sp
private int titleNormalColor; //描述文本的默认显示颜色
Expand Down Expand Up @@ -79,8 +79,8 @@ public BottomBarItem(Context context, @Nullable AttributeSet attrs, int defStyle
}

private void initAttrs(TypedArray ta) {
normalIconResourceId = ta.getResourceId(R.styleable.BottomBarItem_iconNormal, -1);
selectedIconResourceId = ta.getResourceId(R.styleable.BottomBarItem_iconSelected, -1);
normalIcon = ta.getDrawable(R.styleable.BottomBarItem_iconNormal);
selectedIcon = ta.getDrawable(R.styleable.BottomBarItem_iconSelected);

title = ta.getString(R.styleable.BottomBarItem_itemText);
titleTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemTextSize, UIUtils.sp2px(context, titleTextSize));
Expand Down Expand Up @@ -114,17 +114,17 @@ private void initAttrs(TypedArray ta) {
* 检查传入的值是否完善
*/
private void checkValues() {
if (normalIconResourceId == -1) {
throw new IllegalStateException("您还没有设置默认状态下的图标,请指定iconNormal的图标");
if (normalIcon == null) {
throw new IllegalStateException("You have not set the normal icon");
}

if (selectedIconResourceId == -1) {
throw new IllegalStateException("您还没有设置选中状态下的图标,请指定iconSelected的图标");
if (selectedIcon == null) {
throw new IllegalStateException("You have not set the selected icon");
}

if (openTouchBg && touchDrawable == null) {
//如果有开启触摸背景效果但是没有传对应的drawable
throw new IllegalStateException("开启了触摸效果,但是没有指定touchDrawable");
throw new IllegalStateException("Touch effect is turned on, but touchDrawable is not specified");
}

if (unreadTextBg == null) {
Expand All @@ -146,7 +146,7 @@ private void init() {

View view = initView();

mImageView.setImageResource(normalIconResourceId);
mImageView.setImageDrawable(normalIcon);

if (iconWidth != 0 && iconHeight != 0) {
//如果有设置图标的宽度和高度,则设置ImageView的宽高
Expand Down Expand Up @@ -206,17 +206,32 @@ public TextView getTextView() {
return mTextView;
}

public void setNormalIconResourceId(int mIconNormalResourceId) {
this.normalIconResourceId = mIconNormalResourceId;
public void setNormalIcon(Drawable normalIcon) {
this.normalIcon = normalIcon;
refreshTab();
}

public void setSelectedIconResourceId(int mIconSelectedResourceId) {
this.selectedIconResourceId = mIconSelectedResourceId;
public void setNormalIcon(int resId){
setNormalIcon(UIUtils.getDrawable(context,resId));
}

public void setStatus(boolean isSelected) {
mImageView.setImageDrawable(getResources().getDrawable(isSelected ? selectedIconResourceId : normalIconResourceId));
mTextView.setTextColor(isSelected ? titleSelectedColor : titleNormalColor);
public void setSelectedIcon(Drawable selectedIcon) {
this.selectedIcon = selectedIcon;
refreshTab();
}

public void setSelectedIcon(int resId){
setSelectedIcon(UIUtils.getDrawable(context,resId));
}

public void refreshTab(boolean isSelected) {
setSelected(isSelected);
refreshTab();
}

public void refreshTab() {
mImageView.setImageDrawable(isSelected() ? selectedIcon : normalIcon);
mTextView.setTextColor(isSelected() ? titleSelectedColor : titleNormalColor);
}

private void setTvVisible(TextView tv) {
Expand Down Expand Up @@ -266,8 +281,8 @@ public void hideNotify() {

public BottomBarItem create(Builder builder) {
this.context = builder.context;
this.normalIconResourceId = builder.normalIconResourceId;
this.selectedIconResourceId = builder.selectedIconResourceId;
this.normalIcon = builder.normalIcon;
this.selectedIcon = builder.selectedIcon;
this.title = builder.title;
this.titleTextSize = builder.titleTextSize;
this.titleNormalColor = builder.titleNormalColor;
Expand All @@ -294,8 +309,8 @@ public BottomBarItem create(Builder builder) {

public static final class Builder {
private Context context;
private int normalIconResourceId;//普通状态图标的资源id
private int selectedIconResourceId;//选中状态图标的资源id
private Drawable normalIcon;//普通状态图标的资源id
private Drawable selectedIcon;//选中状态图标的资源id
private String title;//标题
private int titleTextSize;//字体大小
private int titleNormalColor; //描述文本的默认显示颜色
Expand Down Expand Up @@ -330,16 +345,16 @@ public Builder(Context context) {
/**
* Sets the default icon's resourceId
*/
public Builder normalIcon(int normalIcon) {
normalIconResourceId = normalIcon;
public Builder normalIcon(Drawable normalIcon) {
this.normalIcon = normalIcon;
return this;
}

/**
* Sets the selected icon's resourceId
*/
public Builder selectedIcon(int selectedIcon) {
selectedIconResourceId = selectedIcon;
public Builder selectedIcon(Drawable selectedIcon) {
this.selectedIcon = selectedIcon;
return this;
}

Expand Down Expand Up @@ -499,15 +514,19 @@ public Builder notifyPointBg(Drawable notifyPointBg) {
/**
* Create a BottomBarItem object
*/
public BottomBarItem create(int normalIcon, int selectedIcon, String text) {
normalIconResourceId = normalIcon;
selectedIconResourceId = selectedIcon;
public BottomBarItem create(Drawable normalIcon, Drawable selectedIcon, String text) {
this.normalIcon = normalIcon;
this.selectedIcon = selectedIcon;
title = text;

BottomBarItem bottomBarItem = new BottomBarItem(context);
return bottomBarItem.create(this);
}

public BottomBarItem create(int normalIconId, int selectedIconId, String text) {
return create(UIUtils.getDrawable(context,normalIconId),UIUtils.getDrawable(context,selectedIconId),text);
}

private int getColor(int colorId){
return context.getResources().getColor(colorId);
}
Expand Down
10 changes: 5 additions & 5 deletions library/src/main/java/com/chaychan/library/BottomBarLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void init() {
}

if (mCurrentItem < mItemViews.size())
mItemViews.get(mCurrentItem).setStatus(true);//设置选中项
mItemViews.get(mCurrentItem).refreshTab(true);

if (mViewPager != null) {
mViewPager.setOnPageChangeListener(this);
Expand Down Expand Up @@ -118,7 +118,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
@Override
public void onPageSelected(int position) {
resetState();
mItemViews.get(position).setStatus(true);
mItemViews.get(position).refreshTab(true);
if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelected(getBottomItem(position), mCurrentItem, position);
}
Expand Down Expand Up @@ -165,15 +165,15 @@ public void onClick(View v) {
private void updateTabState(int position) {
resetState();
mCurrentItem = position;
mItemViews.get(mCurrentItem).setStatus(true);
mItemViews.get(mCurrentItem).refreshTab(true);
}

/**
* 重置当前按钮的状态
*/
private void resetState() {
if (mCurrentItem < mItemViews.size()) {
mItemViews.get(mCurrentItem).setStatus(false);
mItemViews.get(mCurrentItem).refreshTab(false);
}
}

Expand Down Expand Up @@ -269,7 +269,7 @@ protected void onRestoreInstanceState(Parcelable state) {
//重置所有按钮状态
resetState();
//恢复点击的条目颜色
mItemViews.get(mCurrentItem).setStatus(true);
mItemViews.get(mCurrentItem).refreshTab(true);
super.onRestoreInstanceState(bundle.getParcelable(STATE_INSTANCE));
} else {
super.onRestoreInstanceState(state);
Expand Down
5 changes: 5 additions & 0 deletions library/src/main/java/com/chaychan/library/UIUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chaychan.library;

import android.content.Context;
import android.graphics.drawable.Drawable;

/**
* @author chaychan
Expand Down Expand Up @@ -35,4 +36,8 @@ public static int sp2px(Context context,float spValue) {
public static int getColor(Context context,int colorId){
return context.getResources().getColor(colorId);
}

public static Drawable getDrawable(Context context,int resId){
return context.getResources().getDrawable(resId);
}
}

0 comments on commit b771cd1

Please sign in to comment.