Android 沉浸式状态栏 头部可拉伸带有一键置顶功能

一:实现沉浸式状态栏,头部可以拉伸,头部图片可下拉放大,带有一键置顶功能 

先看看三个步骤:

1:在AndroidManifest.xml中设置Activity的属性为android:theme="@android:style/Theme.NoTitleBar"

2:在Layout的第一个布局中设置与头部的间隔(android:layout_marginTop="25dp",当然具体间隔多少需要根据自己的需求来)

3:在setContentView(R.layout.activity_userguide)之前super.onCreate(savedInstanceState)之后设置
 

if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

 

 

 

 

1.主函数代码:

import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private NotifyingScrollView scrollview;
    private RelativeLayout topbar;
    private ImageView topimage;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView textdemo;
    private Button button;
    private ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        }


        InfoData();
    }

    public void InfoData(){

        topbar = (RelativeLayout) findViewById(R.id.topbar);
        topimage = (ImageView) findViewById(R.id.topimage);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        textdemo = (TextView) findViewById(R.id.textdemo);
        button = (Button) findViewById(R.id.button);
        listview = (ListView) findViewById(R.id.listview);
        listview.setAdapter(new MyAdapter(this));
        scrollview = (NotifyingScrollView) findViewById(R.id.scrollview);
        //始终位于最顶部
        scrollview.smoothScrollTo(0,0);

        //初始化标题栏相关的色值,为透明
        topbar.getBackground().setAlpha(0);
        tv_title.setTextColor(Color.argb(0, 255, 255, 255));
        textdemo.setBackgroundColor(Color.argb(0, 255, 255, 255));

        scrollview.setOnScrollChangedListener(new NotifyingScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl,
                                        int oldt) {
                //滑动改变标题栏的透明度和文字透明度,图标
                if (topimage == null) {
                    return;
                }
                if(t < 0){
                    return;
                }

                //置顶操作
                if (t > scrollview.getHeight()/2){

                    button.setVisibility(View.VISIBLE);
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            scrollview.smoothScrollTo(0,0);
                        }
                    });

                }else {

                    button.setVisibility(View.GONE);

                }

                //获取滑动高度,做出对顶部导航栏的操作

                int lHeight = 2*(topbar.getHeight());

                if (t <= lHeight) {

                    int progress = (int) (new Float(t) / new Float(lHeight) * 255);
                    topbar.getBackground().setAlpha(progress);
                    tv_title.setTextColor(Color.argb(progress, 255, 255, 255));
                    textdemo.setBackgroundColor(Color.argb(progress, 240, 240, 240));
                    iv_back.setImageResource(R.mipmap.icon_back_normal);

                } else {

                    topbar.getBackground().setAlpha(255);
                    tv_title.setTextColor(Color.argb(255, 255, 255, 255));
                    textdemo.setBackgroundColor(Color.argb(255, 232, 232, 232));
                    iv_back.setImageResource(R.mipmap.icon_back_pressed);


                }

            }
        });

    }

}

2.主函数布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <com.example.administrator.scrolltable.NotifyingScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/topimage"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:src="@mipmap/aa10227a" />

            <ListView
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:layout_below="@+id/topimage"
                android:id="@+id/listview">

            </ListView>

        </RelativeLayout>
    </com.example.administrator.scrolltable.NotifyingScrollView>

    <RelativeLayout
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="#66cc99" >

        <ImageView
            android:id="@+id/iv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:src="@mipmap/icon_back_normal" />

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="我的"
            android:textColor="#fff000" />

        <TextView
            android:id="@+id/textdemo"
            android:layout_width="match_parent"
            android:layout_height="0.1dp"
            android:layout_alignParentBottom="true"
            android:textColor="#f0f0f0"
            />
    </RelativeLayout>


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="回到顶部"
            android:id="@+id/button"
            android:visibility="gone"
            />
    </RelativeLayout>

</RelativeLayout>

