欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android仿京東頂部搜索框滑動伸縮動畫效果

 更新時間:2019年07月11日 08:32:25   作者:Hi以夢為馬  
這篇文章主要為大家詳細(xì)介紹了Android仿京東頂部搜索框滑動伸縮動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近使用京東發(fā)現(xiàn),京東頂部的搜索框有一個新的伸縮效果,根據(jù)用戶的手勢滑動,伸縮搜索框。覺得效果還不錯,就看了下其他的應(yīng)用有沒有這種伸縮的效果,發(fā)現(xiàn)安居客也使用了類似的一種效果,然后就想著實現(xiàn)這樣的一種動畫效果。
首先看下第三方的效果圖:

京東效果:

安居客效果:

我們最終實現(xiàn)的效果:

仿京東效果:

仿安居客效果:

看完效果圖,接下來,我們開始具體實現(xiàn)上面的效果:

布局文件的編寫

根據(jù)效果我們可以分析我的要做的功能布局效果,首先,整個布局存在一個頭部的滑動操作區(qū)域,包括標(biāo)題欄和搜索欄,然后整個布局還包含了一個滑動控件,滑動控件我們可以使用ScrollView或者NestedScrollView,過程中我們需要監(jiān)聽獲取上下滑動的距離,因此需要自定義我們的滑動控件,獲取滑動的距離:

自定義滑動控件:AnimationNestedScrollView

public class AnimationNestedScrollView extends NestedScrollView {
 private OnAnimationScrollChangeListener listener;

 public AnimationNestedScrollView(@NonNull Context context) {
 super(context);
 }

 public AnimationNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
 super(context, attrs);
 }

 public AnimationNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 }

 public void setOnAnimationScrollListener(OnAnimationScrollChangeListener listener) {
 this.listener = listener;
 }

 public interface OnAnimationScrollChangeListener {
 void onScrollChanged(float dy);
 }

 @Override
 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
 super.onScrollChanged(l, t, oldl, oldt);
 if (listener != null) {
 listener.onScrollChanged(getScrollY() * 0.65f);//x0.65 使位移效果更加平滑 解決手指按住停留時抖動問題
 }
 }
}

這里我使用了NestedScrollView 來實現(xiàn)自定義控件,使用ScrollView也是一樣的效果, 中間主要設(shè)置了滑動的監(jiān)聽方法,獲取滑動的距離。

實現(xiàn)了自定義控件后,我們開始編寫布局文件:

布局文件:activity_search

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.cjxj.androiddemo.activity.SearchActivity">

 <RelativeLayout
 android:id="@+id/search_rl_top"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#3F51B5">

 <RelativeLayout
 android:id="@+id/search_layout"
 android:layout_width="match_parent"
 android:layout_height="44dp">

 <ImageView
 android:id="@+id/search_iv_back"
 android:layout_width="10dp"
 android:layout_height="20dp"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true"
 android:layout_marginLeft="20dp"
 android:src="@drawable/video_back" />

 <TextView
 android:id="@+id/search_tv_title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="11.5dp"
 android:gravity="center"
 android:text="這是標(biāo)題"
 android:textColor="#fff"
 android:textSize="17dp"
 android:textStyle="bold" />

 </RelativeLayout>

 <LinearLayout
 android:id="@+id/search_ll_search"
 android:layout_width="match_parent"
 android:layout_height="35dp"
 android:layout_centerHorizontal="true"
 android:layout_marginLeft="15dp"
 android:layout_marginTop="49dp"
 android:layout_marginRight="15dp"
 android:layout_marginBottom="10dp"
 android:background="@drawable/activity_search_tv_shape"
 android:gravity="center_vertical"
 android:orientation="horizontal"
 android:visibility="visible">

 <ImageView
 android:layout_width="16dp"
 android:layout_height="16dp"
 android:layout_marginLeft="10dp"
 android:src="@drawable/ic_search" />

 <TextView
 android:id="@+id/search_tv_search"
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_marginLeft="5dp"
 android:layout_marginRight="10dp"
 android:layout_weight="1"
 android:cursorVisible="false"
 android:gravity="center_vertical|center_horizontal"
 android:hint="這是搜索框"
 android:textSize="15dp" />

 <ImageView
 android:layout_width="16dp"
 android:layout_height="16dp"
 android:layout_marginRight="10dp"
 android:src="@drawable/ic_search" />

 </LinearLayout>

 </RelativeLayout>

 <com.cjxj.androiddemo.view.AnimationNestedScrollView
 android:id="@+id/search_sv_view"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_below="@id/search_rl_top">

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:descendantFocusability="blocksDescendants"
 android:orientation="vertical">

 <TextView
 android:layout_width="match_parent"
 android:layout_height="900dp" />

 </LinearLayout>

 </com.cjxj.androiddemo.view.AnimationNestedScrollView>


