安卓 ScrollView标题栏随手势变化透明


对于标题栏随手势变化透明度应该是一个非常常见的功能了,包括京东和淘宝都有类似的功能.


最近工作里面需要就去网上查了查,发现都是对listview基础上写的,那么现在写一个基于scrollView的.

使用listView的原文链接http://blog.csdn.net/zhaokaiqiang1992/article/details/35237467

1,首先遇到的第一个问题是scroll没有onScrollChangedLinister,先写一个有onScrollChangedLinister监听的自定义scrollView


package com.teekart.view;

/**
 * Created by king on 2015/11/6.
 */
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;


/**
 * 定义一个有onScrollChanged监听的ScrollView
 * User: Jerry
 * Date: 13-3-11
 * Time: 下午6:27
 */
public class ObservableScrollView extends ScrollView{
    private ScrollViewListener scrollViewListener = null;


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


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


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


    public void setScrollViewListener(ScrollViewListener listener) {
        this.scrollViewListener = listener;
    }


    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if(scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }


    public interface ScrollViewListener {
        void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
    }
}



2,把我们的scrollView替换掉

<com.teekart.view.ObservableScrollView
        android:id="@+id/scrollView1"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/include1"
        android:scrollbars="none">



3,使用它

scrollView1 = (ObservableScrollView) findViewById(R.id.scrollView1);
		scrollView1.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
			@Override
			public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
			// 获取当前位置占头布局高度的百分比
				int height = scrollView.getChildAt(0).getMeasuredHeight()/3;
				if (y<0){y=0;}
				float f = (float) y / (float) height;
				if (f>1){f=1;}
				Log.i("scrollview","x="+x+" y="+y+" oldx="+oldx+" oldy="+oldy + "height" +height+"f"+f);
				rl_travel_title.getBackground().setAlpha((int) (f * 255));
				// 通知标题栏刷新显示
				rl_travel_title.invalidate();
			}
		});



东西比较简单,我就不上源码了.效果图






评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值