<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
今天給大家帶來一個自定義的底部導航,我不會做動圖,只能搞一張圖片給大家看看,大家見諒
這個就是自定義的tablayout底部搞好的樣式了
TabLayout提供了一個水平佈局用於展示tabs,繼承自HorizontalScrollView。一般與Viewpager結合使用實現頁面和標籤聯動的效果,是時下APP中非常常用的一個控制元件
首先我們需要建立一個類或者是模組都可以
package com.example.map.tab; import android.content.Context; import android.os.Bundle; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentTabHost; import com.example.map.R; import java.util.ArrayList; import java.util.List; /** * Created by long on 2022/5/10. * 擴充套件TabHost */ public class XFragmentTabHost extends FragmentTabHost { private Context mContext; private List<View> mTabViews; private List<TabItem> mTabItems; // 字型啟用顏色 private int mTextActiveColor; private int mTextInactiveColor; // 字型啟用大小 private float mTextActiveSize; private float mTextInactiveSize; // 檢視啟用對頂部的偏移 private int mViewActivePaddingTop; private int mViewInactivePaddingTop; // 波紋模式的前景顏色和後景顏色 private int mFrontColor; private int mBehindColor; // TabHost模式 private TabMode mTabMode; public XFragmentTabHost(Context context) { super(context); _init(context); } public XFragmentTabHost(Context context, AttributeSet attrs) { super(context, attrs); _init(context); } private void _init(Context context) { mTabViews = new ArrayList<>(); mTabItems = new ArrayList<>(); mContext = context; mTextActiveColor = ContextCompat.getColor(mContext, R.color.colorActive); mTextInactiveColor = ContextCompat.getColor(mContext, R.color.colorInactive); mFrontColor = ContextCompat.getColor(mContext, R.color.colorFront); mBehindColor = ContextCompat.getColor(mContext, R.color.colorBehind); mTextActiveSize = getResources().getDimension(R.dimen.tab_text_size_active); mTextInactiveSize = getResources().getDimension(R.dimen.tab_text_size_inactive); mViewActivePaddingTop = (int) getResources().getDimension(R.dimen.tab_padding_top_active); mViewInactivePaddingTop = (int) getResources().getDimension(R.dimen.tab_padding_top_inactive); mTabMode = TabMode.MoveToTop; } /** * 覆寫父類別介面,並在這裡做些動畫特效 * @param index 當前選中的Tab項 */ @Override public void setCurrentTab(int index) { // 獲取之前選中的index int lastIndex = getCurrentTab(); super.setCurrentTab(index); // 選中不同的Tab項才做切換處理 if (lastIndex != index) { _switchTab(lastIndex, index); } } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); // 部分機型TabHost帶有分割線,同一將分割線設為透明 getTabWidget().setDividerDrawable(android.R.color.transparent); } /** * 新增TabItem * @param item TabItem * @param fragClass fragment類名 * @param bundle 傳給fragment的引數 */ public void addTabItem(TabItem item, Class<?> fragClass, Bundle bundle) { mTabItems.add(item); View view = _getIndicator(item); mTabViews.add(view); this.addTab(newTabSpec(item.getTitle()).setIndicator(view), fragClass, bundle); } /** * 獲取TabItem檢視 * @param item TabItem * @return */ private View _getIndicator(TabItem item) { View view = LayoutInflater.from(mContext).inflate(R.layout.tab_indicator, null); ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon); TextView title = (TextView) view.findViewById(R.id.tab_title); imageView.setImageResource(item.getImageRes()); title.setText(item.getTitle()); title.setTextColor(mTextInactiveColor); return view; } /** * 切換Tab * @param lastIndex 上一個選中索引 * @param nextIndex 下一個選中索引 */ private void _switchTab(int lastIndex, int nextIndex) { for (int i = 0; i < mTabViews.size(); i++) { if (i == lastIndex) { _switchView(i, false); } else if (i == nextIndex) { _switchView(i, true); } } } /** * 切換檢視 * @param index 索引 * @param isActivated 是否啟用 */ private void _switchView(int index, boolean isActivated) { switch (mTabMode) { case MoveToTop: _doMoveToTop(index, isActivated); break; case ClipDrawable: _doClipDrawable(index, isActivated); break; case Ripple: _doRipple(index, isActivated); break; } } /** * 背景展開處理 * @param index 索引 * @param isActivated 是否啟用 */ private void _doClipDrawable(int index, boolean isActivated) { View view = mTabViews.get(index); View tabView = view.findViewById(R.id.tab_layout); if (isActivated) { TabAnimHelper.clipDrawable(tabView, mTabItems.get(index).getDrawable(), true); } else { TabAnimHelper.clipDrawable(tabView, mTabItems.get(index).getDrawable(), false); } } /** * 波紋處理 * @param index 索引 * @param isActivated 是否啟用 */ private void _doRipple(int index, boolean isActivated) { View view = mTabViews.get(index); View tabView = view.findViewById(R.id.tab_layout); TextView title = (TextView) view.findViewById(R.id.tab_title); if (index == 0) { TabAnimHelper.rippleDrawable(tabView, mFrontColor, mBehindColor, RippleDrawable.MODE_LEFT, isActivated); } else if (index == (mTabViews.size() - 1)){ TabAnimHelper.rippleDrawable(tabView, mFrontColor, mBehindColor, RippleDrawable.MODE_RIGHT, isActivated); } else { TabAnimHelper.rippleDrawable(tabView, mFrontColor, mBehindColor, RippleDrawable.MODE_MIDDLE, isActivated); } if (isActivated) { title.setTextColor(mTextActiveColor); } else { title.setTextColor(mTextInactiveColor); } } /** * 上移動畫處理 * @param index 索引 * @param isActivated 是否啟用 */ private void _doMoveToTop(int index, boolean isActivated) { View view = mTabViews.get(index); TextView title = (TextView) view.findViewById(R.id.tab_title); ImageView icon = (ImageView) view.findViewById(R.id.tab_icon); if (isActivated) { TabAnimHelper.changeTextColor(title, mTextInactiveColor, mTextActiveColor); TabAnimHelper.changeTextSize(title, mTextInactiveSize, mTextActiveSize); TabAnimHelper.changeTopPadding(view, mViewInactivePaddingTop, mViewActivePaddingTop); TabAnimHelper.changeImageSize(icon, 1.0f, 1.1f); } else { TabAnimHelper.changeTextColor(title, mTextActiveColor, mTextInactiveColor); TabAnimHelper.changeTextSize(title, mTextActiveSize, mTextInactiveSize); TabAnimHelper.changeTopPadding(view, mViewActivePaddingTop, mViewInactivePaddingTop); TabAnimHelper.changeImageSize(icon, 1.1f, 1.0f); } } /** * 屬性設定 * @return */ public int getTextActiveColor() { return mTextActiveColor; } public void setTextActiveColor(int textActiveColor) { mTextActiveColor = textActiveColor; } public int getTextInactiveColor() { return mTextInactiveColor; } public void setTextInactiveColor(int textInactiveColor) { mTextInactiveColor = textInactiveColor; } public int getFrontColor() { return mFrontColor; } public void setFrontColor(int frontColor) { mFrontColor = frontColor; } public int getBehindColor() { return mBehindColor; } public void setBehindColor(int behindColor) { mBehindColor = behindColor; } public TabMode getTabMode() { return mTabMode; } public void setTabMode(TabMode tabMode) { mTabMode = tabMode; } /** * Tab的模式 * MoveToTop:向上偏移 * Ripple:波紋 * ClipDrawable:逐步展示背景 */ public enum TabMode { MoveToTop(1), Ripple(2), ClipDrawable(3); private int tabMode; TabMode(int tabMode) { this.tabMode = tabMode; } public int getTabMode() { return tabMode; } } }
然後在res下的value中的color中加入以下顏色
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="black">#000000</color> <color name="white">#FFFFFF</color> <color name="custcolor">#FBD794</color> <color name="translate">#00000000</color> <color name="trans">#70000000</color> <color name="white_trans">#99ffffff</color> <color name="ef">#efefef</color> <color name="c4">#333333</color> <color name="c9">#999999</color> <color name="coloritem">#cccccc</color> <color name="black_bg">#66333333</color> <color name="c1">#111111</color> <color name="c6">#666666</color> <color name="c5">#eeeeee</color> <color name="c2">#ff6700</color> <color name="cfcc">#cccccc</color> <color name="app_color_f6">#f6f6f6</color> <color name="btn_not_click_color">#a8a8a8</color> <color name="btn_click_color">#dc5900</color> <color name="c7">#171717</color> <color name="c3">#cccccc</color> <color name="c33">#333333</color> <color name="test_blue">#338BFF</color> <color name="red1">#f94244</color> <color name="green1">#45cdd5</color> <color name="blue">#5e8cef</color> <color name="blue1">#4980f6</color> <color name="c_c1e4ff">#c1e4ff</color> <color name="c_eff4f8">#eff4f8</color> <color name="e5">#e5e5e5</color> <color name="f6">#f6f6f6</color> <color name="red_normal">#FF0000</color> <color name="red_pressed">#FF6666</color> <color name="blue_bg">#e6fffe</color> <color name="blue_text">#00e8df</color> <color name="org_bg">#ffefe5</color> <color name="green_bg">#e6fff2</color> <color name="green_text">#01e48c</color> <color name="red_bg">#ffeeee</color> <color name="red_text">#ff1901</color> <color name="org1">#fff4ea</color> <color name="org2">#ffa96e</color> <color name="org3">#ff8430</color> <color name="org4">#ff9d5c</color> <color name="org5">#ff802c</color> <color name="org6">#f2c279</color> <color name="org7">#fcf1ea</color> <color name="org8">#fed2c0</color> <color name="org9">#ffece5</color> <color name="green2">#68ffc4</color> <color name="green">#31c088</color> <color name="blue2_trans">#587ed6ff</color> <color name="blue2">#7ed6ff</color> <color name="blue3">#75fff7</color> <color name="blue5">#2d9ef5</color> <color name="photopicker_background">#181819</color> <color name="color1">#00a6b6</color> <color name="c99">#282e35</color> <!-- QR_Code --> <color name="viewfinder_mask">#60000000</color> <color name="result_view">#b0000000</color> <color name="possible_result_points">#c0ffff00</color> <color name="result_image_border">#ffffffff</color> <color name="result_minor_text">#ffc0c0c0</color> <color name="result_points">#c000ff00</color> <color name="system_bar_color">#fed952</color> <color name="red">#eb2127</color> <color name="c_red">#ff4200</color> <color name="red3">#f03030</color> <color name="c_f8">#f8f8f8</color> <color name="c_yellow">#daba7f</color> <color name="c_85">#858585</color> <color name="c_b2">#b2b2b2</color> <color name="c_light_purple">#58ffc8d1</color> <color name="c_purple">#ff6e6b</color> <color name="c_agent_position">#97a8bc</color> <color name="c_agent_name">#143660</color> <color name="c_44db09">#44db09</color> <color name="c_75">#757575</color> <color name="c_f5">#f5f5f5</color> <color name="c_76">#767676</color> <color name="c_d1">#d1d1d1</color> <color name="c_d2">#d2d2d2</color> <color name="c_d3">#d3d3d3</color> <color name="c_yellow3">#ffba16</color> <color name="c_mei_red">#f73954</color> <color name="yellow">#ffbd0a</color> <color name="org">#ff6600</color> <color name="org_trans">#fff6f3</color> <color name="c_d">#dddddd</color> <color name="c_e0">#e0e0e0</color> <color name="red7">#f70c38</color> <color name="green7">#47b81b</color> <color name="deal_bg">#38c595</color> <color name="f1">#f1f1f1</color> <color name="f5">#f5f5f5</color> <color name="c_3c5c8c">#3c5c8c</color> <color name="cus_phone_color">#094691</color> <color name="cus_follow_record_item_name_color">#002553</color> <color name="praise_item_default">#333333</color> <color name="praise_item_selector_default">#cccccc</color> <color name="default_clickable_color">#cccccc</color> <color name="praise_item">#8290AF</color> <color name="im_font_color_text_hint">#abb1b6</color> <color name="ffd1c0">#ffd1c0</color> <color name="fdf0eb">#fdf0eb</color> <color name="e4e9ec">#e4e9ec</color> <color name="d3">#d3d3d3</color> <color name="ffae00">#ffae00</color> <color name="e9eef3">#e9eef3</color> <color name="ecf0f4">#ECF0F4</color> <color name="ac677">#1ac677</color> <color name="ecf1f7">#ECF1F7</color> <color name="cace6">#3cace6</color> <color name="d282">#35d282</color> <color name="ff2a00">#ff2a00</color> <!-- 其它背景色或分割線相關 --> <color name="bg_content">#f3f3f3</color> <color name="divider_line">#d9d9d9</color> <!-- 字型色 --> <color name="text_title">#1b1b1b</color> <color name="text_content">#818181</color> <color name="text_sub">#919191</color> <color name="text_hint">#c7c7c7</color> <color name="text_black">#404040</color> <color name="main_tab_bg">#fcfcfc</color> <color name="black_need">#333333</color> <color name="orange_need">#f0420c</color> <color name="gray_need">#BEC0C1</color> <color name="dialog_content_textcolor">#909090</color> <color name="reply_text_color">#666666</color> <color name="contents_text">#ff000000</color> <color name="encode_view">#ffffffff</color> <color name="help_button_view">#ffcccccc</color> <color name="help_view">#ff404040</color> <color name="result_text">#ffffffff</color> <color name="sbc_header_text">#ff808080</color> <color name="sbc_header_view">#ffffffff</color> <color name="sbc_list_item">#fffff0e0</color> <color name="sbc_layout_view">#ffffffff</color> <color name="sbc_page_number_text">#ff000000</color> <color name="sbc_snippet_text">#ff4b4b4b</color> <color name="share_text">#ff000000</color> <color name="share_view">#ffffffff</color> <color name="status_view">#50000000</color> <color name="status_text">#ffffffff</color> <color name="transparent">#00000000</color> <color name="viewfinder_frame">#ff000000</color> <color name="viewfinder_laser">#ffff0000</color> <!-- 畫TextView橫線顏色 --> <color name="draw_line_color">#D4D4D4</color> <!-- 側邊仿通訊錄26字母列表顏色 --> <color name="addresslist_textcolor">#A9A9A9</color> <color name="car_select_second_child">#999999</color> <color name="car_select_second_group_child_bg">#e1e1e1</color> <color name="map_poi_list_bg">#d3d3d3</color> <!-- 下拉PopupWindow --> <color name="pulldown_popupwindow_bg">#5191F2</color> <color name="butten_green">#1AAC19</color> <color name="check_butten_green">#19ad19</color> <color name="title1">#4a4a4a</color> <color name="title2">#a2a2a2</color> <color name="line">#d1d1d1</color> <color name="c_00c679">#00c679</color> <color name="F5F6F8">#F5F6F8</color> <color name="c65">#656565</color> <color name="fff0e5">#FFF0E5</color> <color name="f0f1f2">#F0F1F2</color> <color name="c7c9d1">#C7C9D1</color> <color name="f5f6f8">#f5f6f8</color> <color name="fd3341">#fd3341</color> <color name="dce1e5">#dce1e5</color> <color name="c_9498a4">#9498a4</color> <color name="c_202741">#202741</color> <color name="c_444861">#444861</color> <color name="c_e8">#e8e8e8</color> <color name="ff8963">#FF8963</color> <color name="c_2a3038">#2a3038</color> <color name="c_939">#9398AE</color> <color name="c_00a">#00A7FF</color> <color name="c_5c6166">#5c6166</color> <color name="c_e4e5e6">#e4e5e6</color> <color name="c_ffb452">#ffb452</color> <color name="c_ff7252">#ff7252</color> <color name="c8898A6">#8898A6</color> <color name="acb0b9">#ACB0B9</color> <color name="caccd0">#CACCD0</color> <color name="c_f0f1f3">#f0f1f3</color> <color name="eda95b40">#eda95b40</color> <color name="c_3257c5">#3257c5</color> <color name="c_9d9da3">#9d9da3</color> <color name="c_d7d8dc">#d7d8dc</color> <color name="c4c6cc">#c4c6cc</color> <color name="cdcfd5">#cdcfd5</color> <color name="d2d4d9">#d2d4d9</color> <color name="c_939499">#939499</color> <color name="c_616366">#616366</color> <color name="dc">#dcdcdc</color> <color name="cc0042ff">#cc0042ff</color> <color name="c_97a0ab">#97a0ab</color> <color name="ebba8f">#ebba8f</color> <color name="a86010">#a86010</color> <color name="colorActive">#00BCD4</color> <color name="colorInactive">#d2d0d1</color> <color name="colorFront">#4db6ac</color> <color name="colorBehind">#84ffff</color> </resources>
然後是我們的佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="@dimen/tab_height" android:minWidth="@dimen/tab_min_width" android:paddingTop="@dimen/tab_padding_top_inactive" android:paddingBottom="@dimen/tab_padding_bottom" android:background="?selectableItemBackgroundBorderless"> <ImageView android:id="@+id/tab_icon" android:layout_width="@dimen/tab_icon" android:layout_height="@dimen/tab_icon" android:layout_gravity="center_horizontal"/> <TextView android:id="@+id/tab_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:lines="1" android:text="首頁" android:textColor="@color/colorInactive" android:textSize="@dimen/tab_text_size_inactive"/> </LinearLayout>
佈局中呢會有一些圖片這個圖片的話就自己搞吧然後在res檔案下建立一個dimen
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="tab_elevation">8dp</dimen> <dimen name="tab_shadow_height">3dp</dimen> <dimen name="tab_shadow_height_without_colored_background">4dp</dimen> <dimen name="tab_min_width">104dp</dimen> <dimen name="tab_max_width">168dp</dimen> <dimen name="tab_height">56dp</dimen> <dimen name="tab_line_width">1dp</dimen> <dimen name="tab_icon">24dp</dimen> <dimen name="tab_padding_top_active">3dp</dimen> <dimen name="tab_padding_top_inactive">6dp</dimen> <dimen name="tab_padding_top_inactive_without_text">16dp</dimen> <dimen name="tab_padding_bottom">3dp</dimen> <dimen name="tab_padding_left">12dp</dimen> <dimen name="tab_padding_right">12dp</dimen> <dimen name="tab_text_size_active">14sp</dimen> <dimen name="tab_text_size_inactive">12sp</dimen> </resources>
我們的底部導航的樣式就搞完了,接下里就是我們的動畫效果寫一個自定義Drawable
package com.example.map.tab; import android.animation.ValueAnimator; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; /** * Created by long on 2022/5/10. * 波紋Drawable */ public class RippleDrawable extends Drawable implements Animatable { /** * 3種模式:左邊、中間和右邊波紋 */ public static final int MODE_LEFT = 1; public static final int MODE_MIDDLE = 2; public static final int MODE_RIGHT = 3; private int mMode = MODE_MIDDLE; // 前景色和後景色畫筆 private Paint mPaintFront; private Paint mPaintBehind; // 用來繪製扇形的矩形框 private RectF mRect; // 目標View的寬高的一半 private int mHalfWidth; private int mHalfHeight; // 擴散半徑 private int mRadius; // 前景色和背景色的分割距離 private int mDivideSpace; // 擴散滿檢視需要的距離,中點到斜角的距離 private int mFullSpace; // 動畫控制 private ValueAnimator mValueAnimator; public RippleDrawable(int frontColor, int behindColor, int mode) { mPaintFront = new Paint(Paint.ANTI_ALIAS_FLAG); mPaintFront.setColor(frontColor); mPaintBehind = new Paint(Paint.ANTI_ALIAS_FLAG); mPaintBehind.setColor(behindColor); mRect = new RectF(); mMode = mode; } @Override public void draw(Canvas canvas) { if (mRadius > mHalfWidth) { int count = canvas.save(); canvas.drawCircle(mHalfWidth, mHalfHeight, mHalfWidth, mPaintBehind); canvas.restoreToCount(count); count = canvas.save(); canvas.drawCircle(mHalfWidth, mHalfHeight, mDivideSpace, mPaintFront); canvas.restoreToCount(count); } else if (mRadius > mDivideSpace) { int count = canvas.save(); canvas.drawCircle(mHalfWidth, mHalfHeight, mRadius, mPaintBehind); canvas.restoreToCount(count); count = canvas.save(); canvas.drawCircle(mHalfWidth, mHalfHeight, mDivideSpace, mPaintFront); canvas.restoreToCount(count); } else { canvas.drawCircle(mHalfWidth, mHalfHeight, mRadius, mPaintFront); } // 左右兩邊才進行扇形繪製 if (mMode != MODE_MIDDLE) { mRect.left = mHalfWidth - mRadius; mRect.right = mHalfWidth + mRadius; mRect.top = mHalfHeight - mRadius; mRect.bottom = mHalfHeight + mRadius; } if (mMode == MODE_LEFT) { canvas.drawArc(mRect, 90, 180, true, mPaintFront); } else if (mMode == MODE_RIGHT) { canvas.drawArc(mRect, -90, 180, true, mPaintFront); } } @Override public void setAlpha(int alpha) { } @Override public void setColorFilter(ColorFilter colorFilter) { } @Override public int getOpacity() { return PixelFormat.RGBA_8888; } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mHalfHeight = (bounds.bottom - bounds.top) / 2; mHalfWidth = (bounds.right - bounds.left) / 2; mDivideSpace = Math.max(mHalfHeight, mHalfWidth) * 3 / 4; mFullSpace = (int) Math.sqrt(mHalfWidth * mHalfWidth + mHalfHeight * mHalfHeight); // 屬性動畫 mValueAnimator = ValueAnimator.ofInt(0, mFullSpace); mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mRadius = (int) animation.getAnimatedValue(); invalidateSelf(); } }); mValueAnimator.setDuration(200); start(); } @Override public void start() { mValueAnimator.start(); } @Override public void stop() { mValueAnimator.end(); } @Override public boolean isRunning() { return mValueAnimator != null && mValueAnimator.isRunning(); } }
然後建立我們的TabFragment
package com.example.map.tab; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import com.example.map.R; /** * Created by long on 2022/5/10. */ public class TabFragment extends Fragment { public static final String FRAG_KEY = "FragKey"; private TextView mFragTabText; private void assignViews(View view) { mFragTabText = (TextView) view.findViewById(R.id.frag_tab_text); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_tab, null); assignViews(view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (getArguments() != null) { String title = getArguments().getString(FRAG_KEY); mFragTabText.setText(title); } } }
然後建立一個動畫的幫助類
package com.example.map.tab; import android.animation.AnimatorSet; import android.animation.ArgbEvaluator; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.graphics.drawable.ClipDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.widget.ImageView; import android.widget.TextView; /** * Created by long on 2022/5/10. * Tab動畫幫助類 */ public class TabAnimHelper { /** * 改變Tab的頂部偏移 * @param view * @param fromPadding * @param toPadding */ public static void changeTopPadding(final View view, int fromPadding, int toPadding) { ValueAnimator animator = ValueAnimator.ofFloat(fromPadding, toPadding); animator.setDuration(150); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { float animatedValue = (float) valueAnimator.getAnimatedValue(); view.setPadding(view.getPaddingLeft(), (int) animatedValue, view.getPaddingRight(), view.getPaddingBottom()); } }); animator.start(); } /** * 改變字型大小 * @param textView * @param from * @param to */ public static void changeTextSize(final TextView textView, float from, float to) { ValueAnimator textSizeChangeAnimator = ValueAnimator.ofFloat(from, to); textSizeChangeAnimator.setDuration(150); textSizeChangeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) valueAnimator.getAnimatedValue()); } }); textSizeChangeAnimator.start(); } /** * 改變字型顏色 * @param textView * @param fromColor * @param toColor */ public static void changeTextColor(final TextView textView, int fromColor, int toColor) { ValueAnimator changeTextColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), fromColor, toColor); changeTextColorAnimation.setDuration(150); changeTextColorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animator) { textView.setTextColor((Integer) animator.getAnimatedValue()); } }); changeTextColorAnimation.start(); } /** * 改變檢視大小 * @param image * @param fromScale * @param toScale */ public static void changeImageSize(ImageView image, float fromScale, float toScale) { ObjectAnimator scaleX; ObjectAnimator scaleY; scaleX = ObjectAnimator.ofFloat(image, "scaleX", fromScale, toScale); scaleY = ObjectAnimator.ofFloat(image, "scaleY", fromScale, toScale); AnimatorSet set = new AnimatorSet(); set.setDuration(150); set.playTogether(scaleX, scaleY); set.start(); } /** * 從中心展開Drawable * @param image * @param drawable * @param isActivated */ @SuppressWarnings("deprecation") public static void clipDrawable(final View image, Drawable drawable, boolean isActivated) { if (drawable == null) { return; } if (isActivated) { final ClipDrawable scaleDrawable = new ClipDrawable(drawable, Gravity.CENTER, ClipDrawable.HORIZONTAL | ClipDrawable.VERTICAL); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { image.setBackground(scaleDrawable); } else { image.setBackgroundDrawable(scaleDrawable); } image.setBackgroundDrawable(scaleDrawable); ValueAnimator animator = ValueAnimator.ofInt(0, 10000); animator.setDuration(200); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { scaleDrawable.setLevel((Integer) animation.getAnimatedValue()); } }); animator.start(); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { image.setBackground(null); } else { image.setBackgroundDrawable(null); } } } /** * 波紋動畫 * @param view * @param frontColor * @param behindColor * @param mode * @param isActivated */ @SuppressWarnings("deprecation") public static void rippleDrawable(final View view, int frontColor, int behindColor, int mode, boolean isActivated) { if (isActivated) { RippleDrawable rippleDrawable = new RippleDrawable(frontColor, behindColor, mode); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(rippleDrawable); } else { view.setBackgroundDrawable(rippleDrawable); } } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(null); } else { view.setBackgroundDrawable(null); } } } }
還有一個item的實體類
package com.example.map.tab; import android.graphics.drawable.Drawable; /** * Created by long on 2022/5/10. * Tab項 */ public class TabItem { private String title; private Drawable drawable; private int imageRes; public TabItem(String title, Drawable drawable, int imageRes) { this.title = title; this.drawable = drawable; this.imageRes = imageRes; } public TabItem(String title, int imageRes) { this.title = title; this.drawable = null; this.imageRes = imageRes; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Drawable getDrawable() { return drawable; } public void setDrawable(Drawable drawable) { this.drawable = drawable; } public int getImageRes() { return imageRes; } public void setImageRes(int imageRes) { this.imageRes = imageRes; } }
然後在我們的Activity中進行一個參照,首先在是我們Activity的一個佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <FrameLayout android:id="@+id/relate_tab_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/shadow" /> <com.example.map.tab.XFragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" /> </com.example.map.tab.XFragmentTabHost> </LinearLayout>
然後在我們的Activity中進行一個應用控制元件
package com.example.map.ui.activity; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.bumptech.glide.request.RequestOptions; import com.example.map.api.ApiService; import com.example.map.base.GlideImageLoader; import com.example.map.bean.WallPaperResponse; import com.example.map.ui.fragment.Fragment1; import com.example.map.ui.fragment.Fragment2; import com.example.map.ui.fragment.Fragment3; import com.example.map.ui.fragment.Fragment4; import com.example.map.ui.fragment.Fragment5; import com.example.map.R; import com.example.map.tab.TabFragment; import com.example.map.tab.TabItem; import com.example.map.tab.XFragmentTabHost; import com.llw.network.NetworkApi; import com.llw.network.observer.BaseObserver; import com.llw.network.utils.KLog; import com.youth.banner.Banner; import com.youth.banner.BannerConfig; import com.youth.banner.Transformer; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class QihooActivity extends AppCompatActivity { private XFragmentTabHost mTabHost; //底部導航的名稱 String[] mTabTitle = new String[]{"首頁", "遊戲", "軟體", "應用圈", "管理"}; //文字上的圖片 int[] mImageResId = new int[]{R.drawable.sel_360_home, R.drawable.sel_360_game, R.drawable.sel_360_software, R.drawable.sel_360_app, R.drawable.sel_360_mag}; //建立的幾個fragment Class[] mFragClass = new Class[]{Fragment1.class, Fragment2.class, Fragment3.class, Fragment4.class, Fragment5.class}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_qihoo); initTabHost(); } private void initTabHost() { mTabHost = (XFragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.relate_tab_content); mTabHost.setTabMode(XFragmentTabHost.TabMode.Ripple); mTabHost.setTextActiveColor(Color.WHITE); mTabHost.setTextInactiveColor(Color.GRAY); // mTabHost.setFrontColor(Color.RED); // mTabHost.setBehindColor(Color.GREEN); for (int i = 0; i < mFragClass.length; i++) { Bundle bundle = new Bundle(); bundle.putString(TabFragment.FRAG_KEY, mTabTitle[i]); mTabHost.addTabItem(new TabItem(mTabTitle[i], mImageResId[i]), mFragClass[i], bundle); } } }
這樣我們的一個自定義的水波紋動畫的底部導航就完成了
到此這篇關於Android自定義水波紋底部導航的實現的文章就介紹到這了,更多相關Android水波紋底部導航內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45