</RelativeLayout>

這里的布局文件是實現(xiàn)安居客的效果的代碼,如果要實現(xiàn)京東的效果,布局文件只需要設(shè)置search_ll_search的屬性即可:

刪除代碼:

android:layout_centerHorizontal="true"

添加代碼:

android:layout_alignParentLeft="true"

然后再修改邏輯代碼參數(shù)即可,后面會介紹。

邏輯的處理

邏輯部分,主要是根據(jù)滑動距離,動態(tài)的修改搜索欄的寬度和頂部距離,同時設(shè)置邊界即可。

public class SearchActivity extends AppCompatActivity {
 private AnimationNestedScrollView sv_view;
 private LinearLayout ll_search;
 private TextView tv_title;
 private float LL_SEARCH_MIN_TOP_MARGIN, LL_SEARCH_MAX_TOP_MARGIN, LL_SEARCH_MAX_WIDTH, LL_SEARCH_MIN_WIDTH, TV_TITLE_MAX_TOP_MARGIN;
 private ViewGroup.MarginLayoutParams searchLayoutParams, titleLayoutParams;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_search);

 initView();
 }

 private void initView() {
 sv_view = findViewById(R.id.search_sv_view);
 ll_search = findViewById(R.id.search_ll_search);
 tv_title = findViewById(R.id.search_tv_title);
 searchLayoutParams = (ViewGroup.MarginLayoutParams) ll_search.getLayoutParams();
 titleLayoutParams = (ViewGroup.MarginLayoutParams) tv_title.getLayoutParams();

 LL_SEARCH_MIN_TOP_MARGIN = CommonUtil.dp2px(this, 4.5f);//布局關(guān)閉時頂部距離
 LL_SEARCH_MAX_TOP_MARGIN = CommonUtil.dp2px(this, 49f);//布局默認(rèn)展開時頂部距離
 LL_SEARCH_MAX_WIDTH = CommonUtil.getScreenWidth(this) - CommonUtil.dp2px(this, 30f);//布局默認(rèn)展開時的寬度
 LL_SEARCH_MIN_WIDTH = CommonUtil.getScreenWidth(this) - CommonUtil.dp2px(this, 82f);//布局關(guān)閉時的寬度
 TV_TITLE_MAX_TOP_MARGIN = CommonUtil.dp2px(this, 11.5f);

 sv_view.setOnAnimationScrollListener(new AnimationNestedScrollView.OnAnimationScrollChangeListener() {
 @Override
 public void onScrollChanged(float dy) {
 float searchLayoutNewTopMargin = LL_SEARCH_MAX_TOP_MARGIN - dy;
 float searchLayoutNewWidth = LL_SEARCH_MAX_WIDTH - dy * 1.3f;//此處 * 1.3f 可以設(shè)置搜索框?qū)挾瓤s放的速率

 float titleNewTopMargin = (float) (TV_TITLE_MAX_TOP_MARGIN - dy * 0.5);

 //處理布局的邊界問題
 searchLayoutNewWidth = searchLayoutNewWidth < LL_SEARCH_MIN_WIDTH ? LL_SEARCH_MIN_WIDTH : searchLayoutNewWidth;

 if (searchLayoutNewTopMargin < LL_SEARCH_MIN_TOP_MARGIN) {
  searchLayoutNewTopMargin = LL_SEARCH_MIN_TOP_MARGIN;
 }

 if (searchLayoutNewWidth < LL_SEARCH_MIN_WIDTH) {
  searchLayoutNewWidth = LL_SEARCH_MIN_WIDTH;
 }

 float titleAlpha = 255 * titleNewTopMargin / TV_TITLE_MAX_TOP_MARGIN;
 if (titleAlpha < 0) {
  titleAlpha = 0;
 }

 //設(shè)置相關(guān)控件的LayoutParams 此處使用的是MarginLayoutParams,便于設(shè)置params的topMargin屬性
 tv_title.setTextColor(tv_title.getTextColors().withAlpha((int) titleAlpha));
 titleLayoutParams.topMargin = (int) titleNewTopMargin;
 tv_title.setLayoutParams(titleLayoutParams);

 searchLayoutParams.topMargin = (int) searchLayoutNewTopMargin;
 searchLayoutParams.width = (int) searchLayoutNewWidth;
 ll_search.setLayoutParams(searchLayoutParams);
 }
 });
 }
}

