Android 滑动Toolbar透明度变化
来源:互联网 发布:nodejs 多个js 编辑:程序博客网 时间:2024/05/18 00:49
一、这是效果
二、实现过程
自定义ScrollView获取滑动量,然后转换为0--1的透明度,并改变Toolbar的setAlpha
三、实现代码
MyScrollView.java
package com.xiaofan.test.view;import android.content.Context;import android.util.AttributeSet;import android.widget.ScrollView;public class MyScrollView extends ScrollView { private TranslucentListener listener; public void setListener(TranslucentListener listener) { this.listener = listener; } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (listener != null) { int scrollY = getScrollY(); int screen_height = getContext().getResources().getDisplayMetrics().heightPixels; if (scrollY <= screen_height / 3f) { //0~1f,透明度应该是1~0f listener.onTranlucent(1 - scrollY / (screen_height / 3f));//划出去的高度/屏幕 高度/3f } } }}activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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" android:orientation="vertical"> <com.xiaofan.test.view.MyScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" android:clipChildren="false" android:paddingTop="?attr/actionBarSize"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="150dp" android:text="哈哈哈1" /> <Button android:layout_width="match_parent" android:layout_height="150dp" android:text="哈哈哈2" /> <Button android:layout_width="match_parent" android:layout_height="150dp" android:text="哈哈哈3" /> <Button android:layout_width="match_parent" android:layout_height="150dp" android:text="哈哈哈4" /> <Button android:layout_width="match_parent" android:layout_height="150dp" android:text="哈哈哈5" /> <Button android:layout_width="match_parent" android:layout_height="150dp" android:text="哈哈哈6" /> </LinearLayout> </com.xiaofan.test.view.MyScrollView> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:title="哈哈哈哈哈哈哈" /></RelativeLayout>
MainActivity
package com.xiaofan.test;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.util.Log;import com.xiaofan.test.view.MyScrollView;import com.xiaofan.test.view.TranslucentListener;public class MainActivity extends AppCompatActivity implements TranslucentListener { private Toolbar toolbar; private MyScrollView scrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); scrollView = (MyScrollView) findViewById(R.id.scrollView); scrollView.setListener(this); setSupportActionBar(toolbar); } @Override public void onTranlucent(float alpha) { toolbar.setAlpha(alpha); }}
TranslucentListener
public interface TranslucentListener {
void onTranlucent(float alpha);
}
注意:想实现内容不考虑padding需添加
android:clipToPadding="false" 该绘制的时候考不考虑padding false考虑padding
android:clipChildren="false 子控件是否不超出padding的区域
阅读全文
0 0
- Android 滑动Toolbar透明度变化
- 滑动改变toolbar透明度
- Android开发笔记之Toolbar 透明状态栏及透明度变化
- Android实现QQ空间沉浸式状态栏,并且滑动ListView实现透明度变化
- Toolbar随着ScrollView滑动透明度渐变效果实现
- Toolbar随着ScrollView滑动透明度渐变效果实现
- Android 笔记之 设置透明度 ActionBar ----ToolBar
- Android WebView滑动隐藏Toolbar
- ScrollView的回弹效果和滚动时toolBar的透明度变化
- android 设置toolbar透明度所有的页面都会变透明
- android滑动toolbar 很炫的标题栏
- Android 监听手指滑动,Toolbar颜色渐变
- 得到ListView的滑动距离来设置上方菜单的透明度变化
- 移动端页面上下滑动,大小、透明度的变化视觉差异效果切换页面
- android更改布局的透明度时其他布局的透明度也跟着变化
- JS透明度变化动画
- 实现头部透明度变化
- Android中的TollBar随着listview滑动透明度渐变
- sublime text 3 安装ctags
- Android——Android基础控件
- windows上IPC配置不当利用以及防护
- vim
- android 软键盘的显示与隐藏
- Android 滑动Toolbar透明度变化
- html相关
- spring boot
- 使用ccache加速xcode编译过程
- 暑假集训test10
- POJ-3190 Stall Reservations(贪心)
- 《慕课网玩转算法面试》笔记及习题解答5.1.~5.3
- [A-frame学习文档笔记]JavaScriptEvents DOM APIs的简单使用
- flask-login 源码解析