Android 滑动Toolbar透明度变化

一、这是效果

二、实现过程

       自定义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的区域



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值