具體的代碼都有相應(yīng)的注釋,需要解釋的是,這里同樣是實現(xiàn)安居客的效果的代碼,如果要實現(xiàn)京東效果,這里需要做相關(guān)修改:

1.修改搜索欄的最小寬度:

LL_SEARCH_MIN_WIDTH = CommonUtil.getScreenWidth(this) - CommonUtil.dp2px(this, 122f);//布局關(guān)閉時的寬度

2.設(shè)置搜索框?qū)挾瓤s放的速率

float searchLayoutNewWidth = LL_SEARCH_MAX_WIDTH - dy * 3.0f;//此處 * 1.3f 可以設(shè)置搜索框?qū)挾瓤s放的速率

通過這兩步修改,結(jié)合上文說的布局文件的修改,即可實現(xiàn)京東的效果。

注:

1.文件中我們使用的LayoutParams是MarginLayoutParams,主要是便于我們設(shè)置相關(guān)控件的topMargin屬性.
2.文件中CommonUtil是方法公共類,主要是用于獲取屏幕的寬度,以及dp和px的轉(zhuǎn)換,相關(guān)方法如下:

public static int dp2px(Context context, float dpValue) {
 if (null == context) {
 return 0;
 }
 final float scale = context.getResources().getDisplayMetrics().density;
 return (int) (dpValue * scale + 0.5f);
 }

//...

public static int getScreenWidth(Context context) {
 if (null == context) {
 return 0;
 }
 return context.getResources().getDisplayMetrics().widthPixels;
 }

至此,我們想要的效果就基本實現(xiàn)了,如有問題歡迎留言指正。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android ApiDemo示例工程的創(chuàng)建

    Android ApiDemo示例工程的創(chuàng)建

    本文主要介紹Android ApiDemo示例工程的創(chuàng)建,這里SDK中的示例工程做了大致介紹,并說明如何創(chuàng)建ApiDemo 示例工程,有需要看自帶代碼的朋友可以參考下
    2016-09-09
  • Android?Gradle模塊依賴替換使用技巧

    Android?Gradle模塊依賴替換使用技巧

    這篇文章主要為大家介紹了Android?Gradle模塊依賴替換使用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Android實現(xiàn)根據(jù)評分添加星級條

    Android實現(xiàn)根據(jù)評分添加星級條

    這篇文章主要介紹了Android實現(xiàn)根據(jù)評分添加星級條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Android 獲取判斷是否有懸浮窗權(quán)限的方法

    Android 獲取判斷是否有懸浮窗權(quán)限的方法

    今天小編就為大家分享一篇Android 獲取判斷是否有懸浮窗權(quán)限的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • android bitmap compress(圖片壓縮)代碼

    android bitmap compress(圖片壓縮)代碼

    android bitmap compress(圖片壓縮)代碼,需要的朋友可以參考一下
    2013-06-06
  • 淺析Android加載字體包及封裝的方法

    淺析Android加載字體包及封裝的方法

    這篇文章主要介紹了Android加載字體包及封裝的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Android 中Lambda表達式的使用實例詳解

    Android 中Lambda表達式的使用實例詳解

    這篇文章主要介紹了 Android 中Lambda表達式的使用實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Android Studio實現(xiàn)簡單的通訊錄

    Android Studio實現(xiàn)簡單的通訊錄

    這篇文章主要為大家詳細(xì)介紹了Android Studio實現(xiàn)簡單的通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • MobPush?Android常見問題解決分析

    MobPush?Android常見問題解決分析

    這篇文章主要介紹了MobPush?Android常見問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Android Activity的啟動過程源碼解析

    Android Activity的啟動過程源碼解析

    這篇文章主要介紹了Android Activity的啟動過程源碼解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05

最新評論