亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中Fab(FloatingActionButton)實(shí)現(xiàn)上下滑動(dòng)的漸變效果

 更新時(shí)間:2017年02月10日 14:05:11   投稿:daisy  
這篇文章主要給大家介紹了Android中FloatingActionButton(簡(jiǎn)稱(chēng)FAB)是如何實(shí)現(xiàn)上下滑動(dòng)的漸變效果,文中給出了詳細(xì)的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,有需要的朋友們可以一起看看吧。

前言

Promoted Actions是指一種操作按鈕,它不是放在actionbar中,而是直接在可見(jiàn)的UI布局中(當(dāng)然這里的UI指的是setContentView所管轄的范圍)。因此它更容易在代碼中被獲取到(試想如果你要在actionbar中獲取一個(gè)菜單按鈕是不是很難?),Promoted Actions往往主要用于一個(gè)界面的主要操作,比如在email的郵件列表界面,promoted action可以用于接受一個(gè)新郵件。promoted action在外觀(guān)上其實(shí)就是一個(gè)懸浮按鈕,更常見(jiàn)的是漂浮在界面上的圓形按鈕,一般我直接將promoted action稱(chēng)作懸浮按鈕,英文名稱(chēng)Float Action Button 簡(jiǎn)稱(chēng)(FAB,不是FBI哈)。

系統(tǒng)自帶的 Fab 也會(huì)隨著頁(yè)面上下滾動(dòng),但是淡出或者進(jìn)入的效果太不自然。

這里記錄一個(gè)小知識(shí)點(diǎn),Fab 隨著頁(yè)面的 RecyclerView 上下滾動(dòng)而漸變的動(dòng)畫(huà)效果。

包含 Fab 控件的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".view.activity.MainActivity">

 <android.support.design.widget.AppBarLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:theme="@style/AppTheme.AppBarOverlay">

 <android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  android:background="?attr/colorPrimary"
  app:layout_scrollFlags="scroll|enterAlways"
  app:popupTheme="@style/AppTheme.PopupOverlay" />


 <android.support.design.widget.TabLayout
  android:id="@+id/tab_layout"
  app:tabIndicatorColor="#FFFFFF"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
 </android.support.design.widget.TabLayout>
 </android.support.design.widget.AppBarLayout>

 <include layout="@layout/content_main" />

 <android.support.design.widget.FloatingActionButton
 android:id="@+id/fab"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_margin="@dimen/fab_margin"
 android:src="@android:drawable/ic_dialog_email"
 app:layout_behavior="com.wu.allen.zhuanlan.util.ScrollAwareFABBehavior"/>

</android.support.design.widget.CoordinatorLayout>

實(shí)現(xiàn)的 Java 代碼如下:

public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
 private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
 private boolean mIsAnimatingOut = false;

 public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
 super();
 }

 @Override
 public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
     final View directTargetChild, final View target, final int nestedScrollAxes) {
 // Ensure we react to vertical scrolling
 return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
  || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
 }

 @Override
 public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
    final View target, final int dxConsumed, final int dyConsumed,
    final int dxUnconsumed, final int dyUnconsumed) {
 super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
 if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {
  // User scrolled down and the FAB is currently visible -> hide the FAB
  animateOut(child);
 } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
  // User scrolled up and the FAB is currently not visible -> show the FAB
  animateIn(child);
 }
 }

 // Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits
 private void animateOut(final FloatingActionButton button) {
 if (Build.VERSION.SDK_INT >= 14) {
  ViewCompat.animate(button).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer()
   .setListener(new ViewPropertyAnimatorListener() {
   public void onAnimationStart(View view) {
    ScrollAwareFABBehavior.this.mIsAnimatingOut = true;
   }

   public void onAnimationCancel(View view) {
    ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
   }

   public void onAnimationEnd(View view) {
    ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
    view.setVisibility(View.GONE);
   }
   }).start();
 } else {
  Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_out);
  anim.setInterpolator(INTERPOLATOR);
  anim.setDuration(200L);
  anim.setAnimationListener(new Animation.AnimationListener() {
  public void onAnimationStart(Animation animation) {
   ScrollAwareFABBehavior.this.mIsAnimatingOut = true;
  }

  public void onAnimationEnd(Animation animation) {
   ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
   button.setVisibility(View.GONE);
  }

  @Override
  public void onAnimationRepeat(final Animation animation) {
  }
  });
  button.startAnimation(anim);
 }
 }

 // Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters
 private void animateIn(FloatingActionButton button) {
 button.setVisibility(View.VISIBLE);
 if (Build.VERSION.SDK_INT >= 14) {
  ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F)
   .setInterpolator(INTERPOLATOR).withLayer().setListener(null)
   .start();
 } else {
  Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_in);
  anim.setDuration(200L);
  anim.setInterpolator(INTERPOLATOR);
  button.startAnimation(anim);
 }
 }
}

