最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 根據(jù)手勢頂部View自動展示與隱藏效果

 更新時間:2017年08月21日 10:20:52   作者:不甘猿人  
這篇文章主要介紹了Android 根據(jù)手勢頂部View自動展示與隱藏效果,本文給大家介紹非常詳細包括實現(xiàn)原理和實例代碼,需要的朋友參考下吧

首先來看一下效果:

這里寫圖片描述 

 大體思路如下:

總體布局用了一個自定義的ViewGroup,里面包了兩個View(top View,bottomView)

我在bottomView里放了ViewPager,里面又有Fragment,F(xiàn)ragment里放的是ListView

原理:

ViewGroup在分發(fā)touchEvent的時候先通過手勢GestureDetector判斷手勢方向,當向上滑動的時候讓topView和bottomView同時向上移動,反之亦然。

整體思路不是很難如下是干貨:

布局文件

<com.lin.gesturedetector.MyViewGroup
  android:id="@+id/view_group"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <include
   android:id="@+id/group_top"
   layout="@layout/view_top" />
  <include
   android:id="@+id/group_bottom"
   layout="@layout/view_bottom" />
 </com.lin.gesturedetector.MyViewGroup>

手勢監(jiān)聽重要的是打log看一下上下滑動是數(shù)值的變化,找到其規(guī)律:           

 @Override
   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    Log.i(tag, "onScroll -> distanceY" + distanceY);
    if (distanceY < 0) {// 手勢向下滑動是負值
     animatorLayoutOffset(1);
    }
    if (distanceY > 0) {
     animatorLayoutOffset(0f);
    }
    return true;
   }

一定記得在ViewGroup內查找控件需要在onFinishInflate后才能找到:   

 @Override
 protected void onFinishInflate() {
  super.onFinishInflate();
  viewTop = findViewById(R.id.group_top);
  viewBottom = findViewById(R.id.group_bottom);
 }

在ViewGroup布局的邏輯中需要處理的有一下幾點:

1、onMeasure的時候要把子控件測量出來

2、onLayout時需要手動將子控件布局

接下來就是監(jiān)聽手勢設置動畫,不停的onLayout以達到topView和bottomView的布局效果  

@Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int width = MeasureSpec.getSize(widthMeasureSpec);
  int height = MeasureSpec.getSize(heightMeasureSpec);
  viewTop.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
  viewBottom.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
  setMeasuredDimension(width, height);
 }
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int topHeight = viewTop.getMeasuredHeight();
  float offset = layoutOffset * topHeight;
  int width = r - l;
  float topViewYTop = offset - topHeight;
  float topViewYBottom = topViewYTop + topHeight;
  viewTop.layout(0, (int) topViewYTop, width, (int) topViewYBottom);
  viewBottom.layout(0, (int) topViewYBottom, width, (int) topViewYBottom + viewBottom.getMeasuredHeight());
 }
 private void animatorLayoutOffset(float offset) {
  if (animator != null && animator.isRunning()) {
   return;
  }
  animator = ObjectAnimator.ofFloat(this, "layoutOffset", layoutOffset, offset);
  animator.setDuration(500);
  animator.start();
 }

項目地址在這:

GitHub

總結

以上所述是小編給大家介紹的Android 根據(jù)手勢頂部View自動展示與隱藏效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • Android自定義View之漸變色折線圖的實現(xiàn)

    Android自定義View之漸變色折線圖的實現(xiàn)

    折線圖的實現(xiàn)方法在github上有很多開源的程序,但是對于初學者來講,簡單一點的教程可能更容易入門,下面這篇文章主要給大家介紹了關于Android自定義View之漸變色折線圖的相關資料,需要的朋友可以參考下
    2022-04-04
  • android短信管理器SmsManager實例詳解

    android短信管理器SmsManager實例詳解

    這篇文章主要為大家詳細介紹了android短信管理器SmsManager實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android使用代碼動態(tài)生成界面

    Android使用代碼動態(tài)生成界面

    這篇文章主要為大家詳細介紹了Android使用代碼動態(tài)生成界面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 基于Android代碼實現(xiàn)常用布局

    基于Android代碼實現(xiàn)常用布局

    大家在日常中經(jīng)常見到用xml文件實現(xiàn)android常用布局,但是大家知道如何用代碼實現(xiàn)呢?使用代碼實現(xiàn)可以幫助我們學習sdk api,所以小編把我日常整理些關于android常用布局代碼實現(xiàn)分享給大家
    2015-11-11
  • Android開發(fā)之媒體播放工具類完整示例

    Android開發(fā)之媒體播放工具類完整示例

    這篇文章主要介紹了Android開發(fā)之媒體播放工具類,結合完整實例形式分析了基于MediaPlayer的事件監(jiān)聽與多媒體文件播放相關操作技巧,需要的朋友可以參考下
    2018-02-02
  • com.android.support版本沖突解決方法

    com.android.support版本沖突解決方法

    在本篇文章里小編給大家整理的是關于com.android.support版本沖突解決方法以及相關知識點,需要的朋友們學習下。
    2019-09-09
  • 基于Android實現(xiàn)仿QQ5.0側滑

    基于Android實現(xiàn)仿QQ5.0側滑

    本課程將帶領大家通過自定義控件實現(xiàn)QQ5.0側滑菜單,課程將循序漸進,首先實現(xiàn)最普通的側滑菜單,然后引入屬性動畫與拖動菜單效果相結合,最終實現(xiàn)QQ5.0側滑菜單效果。通過本課程大家會對側滑菜單有更深層次的了解,通過自定義控件和屬性動畫打造千變萬化的側滑菜單效果
    2015-12-12
  • Android使用CoordinatorLayout實現(xiàn)底部彈出菜單

    Android使用CoordinatorLayout實現(xiàn)底部彈出菜單

    這篇文章主要為大家詳細介紹了Android使用CoordinatorLayout實現(xiàn)底部彈出菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android開發(fā)之資源目錄assets與res/raw的區(qū)別分析

    Android開發(fā)之資源目錄assets與res/raw的區(qū)別分析

    這篇文章主要介紹了Android開發(fā)之資源目錄assets與res/raw的區(qū)別,結合實例形式分析了Android開發(fā)中資源目錄assets與res/raw的具體功能、使用方法與區(qū)別,需要的朋友可以參考下
    2016-01-01
  • 解決android.support.v4.content.FileProvide找不到的問題

    解決android.support.v4.content.FileProvide找不到的問題

    這篇文章主要介紹了解決android.support.v4.content.FileProvide找不到的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03

最新評論

临江市| 招远市| 江华| 贺兰县| 饶阳县| 开平市| 崇仁县| 桦南县| 防城港市| 武安市| 台东市| 文山县| 都昌县| 天祝| 西盟| 深州市| 加查县| 阆中市| 灯塔市| 浙江省| 锡林郭勒盟| 烟台市| 西乌珠穆沁旗| 威海市| 永泰县| 柞水县| 萝北县| 鄢陵县| 西盟| 九江市| 沙湾县| 渭南市| 木兰县| 马关县| 清流县| 镇远县| 巨鹿县| 天长市| 辽阳县| 胶州市| 襄樊市|