android渐变Toolbar的实现

来源:互联网 发布:圣安德鲁斯大学cs知乎 编辑:程序博客网 时间:2024/05/16 02:31

toolbar渐变效果很常见,实现方法也多,先看效果图
这里写图片描述

采用最简单的实现方式。直接看code

第一部分:布局文件

<?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"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:context="com.example.minwenping.toolbarjianbiandemo.MainActivity">    <com.example.minwenping.toolbarjianbiandemo.XScrollView        android:id="@+id/scrollView"        android:clipToPadding="false"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:paddingTop="?android:actionBarSize">        <android.support.v7.widget.LinearLayoutCompat            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical">            <Button                android:layout_width="match_parent"                android:layout_height="100dp"                android:layout_marginTop="4dp"                android:text="按钮button1" />            <Button                android:layout_width="match_parent"                android:layout_height="100dp"                android:layout_marginTop="4dp"                android:text="按钮button2" />            <Button                android:layout_width="match_parent"                android:layout_height="100dp"                android:layout_marginTop="4dp"                android:text="按钮button3" />            <Button                android:layout_width="match_parent"                android:layout_height="100dp"                android:layout_marginTop="4dp"                android:text="按钮button4" />            <Button                android:layout_width="match_parent"                android:layout_height="100dp"                android:layout_marginTop="4dp"                android:text="按钮button5" />            <Button                android:layout_width="match_parent"                android:layout_height="100dp"                android:layout_marginTop="4dp"                android:text="按钮button6" />            <Button                android:layout_width="match_parent"                android:layout_height="100dp"                android:layout_marginTop="4dp"                android:text="按钮button7" />            <Button                android:layout_width="match_parent"                android:layout_height="100dp"                android:layout_marginTop="4dp"                android:text="按钮button1" />            <Button                android:layout_width="match_parent"                android:layout_height="100dp"                android:layout_marginTop="4dp"                android:text="按钮button1" />        </android.support.v7.widget.LinearLayoutCompat>    </com.example.minwenping.toolbarjianbiandemo.XScrollView>    <android.support.v7.widget.Toolbar        android:id="@+id/toolbar"        android:background="@color/colorAccent"        android:titleTextColor="@color/colorPrimary"        android:layout_width="match_parent"        android:layout_height="?android:attr/actionBarSize"        app:title="Hello World!粉红色的toolbar" /></RelativeLayout>

第二部分是自定义scrollview的code,因为scrollview的滑动监听不兼容低版本,所以继承重写

public class XScrollView extends ScrollView {    int height;    public void setAlphaChangeListener(AlphaChangeListener alphaChangeListener) {        this.alphaChangeListener = alphaChangeListener;    }    AlphaChangeListener alphaChangeListener;    public XScrollView(Context context) {        this(context,null);    }    public XScrollView(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public XScrollView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        height=getResources().getDisplayMetrics().heightPixels;    }    @Override    protected void onScrollChanged(int l, int t, int oldl, int oldt) {        super.onScrollChanged(l, t, oldl, oldt);        if (alphaChangeListener!=null) {            if (t<=height/4){                float a=1-t*4.0f/height;                alphaChangeListener.alphaChanging(a);            }        }    }}

第三部分code,外部使用的接口回调

public interface AlphaChangeListener {    abstract void alphaChanging(float alpha);}

第四部分code,activity中的使用

 protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        toolBar = ((Toolbar) findViewById(R.id.toolbar));        scrollview = ((XScrollView) findViewById(R.id.scrollView));        setSupportActionBar(toolBar);        scrollview.setSmoothScrollingEnabled(true);        scrollview.setAlphaChangeListener(this);    }    @Override    public void alphaChanging(float alpha) {        toolBar.setAlpha(alpha);    }