diff --git a/demo/build.gradle b/demo/build.gradle index f5286b1..2e36187 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -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') } diff --git a/demo/src/main/java/com/chaychan/bottombarlayout/FragmentManagerActivity.java b/demo/src/main/java/com/chaychan/bottombarlayout/FragmentManagerActivity.java index c5c829a..e565938 100644 --- a/demo/src/main/java/com/chaychan/bottombarlayout/FragmentManagerActivity.java +++ b/demo/src/main/java/com/chaychan/bottombarlayout/FragmentManagerActivity.java @@ -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) { @@ -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); @@ -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);//停止旋转动画 } }); diff --git a/demo/src/main/java/com/chaychan/bottombarlayout/ViewPagerActivity.java b/demo/src/main/java/com/chaychan/bottombarlayout/ViewPagerActivity.java index 0fc8783..40b385d 100644 --- a/demo/src/main/java/com/chaychan/bottombarlayout/ViewPagerActivity.java +++ b/demo/src/main/java/com/chaychan/bottombarlayout/ViewPagerActivity.java @@ -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) { @@ -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); @@ -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);//停止旋转动画 } diff --git a/library/build.gradle b/library/build.gradle index 3a6ba59..33c512f 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -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" @@ -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" } diff --git a/library/src/main/java/com/chaychan/library/BottomBarItem.java b/library/src/main/java/com/chaychan/library/BottomBarItem.java index 83d2f9a..5120066 100644 --- a/library/src/main/java/com/chaychan/library/BottomBarItem.java +++ b/library/src/main/java/com/chaychan/library/BottomBarItem.java @@ -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; //描述文本的默认显示颜色 @@ -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)); @@ -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) { @@ -146,7 +146,7 @@ private void init() { View view = initView(); - mImageView.setImageResource(normalIconResourceId); + mImageView.setImageDrawable(normalIcon); if (iconWidth != 0 && iconHeight != 0) { //如果有设置图标的宽度和高度,则设置ImageView的宽高 @@ -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) { @@ -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; @@ -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; //描述文本的默认显示颜色 @@ -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; } @@ -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); } diff --git a/library/src/main/java/com/chaychan/library/BottomBarLayout.java b/library/src/main/java/com/chaychan/library/BottomBarLayout.java index dd94fbe..2c7037d 100644 --- a/library/src/main/java/com/chaychan/library/BottomBarLayout.java +++ b/library/src/main/java/com/chaychan/library/BottomBarLayout.java @@ -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); @@ -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); } @@ -165,7 +165,7 @@ public void onClick(View v) { private void updateTabState(int position) { resetState(); mCurrentItem = position; - mItemViews.get(mCurrentItem).setStatus(true); + mItemViews.get(mCurrentItem).refreshTab(true); } /** @@ -173,7 +173,7 @@ private void updateTabState(int position) { */ private void resetState() { if (mCurrentItem < mItemViews.size()) { - mItemViews.get(mCurrentItem).setStatus(false); + mItemViews.get(mCurrentItem).refreshTab(false); } } @@ -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); diff --git a/library/src/main/java/com/chaychan/library/UIUtils.java b/library/src/main/java/com/chaychan/library/UIUtils.java index 8b11a9e..86f55b2 100644 --- a/library/src/main/java/com/chaychan/library/UIUtils.java +++ b/library/src/main/java/com/chaychan/library/UIUtils.java @@ -1,6 +1,7 @@ package com.chaychan.library; import android.content.Context; +import android.graphics.drawable.Drawable; /** * @author chaychan @@ -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); + } }