3.自定义Scrollview:

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

/**
 * 作者: yzp on 2016-10-09.
 * 邮箱: 15111424807@163.com
 * QQ: 486492302
 *
 * 重写scrollview,以实现滑动效果
 */
public class NotifyingScrollView extends ScrollView implements View.OnTouchListener {

    // 记录首次按下位置
    private float mFirstPosition = 0;
    // 是否正在放大
    private Boolean mScaling = false;
    private View dropZoomView;
    private int dropZoomViewWidth;
    private int dropZoomViewHeight;

    private boolean mDisableEdgeEffects = true;

    public interface OnScrollChangedListener {
        void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);
    }

    private OnScrollChangedListener mOnScrollChangedListener;

    public NotifyingScrollView(Context context) {
        super(context);
    }

    public NotifyingScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NotifyingScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }

    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mOnScrollChangedListener = listener;
    }

    @Override
    protected float getTopFadingEdgeStrength() {
        if (mDisableEdgeEffects && Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return 0.0f;
        }
        return super.getTopFadingEdgeStrength();
    }

    @Override
    protected float getBottomFadingEdgeStrength() {
        if (mDisableEdgeEffects && Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return 0.0f;
        }
        return super.getBottomFadingEdgeStrength();
    }

    //下拉放大效果处理
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }
    private void init() {
        setOverScrollMode(OVER_SCROLL_NEVER);
        if (getChildAt(0) != null) {
            ViewGroup vg = (ViewGroup) getChildAt(0);
            if (vg.getChildAt(0) != null) {
                dropZoomView = vg.getChildAt(0);
                setOnTouchListener(this);

            }
        }
    }
    /**
     * 下拉头部放大效果
     * @param v
     * @param event
     * @return
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (dropZoomViewWidth <= 0 || dropZoomViewHeight <= 0) {
            dropZoomViewWidth = dropZoomView.getMeasuredWidth();
            dropZoomViewHeight = dropZoomView.getMeasuredHeight();
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                //手指离开后恢复图片
                mScaling = false;
                replyImage();
                break;
            case MotionEvent.ACTION_MOVE:
                if (!mScaling) {
                    if (getScrollY() == 0) {
                        mFirstPosition = event.getY();// 滚动到顶部时记录位置,否则正常返回
                    } else {
                        break;
                    }
                }
                int distance = (int) ((event.getY() - mFirstPosition) * 0.6); // 滚动距离乘以一个系数
                if (distance < 0) { // 当前位置比记录位置要小,正常返回
                    break;
                }

                // 处理放大
                mScaling = true;
                setZoom(1 + distance);
                return true; // 返回true表示已经完成触摸事件,不再处理
        }
        return false;
    }

    // 回弹动画 (使用了属性动画)
    public void replyImage() {
        final float distance = dropZoomView.getMeasuredWidth() - dropZoomViewWidth;

        // 设置动画
        ValueAnimator anim = ObjectAnimator.ofFloat(0.0F, 1.0F).setDuration((long) (distance * 0.7));

        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float cVal = (Float) animation.getAnimatedValue();
                setZoom(distance - ((distance) * cVal));
            }
        });
        anim.start();

    }

    //缩放
    public void setZoom(float s) {
        if (dropZoomViewHeight <= 0 || dropZoomViewWidth <= 0) {
            return;
        }
        ViewGroup.LayoutParams lp = dropZoomView.getLayoutParams();
        //此处不处理头部某一边变宽
//        lp.width = (int) (dropZoomViewWidth + s);
        lp.height = (int) (dropZoomViewHeight * ((dropZoomViewWidth + s) / dropZoomViewWidth));
        dropZoomView.setLayoutParams(lp);
    }

}

4.适配器:

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MyAdapter extends BaseAdapter {

    Context context;

    public MyAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context=context;
    }

    @Override
    public int getCount() {
        return 15;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View  vi = convertView;
        vi = vi.inflate(context, R.layout.listview_text, null);
        return vi;
    }
}

5.适配器布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:id="@+id/text_up"
        android:textSize="18sp"
        android:text="上滑啦"/>

</LinearLayout>

二:Android沉浸式状态栏 + actionBar渐变 + scrollView顶部伸缩(拉伸时上下伸缩,左右不变)

(上图是参考图片,来自yanjunhui) 

注意:沉浸式状态栏(API-Level 19, Android4.4 KitKat 之后加入的东西),而且在Api-Level 21版本中新增了一个属性(下面会说到)。所以,style文件应该声明三份:

values:

<style name="TranslucentTheme" parent="@style/AppTheme"></style>

values-19:

<style name="TranslucentTheme" parent="@style/AppTheme">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">false</item>
</style>

 values-V21

<style name="TranslucentTheme" parent="@style/AppTheme">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">false</item>
    <!-- v-21 中新增的属性 -->
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

1.自定义TranslucentScrollView类:

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.support.v4.graphics.ColorUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ScrollView;

import test.com.R;
import test.com.utils.SizeUtils;

public class TranslucentScrollView extends ScrollView {

    static final String TAG = "TranslucentScrollView";

    //伸缩视图
    private View zoomView;
    //伸缩视图初始高度
    private int zoomViewInitHeight = 0;
    // 记录首次按下位置
    private float mFirstPosition = 0;
    // 是否正在放大
    private Boolean mScaling = false;

    //渐变的视图
    private View transView;
    //渐变颜色
    private int transColor = Color.WHITE;
    //渐变开始位置
    private int transStartY = 50;
    //渐变结束位置
    private int transEndY = 300;

    //渐变开始默认位置,Y轴,50dp
    private final int DFT_TRANSSTARTY = 50;
    //渐变结束默认位置,Y轴,300dp
    private final int DFT_TRANSENDY = 300;

    private TranslucentScrollView.TranslucentChangedListener translucentChangedListener;

    public interface TranslucentChangedListener {
        /**
         * 透明度变化,取值范围0-255
         *
         * @param transAlpha
         */
        void onTranslucentChanged(int transAlpha);
    }

    public TranslucentScrollView(Context context) {
        super(context);
    }

    public TranslucentScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TranslucentScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setTranslucentChangedListener(TranslucentScrollView.TranslucentChangedListener translucentChangedListener) {
        this.translucentChangedListener = translucentChangedListener;
    }

    /**
     * 设置伸缩视图
     *
     * @param zoomView
     */
    public void setPullZoomView(View zoomView) {
        this.zoomView = zoomView;
        zoomViewInitHeight = zoomView.getLayoutParams().height;
        if (zoomViewInitHeight == LayoutParams.MATCH_PARENT || zoomViewInitHeight == WindowManager.LayoutParams.WRAP_CONTENT) {
            zoomView.post(new Runnable() {
                @Override
                public void run() {
                    zoomViewInitHeight = TranslucentScrollView.this.zoomView.getHeight();
                }
            });
        }
    }

    /**
     * 设置渐变视图
     *
     * @param transView 渐变的视图
     */
    public void setTransView(View transView) {
        setTransView(transView, getResources().getColor(R.color.colorPrimary), SizeUtils.dip2px(getContext(), DFT_TRANSSTARTY), SizeUtils.dip2px(getContext(), DFT_TRANSENDY));
    }

    /**
     * 设置渐变颜色
     *
     * @param colorRes 渐变的颜色
     */
    public void setTransColor(@ColorInt int colorRes) {
        this.transColor = colorRes;
    }

    /**
     * 设置渐变视图
     *
     * @param transView  渐变的视图
     * @param transColor 渐变颜色
     * @param transEndY  渐变结束位置
     */
    public void setTransView(View transView, @ColorInt int transColor, int transStartY, int transEndY) {
        this.transView = transView;
        //初始视图-透明
        this.transView.setBackgroundColor(ColorUtils.setAlphaComponent(transColor, 0));
        this.transStartY = transStartY;
        this.transEndY = transEndY;
        this.transColor = transColor;
        if (transStartY > transEndY) {
            throw new IllegalArgumentException("transStartY 不得大于 transEndY .. ");
        }
    }

    /**
     * 获取透明度
     *
     * @return
     */
    private int getTransAlpha() {
        float scrollY = getScrollY();
        if (transStartY != 0) {
            if (scrollY <= transStartY) {
                return 0;
            } else if (scrollY >= transEndY) {
                return 255;
            } else {
                return (int) ((scrollY - transStartY) / (transEndY - transStartY) * 255);
            }
        } else {
            if (scrollY >= transEndY) {
                return 255;
            }
            return (int) ((transEndY - scrollY) / transEndY * 255);
        }
    }

    /**
     * 重置ZoomView
     */
    private void resetZoomView() {
        final ViewGroup.LayoutParams lp = zoomView.getLayoutParams();
        final float h = zoomView.getLayoutParams().height;// ZoomView当前高度

        // 设置动画
        ValueAnimator anim = ObjectAnimator.ofFloat(0.0F, 1.0F).setDuration(200);

        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float cVal = (Float) animation.getAnimatedValue();
                lp.height = (int) (h - (h - zoomViewInitHeight) * cVal);
                zoomView.setLayoutParams(lp);
            }
        });
        anim.start();
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        int transAlpha = getTransAlpha();

        if (transView != null) {
            Log.d(TAG, "[onScrollChanged .. in ], 透明度 == " + transAlpha);
            transView.setBackgroundColor(ColorUtils.setAlphaComponent(transColor, transAlpha));
        }
        if (translucentChangedListener != null) {
            translucentChangedListener.onTranslucentChanged(transAlpha);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (zoomView != null) {
            ViewGroup.LayoutParams params = zoomView.getLayoutParams();
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    //手指离开后恢复图片
                    mScaling = false;
                    resetZoomView();
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (!mScaling) {
                        if (getScrollY() == 0) {
                            mFirstPosition = event.getY();
                        } else {
                            break;
                        }
                    }

                    int distance = (int) ((event.getY() - mFirstPosition) * 0.6);
                    if (distance < 0) {
                        break;
                    }
                    mScaling = true;
                    params.height = zoomViewInitHeight + distance;

                    Log.d(TAG, "params.height == " + params.height + ", zoomViewInitHeight == " + zoomViewInitHeight + ", distance == " + distance);
                    zoomView.setLayoutParams(params);
                    return true;
            }
        }

        return super.onTouchEvent(event);
    }

}

 2.主函数布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <test.com.widget.TranslucentScrollView
        android:id="@+id/pullzoom_scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/lay_header"
                android:layout_width="match_parent"
                android:layout_height="248dp"
                android:background="@mipmap/bg_banner_my"
                android:orientation="vertical">

                <View
                    android:id="@+id/v_statusbar"
                    android:layout_width="match_parent"
                    android:layout_height="0.0dp"
                    android:background="@color/translate" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <RelativeLayout
                        android:layout_width="137dp"
                        android:layout_height="137dp"
                        android:layout_centerInParent="true"
                        android:background="@mipmap/bg_avatar">

                        <ImageView
                            android:id="@+id/img_avatar"
                            android:layout_width="105dp"
                            android:layout_height="105dp"
                            android:layout_centerInParent="true"
                            android:scaleType="fitXY"
                            android:src="@mipmap/dft_avatar" />
                    </RelativeLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_centerHorizontal="true"
                        android:layout_marginBottom="15dp"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/tv_name"
                            style="@style/text_white"
                            android:layout_gravity="center_horizontal"
                            android:text="@string/auther"
                            android:textColor="@color/white"
                            android:textSize="15sp" />

                        <TextView
                            android:id="@+id/tv_fans"
                            style="@style/text_white"
                            android:layout_gravity="center_horizontal"
                            android:layout_marginTop="4dp"
                            android:text="@string/email"
                            android:textSize="13sp" />
                    </LinearLayout>
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:orientation="horizontal"
                android:paddingBottom="8dp"
                android:paddingTop="8dp">

                <LinearLayout
                    android:id="@+id/lay_car"
                    android:layout_width="0.0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:scaleType="fitXY"
                        android:src="@mipmap/ic_shopcar_my" />

                    <TextView
                        style="@style/text_black"
                        android:layout_marginTop="5dp"
                        android:text="@string/shopping_car" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/lay_history"
                    android:layout_width="0.0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:scaleType="fitXY"
                        android:src="@mipmap/ic_consume_history" />

                    <TextView
                        style="@style/text_black"
                        android:layout_width="wrap_content"
                        android:layout_marginTop="5dp"
                        android:text="@string/consume_history" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/lay_wallet"
                    android:layout_width="0.0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:scaleType="fitXY"
                        android:src="@mipmap/ic_wallet" />

                    <TextView
                        style="@style/text_black"
                        android:layout_marginTop="5dp"
                        android:text="@string/wallet" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:orientation="vertical">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="15dp"
                    android:background="@color/bg_main" />

                <LinearLayout
                    android:id="@+id/lay_sign"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@color/white"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="16dp"
                        android:background="@mipmap/ic_sign" />

                    <TextView
                        style="@style/text_black"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="17dp"
                        android:text="@string/sign" />

                    <TextView
                        android:id="@+id/tv_has_sign"
                        style="@style/text_black"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="3dp"
                        android:layout_weight="1"
                        android:text="@string/today_has_sign"
                        android:textColor="@color/gray_dft"
                        android:textSize="13sp"
                        android:visibility="invisible" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="16dp"
                        android:background="@mipmap/ic_right_gray" />
                </LinearLayout>

                <View
                    style="@style/horizontal_divider_light"
                    android:layout_marginLeft="16dp" />

                <LinearLayout
                    android:id="@+id/lay_address"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@color/white"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="16dp"
                        android:background="@mipmap/ic_address" />

                    <TextView
                        style="@style/text_black"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="17dp"
                        android:layout_weight="1"
                        android:text="@string/receive_address" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="16dp"
                        android:background="@mipmap/ic_right_gray" />
                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="15dp"
                    android:background="@color/bg_main" />

                <LinearLayout
                    android:id="@+id/lay_agent"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@color/white"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="16dp"
                        android:background="@mipmap/ic_agent_my" />

                    <TextView
                        style="@style/text_black"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="17dp"
                        android:layout_weight="1"
                        android:text="@string/my_agent" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="16dp"
                        android:background="@mipmap/ic_right_gray" />
                </LinearLayout>

                <View
                    style="@style/horizontal_divider_light"
                    android:layout_marginLeft="16dp" />

                <LinearLayout
                    android:id="@+id/lay_teacher"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@color/white"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="16dp"
                        android:background="@mipmap/ic_teacher_my" />

                    <TextView
                        style="@style/text_black"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="17dp"
                        android:layout_weight="1"
                        android:text="@string/i_want_a_teacher" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="16dp"
                        android:background="@mipmap/ic_right_gray" />
                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="15dp"
                    android:background="@color/bg_main" />

                <LinearLayout
                    android:id="@+id/lay_luck"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@color/white"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="16dp"
                        android:background="@mipmap/ic_luck_my" />

                    <TextView
                        style="@style/text_black"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="17dp"
                        android:layout_weight="1"
                        android:text="@string/start_luck" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="16dp"
                        android:background="@mipmap/ic_right_gray" />
                </LinearLayout>

                <View
                    style="@style/horizontal_divider_light"
                    android:layout_marginLeft="16dp" />

                <LinearLayout
                    android:id="@+id/lay_invite"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@color/white"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="16dp"
                        android:background="@mipmap/ic_invite_my" />

                    <TextView
                        style="@style/text_black"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="17dp"
                        android:layout_weight="1"
                        android:text="@string/invite_reward" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="16dp"
                        android:background="@mipmap/ic_right_gray" />
                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="15dp"
                    android:background="@color/bg_main" />

                <LinearLayout
                    android:id="@+id/lay_set"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@color/white"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="16dp"
                        android:background="@mipmap/ic_set_my" />

                    <TextView
                        style="@style/text_black"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="17dp"
                        android:layout_weight="1"
                        android:text="@string/set" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="16dp"
                        android:background="@mipmap/ic_right_gray" />
                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="140dp"
                    android:background="@color/bg_main" />
            </LinearLayout>
        </LinearLayout>
    </test.com.widget.TranslucentScrollView>

    <test.com.widget.TranslucentActionBar
        android:id="@+id/actionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/orange_dft" />

