随着ScrollView上下滚动 标题栏透明度随之而改变

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qidingquan/article/details/49664329

Activity中的代码
package qdq.scolldistancetest;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {
    private MyScrollView scrollView;
    private ImageView background_iv;//红色背景布局
    private RelativeLayout title_layout; //标题栏布局
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    public void initView() {
        background_iv = (ImageView) findViewById(R.id.background_iv);
        title_layout = (RelativeLayout) findViewById(R.id.title_layout);
        scrollView = (MyScrollView) findViewById(R.id.scrollView);
        scrollView.setOnScrollistener(new MyScrollView.OnScrollistener() {
            @Override
            public void onScroll(int startY, int endY) {
                //根据scrollview滑动更改标题栏透明度
                dynamicChangeAphla(startY, endY);
            }
        });
    }

    /**
     * 根据内容窗体的移动改变标题栏背景透明度
     * @param startY scrollview开始滑动的y坐标(相对值)
     * @param endY scrollview结束滑动的y坐标(相对值)
     */
    private void dynamicChangeAphla(int startY, int endY) {
        //获取到状态栏的高度
        int statusBarHeight = getStatusBarHeight(this);
        //获取标题高度
        int titleHeight= title_layout.getLayoutParams().height;
        //获取背景高度
        int backHeight=background_iv.getLayoutParams().height;

        //获取控件的绝对位置坐标
        int[] location = new int[2];
        background_iv.getLocationInWindow(location);
        //从屏幕顶部到控件顶部的坐标位置Y
        int currentY=location[1];
        //表示回到原位(滑动到顶部)
        if (currentY>=statusBarHeight){
            title_layout.setBackgroundColor(Color.argb(0,0,255,0));
        }
        //红色背景移除或移入屏幕的过程中;颜色透明度改变
        if (currentY<statusBarHeight&&currentY>=-backHeight){
            //计算出滚动过程中改变的透明值
            double changeValue=((double)Math.abs(currentY) /(statusBarHeight + backHeight))*255;
            //判断是向上还是向下
            if (endY>startY){//向上;透明度值增加,实际透明度减小
                title_layout.setBackgroundColor(Color.argb((int)changeValue,0,255,0));
            }else if(endY<startY){//向下;透明度值减小,实际透明度增加
                title_layout.setBackgroundColor(Color.argb((int)changeValue,0,255,0));
            }
        }
        //红色背景移除屏幕
        if(currentY<-backHeight){
            title_layout.setBackgroundColor(Color.argb(255,0,255,0));
        }
    }

    /**
     * 获取通知栏高度
     * @param context 上下文
     * @return 通知栏高度
     */
    private int getStatusBarHeight(Context context){
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }
}
</span>
自定义ScrollView添加滚动监听
package qdq.scolldistancetest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * 自定义 scrollview
 */
public class MyScrollView extends ScrollView{

    private OnScrollistener onScrollistener;
    public OnScrollistener getOnScrollistener() {
        return onScrollistener;
    }
    public void setOnScrollistener(OnScrollistener onScrollistener) {
        this.onScrollistener = onScrollistener;
    }
    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

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

    public interface OnScrollistener{
        void onScroll(int startY, int endY);
    }
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        onScrollistener.onScroll(oldt,t);
        super.onScrollChanged(l, t, oldl, oldt);
    }
}
</span>

布局文件中使用自定义ScrollView

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

    <qdq.scolldistancetest.MyScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="250dp" >
                <ImageView
                    android:id="@+id/background_iv"
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:background="#ff0000" />
                <!--添加一层遮盖-->
                <View
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:background="@drawable/background_shape" />
            </RelativeLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="800dp"/>
        </LinearLayout>
    </qdq.scolldistancetest.MyScrollView>

    <RelativeLayout
        android:id="@+id/title_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#0000FF00" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_margin="10dp"
            android:text="标题"
            android:textColor="#696969"
            android:textSize="20sp" />
    </RelativeLayout>
</RelativeLayout></span>
AndroidManifest.xml中修改主题样式,去掉状态栏

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

展开阅读全文

没有更多推荐了,返回首页