fab_in.xml 文件如下(fab_out.xml 同理),當(dāng)然要改變淡出或者進(jìn)入的樣式,一般修改這里的 XML 文件就可以了 :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

 <alpha android:fromAlpha="0.0"
 android:toAlpha="1.0"/>

 <scale android:fromXScale="0.0"
 android:fromYScale="0.0"
 android:toXScale="1.0"
 android:toYScale="1.0"
 android:pivotX="50%"
 android:pivotY="50%"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

 <alpha android:fromAlpha="1.0"
 android:toAlpha="0.0"/>

 <scale android:fromXScale="1.0"
 android:fromYScale="1.0"
 android:toXScale="0.0"
 android:toYScale="0.0"
 android:pivotX="50%"
 android:pivotY="50%"/>

</set>

大致效果就像上面。

總結(jié)

好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開(kāi)發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。

相關(guān)文章

  • android?studio組件通信:Intend啟動(dòng)Activity接收返回結(jié)果

    android?studio組件通信:Intend啟動(dòng)Activity接收返回結(jié)果

    這篇文章主要介紹了android?studio組件通信:Intend啟動(dòng)Activity接收返回結(jié)果,設(shè)計(jì)一個(gè)主Activity和一個(gè)子Activity(Sub-Activity),使用主Activity上的按鈕啟動(dòng)子Activity,并將子Activity的一些信息返回給主Activity,并顯示在主Activity上,需要的朋友可以參考一下
    2021-12-12
  • Android短信驗(yàn)證服務(wù)分享

    Android短信驗(yàn)證服務(wù)分享

    這篇文章主要為大家詳細(xì)介紹了Android短信驗(yàn)證服務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Android學(xué)習(xí)之使用SharedPreferences存儲(chǔ)應(yīng)用程序數(shù)據(jù)

    Android學(xué)習(xí)之使用SharedPreferences存儲(chǔ)應(yīng)用程序數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)之使用SharedPreferences保存應(yīng)用程序數(shù)據(jù)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android開(kāi)發(fā)中R.java文件丟失或無(wú)法更新的解決方法

    Android開(kāi)發(fā)中R.java文件丟失或無(wú)法更新的解決方法

    這篇文章主要介紹了Android開(kāi)發(fā)中R.java文件丟失或無(wú)法更新的解決方法,較為詳細(xì)的列舉分析了出現(xiàn)R.java文件丟失或無(wú)法更新的常見(jiàn)原因及相應(yīng)的解決方法,需要的朋友可以參考下
    2016-02-02
  • 詳解Android截屏事件監(jiān)聽(tīng)

    詳解Android截屏事件監(jiān)聽(tīng)

    本篇文章主要介紹了Android截屏事件監(jiān)聽(tīng),Android系統(tǒng)沒(méi)有直接對(duì)截屏事件監(jiān)聽(tīng)的接口,本文介紹了2種方法,有興趣的可以了解一下。
    2016-12-12
  • 一篇文章揭開(kāi)Kotlin協(xié)程的神秘面紗

    一篇文章揭開(kāi)Kotlin協(xié)程的神秘面紗

    最近看了下Kotlin的協(xié)程,覺(jué)得挺好的,寫(xiě)篇文章總結(jié)總結(jié),所以下面這篇文章主要給大家介紹了關(guān)于Kotlin協(xié)程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • Android自定義圓角ImageView控件

    Android自定義圓角ImageView控件

    這篇文章主要為大家詳細(xì)介紹了Android自定義圓角ImageView的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • sqlite查詢(xún)結(jié)果在listview中展示的實(shí)現(xiàn)

    sqlite查詢(xún)結(jié)果在listview中展示的實(shí)現(xiàn)

    下面小編就為大家?guī)?lái)一篇sqlite查詢(xún)結(jié)果在listview中展示的實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • Android編程實(shí)現(xiàn)擦除Bitmap中某一塊的方法

    Android編程實(shí)現(xiàn)擦除Bitmap中某一塊的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)擦除Bitmap中某一塊的方法,涉及Android操作Bitmap顏色像素值調(diào)整的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • Flutter網(wǎng)絡(luò)請(qǐng)求Dio庫(kù)的使用及封裝詳解

    Flutter網(wǎng)絡(luò)請(qǐng)求Dio庫(kù)的使用及封裝詳解

    本文主要介紹了Flutter網(wǎng)絡(luò)請(qǐng)求Dio庫(kù)的使用及封裝詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04

最新評(píng)論