一、这是效果
二、实现过程
自定义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的区域