Skip to content

Commit

Permalink
增加条目触摸背景的设置,可以增加触摸背景色变化和水波纹效果
Browse files Browse the repository at this point in the history
  • Loading branch information
chaychan committed Jun 28, 2017
1 parent c136a18 commit 8a58758
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions demo/src/main/res/drawable-v21/selector_bg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/selector_grey">
<item android:drawable="@color/tab_gb"/>
</ripple>
5 changes: 5 additions & 0 deletions demo/src/main/res/drawable/selector_bg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/selector_grey" android:state_pressed="true"/>
<item android:drawable="@color/tab_gb"/>
</selector>
8 changes: 8 additions & 0 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>

<com.chaychan.library.BottomBarItem
Expand All @@ -48,6 +50,8 @@
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>


Expand All @@ -62,6 +66,8 @@
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>

<com.chaychan.library.BottomBarItem
Expand All @@ -75,6 +81,8 @@
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>

</com.chaychan.library.BottomBarLayout>
Expand Down
1 change: 1 addition & 0 deletions demo/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<color name="tab_gb">#F3F5F4</color>
<color name="tab_normal_color">#515051</color>
<color name="tab_selected_color">#D33D3C</color>

</resources>
20 changes: 20 additions & 0 deletions library/src/main/java/com/chaychan/library/BottomBarItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
Expand All @@ -28,9 +29,14 @@ public class BottomBarItem extends LinearLayout {
private int mTextColorNormal = 0xFF999999; //描述文本的默认显示颜色
private int mTextColorSelected = 0xFF46C01B; //述文本的默认选中显示颜色
private int mMarginTop = 0;//文字和图标的距离,默认0dp
private boolean mOpenTouchBg = false;// 是否开启触摸背景,默认关闭
private Drawable mTouchDrawable;//触摸时的背景

private TextView mTextView;
private ImageView mImageView;



public BottomBarItem(Context context) {
this(context, null);
}
Expand All @@ -56,6 +62,10 @@ public BottomBarItem(Context context, @Nullable AttributeSet attrs, int defStyle
mTextColorSelected = ta.getColor(R.styleable.BottomBarItem_textColorSelected, mTextColorSelected);

mMarginTop = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemMarginTop, UIUtils.dip2Px(mContext, mMarginTop));

mOpenTouchBg = ta.getBoolean(R.styleable.BottomBarItem_openTouchBg, mOpenTouchBg);
mTouchDrawable = ta.getDrawable(R.styleable.BottomBarItem_touchDrawable);

ta.recycle();

checkValues();
Expand All @@ -73,6 +83,11 @@ private void checkValues() {
if (mIconSelectedResourceId == -1) {
throw new IllegalStateException("您还没有设置选中状态下的图标,请指定iconSelected的图标");
}

if (mOpenTouchBg && mTouchDrawable == null){
//如果有开启触摸背景效果但是没有传对应的drawable
throw new IllegalStateException("开启了触摸效果,但是没有指定touchDrawable");
}
}

private void init() {
Expand All @@ -92,6 +107,11 @@ private void init() {
layoutParams.topMargin = mMarginTop;
mTextView.setLayoutParams(layoutParams);

if (mOpenTouchBg){
//如果有开启触摸背景
setBackground(mTouchDrawable);
}

addView(view);
}

Expand Down
4 changes: 4 additions & 0 deletions library/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
<attr name="textColorSelected" format="color"/>
<!--文字和图标的顶部距离-->
<attr name="itemMarginTop" format="dimension"/>
<!--是否开启触摸背景效果-->
<attr name="openTouchBg" format="boolean"/>
<!--设置触摸背景-->
<attr name="touchDrawable" format="reference"/>
</declare-styleable>
</resources>
5 changes: 5 additions & 0 deletions library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="selector_grey">#DDDDDD</color>
</resources>

0 comments on commit 8a58758

Please sign in to comment.