</RelativeLayout>

3.主函数逻辑:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import test.com.base.BaseActivity;
import test.com.impl.ActionBarClickListener;
import test.com.widget.TranslucentActionBar;
import test.com.widget.TranslucentScrollView;

public class MainActivity extends BaseActivity implements ActionBarClickListener, TranslucentScrollView.TranslucentChangedListener {

    private TranslucentScrollView translucentScrollView;
    private TranslucentActionBar actionBar;
    private View zoomView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
        actionBar = (TranslucentActionBar) findViewById(R.id.actionbar);
        //初始actionBar
        actionBar.setData("测试", 0, null, 0, null, null);
        //开启渐变
        actionBar.setNeedTranslucent();
        //设置状态栏高度
        actionBar.setStatusBarHeight(getStatusBarHeight());

        translucentScrollView = (TranslucentScrollView) findViewById(R.id.pullzoom_scrollview);
        //设置透明度变化监听
        translucentScrollView.setTranslucentChangedListener(this);
        //关联需要渐变的视图
        translucentScrollView.setTransView(actionBar);
        //设置ActionBar键渐变颜色
        translucentScrollView.setTransColor(getResources().getColor(R.color.orange_dft));

        zoomView = findViewById(R.id.lay_header);
        //关联伸缩的视图
        translucentScrollView.setPullZoomView(zoomView);
    }

    @Override
    public void onLeftClick() {

    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onTranslucentChanged(int transAlpha) {
        actionBar.tvTitle.setVisibility(transAlpha > 48 ? View.VISIBLE : View.GONE);
    }

}

