色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Android 滑動Scrollview標題欄漸變效果(仿京東toolbar)

瀏覽:133日期:2022-06-06 10:25:47

Scrollview標題欄滑動漸變

仿京東樣式(上滑顯示下滑漸變消失)

Android 滑動Scrollview標題欄漸變效果(仿京東toolbar)

Android 滑動Scrollview標題欄漸變效果(仿京東toolbar)

/** * @ClassName MyScrollView * @Author Rex * @Date 2021/1/27 17:38 */public class MyScrollView extends ScrollView { private TranslucentListener mTranslucentListener; public void setTranslucentListener(TranslucentListener translucentListener) { this.mTranslucentListener = translucentListener; } public MyScrollView(Context context) { this(context, null); } public MyScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (mTranslucentListener != null) { //ScrollView滑出高度 int scrollY = getScrollY(); //屏幕高度 int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels; //有效滑動距離為屏幕2分之一 // alpha = 滑動高度/(screenHeight/3f) if (scrollY <= screenHeight / 2f) { Log.d('>>>>>>>>>', 'ScrollView劃出高度:' + scrollY); Log.d('>>>>>>>>>', '屏幕高度:' + screenHeight); Log.d('>>>>>>>>>', '漸變值:' + (0 + scrollY / (screenHeight / 4f))); // 漸變的過程 1~0 mTranslucentListener.onTranslucent(0 + scrollY / (screenHeight /4f)); } } }}

Activity 設置

public class ToolbarActivity extends AppCompatActivity implements TranslucentListener { private Toolbar mToolBar; private MyScrollView mScrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_toobar); mToolBar = findViewById(R.id.id_toolbar); mScrollView = findViewById(R.id.id_scrollView); //初始化漸變為0 mToolBar.setAlpha(0); //設置漸變回調 mScrollView.setTranslucentListener(this); } @Override public void onTranslucent(float alpha) { mToolBar.setAlpha(alpha); }}

漸變回調接口

/** * @ClassName TranslucentListener * @Author rex * @Date 2021/1/27 17:38 */public interface TranslucentListener { /** * 透明度的回調監聽 * * @param alpha 0~1 透明度 */ public void onTranslucent(float alpha);}

布局文件

<RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' android:layout_width='match_parent' android:layout_height='match_parent'> <com.rex.rxhttpdemo.MyScrollView android: android:layout_width='match_parent' android:layout_height='match_parent' android:clipChildren='false' android:clipToPadding='false' > <LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'> <Button android: android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button0' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button1' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button2' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button3' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button4' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button5' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button5' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button5' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button5' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button5' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button5' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button5' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button5' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> <Button android:layout_width='match_parent' android:layout_height='match_parent' android:text='Button' /> </LinearLayout> </com.rex.rxhttpdemo.MyScrollView> <androidx.appcompat.widget.Toolbar android: android:layout_width='match_parent' android:layout_height='wrap_content' android:background='@color/colorAccent' app:title='title' /></RelativeLayout>

下滑顯示上滑漸變消失

/** * @ClassName MyScrollView * @Author Rex * @Date 2021/1/27 17:38 */public class MyScrollView extends ScrollView { private TranslucentListener mTranslucentListener; public void setTranslucentListener(TranslucentListener translucentListener) { this.mTranslucentListener = translucentListener; } public MyScrollView(Context context) { this(context, null); } public MyScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (mTranslucentListener != null) { //ScrollView滑出高度 int scrollY = getScrollY(); //屏幕高度 int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels; //有效滑動距離為屏幕2分之一 // alpha = 滑動高度/(screenHeight/3f) if (scrollY <= screenHeight / 2f) { Log.d('>>>>>>>>>', 'ScrollView劃出高度:' + scrollY); Log.d('>>>>>>>>>', '屏幕高度:' + screenHeight); Log.d('>>>>>>>>>', '漸變值:' + (1 - scrollY / (screenHeight / 4f))); // 漸變的過程 1~0 mTranslucentListener.onTranslucent(1 - scrollY / (screenHeight /4f)); } } }}

注意: 這里只是更改了 mTranslucentListener.onTranslucent 里的 漸變值

Activty 里 把初始化 mToolBar.setAlpha(0); 去掉

XML

<com.rex.rxhttpdemo.MyScrollView android: android:layout_width='match_parent' android:layout_height='match_parent' android:clipChildren='false' android:clipToPadding='false' android:paddingTop='?attr/actionBarSize' > </com.rex.rxhttpdemo.MyScrollView>

xml 加入 paddingtop .

注意:android:clipChildren=“false”android:clipToPadding='false'這倆個屬性 如果不加會有留白

到此這篇關于Android 滑動Scrollview標題欄漸變效果(仿京東toolbar)的文章就介紹到這了,更多相關Android 滑動Scrollview標題欄漸變內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: 京東
相關文章:
主站蜘蛛池模板: 爱爱亚洲 | 在线观看免费国产 | 亚洲综合成人网在线观看 | 91精品91久久久久久 | 亚洲成a v人片在线看片 | 成人做爰网站免费看 | 永久免费91桃色福利 | 国产自精品在线 | 国产三级精品三级国产 | 亚洲乱码一二三四五六区 | 欧美在线一区二区 | 午夜精品尤物福利视频在线 | 国产精品精品国产 | 黄色三级网站在线观看 | 中文字幕日韩精品亚洲七区 | 成人三级视频 | 国产成人福利视频在线观看 | 国产成人精品亚洲2020 | 日韩特黄特色大片免费视频 | 国产年成美女网站视频免费看 | a级毛片视频免费观看 | 国产精品视频免费观看调教网 | 国产在线精品成人一区二区三区 | 岛国搬运工最新网地址 | 精品国产欧美另类一区 | 欧美国产在线一区 | 欧美日韩在线视频免费完整 | 欧美成人午夜片一一在线观看 | 国产成人亚洲精品久久 | 扒开双腿猛进入爽爽在线观看 | 韩国特级毛片 | 91精品全国免费观看 | 不卡一区二区在线 | 深夜爽爽爽gif福利免费 | 色欧美与xxxxx | 亚洲国产精品第一区二区三区 | 久久国产欧美日韩高清专区 | 真正国产乱子伦高清对白 | 亚洲三级视频在线观看 | 在线中文字幕一区 | 国产精品久久久一区二区三区 |