android渐变Toolbar的实现

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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值