4.ActionBarClickListener


public interface ActionBarClickListener {

    void onLeftClick();

    void onRightClick();

}

5.TranslucentActionBar

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import test.com.R;
import test.com.impl.ActionBarClickListener;

/**
 * 支持渐变的 actionBar
 */

public final class TranslucentActionBar extends LinearLayout {

    private View layRoot;
    private View vStatusBar;
    private View layLeft;
    private View layRight;
    public TextView tvTitle;
    private TextView tvLeft;
    private TextView tvRight;
    private View iconLeft;
    private View iconRight;

    public TranslucentActionBar(Context context) {
        this(context, null);
    }

    public TranslucentActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TranslucentActionBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        setOrientation(HORIZONTAL);
        View contentView = inflate(getContext(), R.layout.actionbar_trans, this);
        layRoot = contentView.findViewById(R.id.lay_transroot);
        vStatusBar = contentView.findViewById(R.id.v_statusbar);
        tvTitle = (TextView) contentView.findViewById(R.id.tv_actionbar_title);
        tvLeft = (TextView) contentView.findViewById(R.id.tv_actionbar_left);
        tvRight = (TextView) contentView.findViewById(R.id.tv_actionbar_right);
        iconLeft = contentView.findViewById(R.id.iv_actionbar_left);
        iconRight = contentView.findViewById(R.id.v_actionbar_right);
    }

    /**
     * 设置状态栏高度
     *
     * @param statusBarHeight
     */
    public void setStatusBarHeight(int statusBarHeight) {
        ViewGroup.LayoutParams params = vStatusBar.getLayoutParams();
        params.height = statusBarHeight;
        vStatusBar.setLayoutParams(params);
    }

    /**
     * 设置是否需要渐变
     */
    public void setNeedTranslucent() {
        setNeedTranslucent(true, false);
    }

    /**
     * 设置是否需要渐变,并且隐藏标题
     *
     * @param translucent
     */
    public void setNeedTranslucent(boolean translucent, boolean titleInitVisibile) {
        if (translucent) {
            layRoot.setBackgroundDrawable(null);
        }
        if (!titleInitVisibile) {
            tvTitle.setVisibility(View.GONE);
        }
    }

    /**
     * 设置标题
     *
     * @param strTitle
     */
    public void setTitle(String strTitle) {
        if (!TextUtils.isEmpty(strTitle)) {
            tvTitle.setText(strTitle);
        } else {
            tvTitle.setVisibility(View.GONE);
        }
    }

    /**
     * 设置数据
     *
     * @param strTitle
     * @param resIdLeft
     * @param strLeft
     * @param resIdRight
     * @param strRight
     * @param listener
     */
    public void setData(String strTitle, int resIdLeft, String strLeft, int resIdRight, String strRight, final ActionBarClickListener listener) {
        if (!TextUtils.isEmpty(strTitle)) {
            tvTitle.setText(strTitle);
        } else {
            tvTitle.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(strLeft)) {
            tvLeft.setText(strLeft);
            tvLeft.setVisibility(View.VISIBLE);
        } else {
            tvLeft.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(strRight)) {
            tvRight.setText(strRight);
            tvRight.setVisibility(View.VISIBLE);
        } else {
            tvRight.setVisibility(View.GONE);
        }

        if (resIdLeft == 0) {
            iconLeft.setVisibility(View.GONE);
        } else {
            iconLeft.setBackgroundResource(resIdLeft);
            iconLeft.setVisibility(View.VISIBLE);
        }

        if (resIdRight == 0) {
            iconRight.setVisibility(View.GONE);
        } else {
            iconRight.setBackgroundResource(resIdRight);
            iconRight.setVisibility(View.VISIBLE);
        }

        if (listener != null) {
            layLeft = findViewById(R.id.lay_actionbar_left);
            layRight = findViewById(R.id.lay_actionbar_right);
            layLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onLeftClick();
                }
            });
            layRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onRightClick();
                }
            });
        }
    }

}

