日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android view显示隐藏动画效果,Android 根据手势顶部View自动展示与隐藏效果

發布時間:2025/3/11 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android view显示隐藏动画效果,Android 根据手势顶部View自动展示与隐藏效果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先來看一下效果:

大體思路如下:

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

我在bottomView里放了ViewPager,里面又有Fragment,Fragment里放的是ListView

原理:

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

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

布局文件

android:id="@+id/view_group"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/group_top"

layout="@layout/view_top" />

android:id="@+id/group_bottom"

layout="@layout/view_bottom" />

手勢監聽重要的是打log看一下上下滑動是數值的變化,找到其規律:

@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時需要手動將子控件布局

接下來就是監聽手勢設置動畫,不停的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();

}

項目地址在這:

總結

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

總結

以上是生活随笔為你收集整理的android view显示隐藏动画效果,Android 根据手势顶部View自动展示与隐藏效果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。