6.actionbar_trans布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lay_transroot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <View
        android:id="@+id/v_statusbar"
        android:layout_width="match_parent"
        android:layout_height="1.0dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/lay_actionbar_left"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv_actionbar_left"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:background="@mipmap/ic_left_light"
                android:visibility="gone" />

            <TextView
                android:id="@+id/tv_actionbar_left"
                style="@style/text_white"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/iv_actionbar_left"
                android:gravity="center_vertical"
                android:maxLength="2"
                android:singleLine="true"
                android:text="返回"
                android:visibility="gone" />
        </RelativeLayout>

        <TextView
            android:id="@+id/tv_actionbar_title"
            style="@style/text_white"
            android:layout_centerInParent="true"
            android:text="标题"
            android:textSize="16sp" />

        <RelativeLayout
            android:id="@+id/lay_actionbar_right"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:gravity="right"
            android:orientation="horizontal">

            <View
                android:id="@+id/v_actionbar_right"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/tv_actionbar_right"
                style="@style/text_white"
                android:layout_height="match_parent"
                android:layout_marginRight="10dp"
                android:layout_toLeftOf="@+id/v_actionbar_right"
                android:gravity="center_vertical|right"
                android:singleLine="true"
                android:visibility="gone" />
        </RelativeLayout>

    </RelativeLayout>
</LinearLayout>

下载地址:https://github.com/yanjunhui2014/TranslucentScrollView

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值