ScrollView上下滑动修改顶部title背景色淡入淡出

MainActivity XML:

<?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">

    <com.example.scroll.ObservableScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

        <com.example.scroll.ConflictListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.example.scroll.ObservableScrollView>

    <RelativeLayout
        android:id="@+id/relative"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:paddingTop="20dp">

        <Button
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="点我" />

        <Button
            android:id="@+id/bt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="点我" />
    </RelativeLayout>
</RelativeLayout>


MainActivity:

public class MainActivity extends AppCompatActivity implements ObservableScrollView.ScrollViewListener {

    private ObservableScrollView mScrollView;
    private ConflictListView mListView;
    private RelativeLayout mTextView;
    private int imageHight;
    private ImageView mImageView;
    private View headerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WindowUtils.setTraspartenStatusBar(this);
        mListView = (ConflictListView) findViewById(R.id.lv);
        mTextView = (RelativeLayout) findViewById(R.id.relative);
        mScrollView = (ObservableScrollView) findViewById(R.id.scroll);
        headerView = View.inflate(this, R.layout.header, null);
        mImageView = (ImageView) headerView.findViewById(R.id.imageview);
        Button bt1 = (Button) findViewById(R.id.bt1);
        Button bt2 = (Button) findViewById(R.id.bt2);
        findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "点我1", Toast.LENGTH_SHORT).show();
            }
        });
        findViewById(R.id.bt2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "点我2", Toast.LENGTH_SHORT).show();
            }
        });
        initListeners();
        initData();
    }

    private void initData() {
        mListView.addHeaderView(headerView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.data));
        mListView.setAdapter(adapter);
    }

    private void initListeners() {
        // 获取顶部图片高度后,设置滚动监听
        ViewTreeObserver vto = mImageView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                imageHight = mImageView.getHeight();
                mScrollView.setScrollViewListener(MainActivity.this);
            }
        });
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        Log.i("TAG", "y--->" + y);
        if (y <= 0) {
            mTextView.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得,或者美工提供
        } else if (y > 0 && y <= imageHight) {
            float scale = (float) y / imageHight;
            float alpha = (255 * scale);
            // 只是layout背景透明(仿知乎滑动效果)
            mTextView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));
        } else {
            mTextView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));
        }
    }
}

自定义的ScrollView:

public class ObservableScrollView extends ScrollView {

	public interface ScrollViewListener {

		void onScrollChanged(ObservableScrollView scrollView, int x, int y,
                             int oldx, int oldy);

	}

	private ScrollViewListener scrollViewListener = null;

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

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

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

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

	@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);
		}
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
android 下拉,淡入淡出特效源码 依赖support v7 // /** * PopupWindow上菜单进入动画 */ public static Animation createPopupAnimIn(Context context, int fromYDelta) { AnimationSet animationSet = new AnimationSet(context, null); // animationSet.setInterpolator(new BounceInterpolator()); //结束时弹跳 animationSet.setFillAfter(true); //移动 TranslateAnimation translateAnim = new TranslateAnimation(0, 0, fromYDelta, 20); translateAnim.setDuration(TIME_IN); animationSet.addAnimation(translateAnim); //回弹效果 TranslateAnimation translateAnim2 = new TranslateAnimation(0, 0, 0, -20); translateAnim2.setStartOffset(TIME_IN); translateAnim2.setDuration(TIME_IN_BACK); animationSet.addAnimation(translateAnim2); return animationSet; } /** * PopupWindow上菜单离开动画 */ public static Animation createPopupAnimOut(Context context, int toYDelta) { AnimationSet animationSet = new AnimationSet(context, null); animationSet.setFillAfter(true); TranslateAnimation translateAnim = new TranslateAnimation(0, 0, 0, toYDelta); translateAnim.setDuration(TIME_OUT); animationSet.addAnimation(translateAnim); return animationSet; } /** * PopupWindow背景进入动画(透明度渐变) */ public static Animation createPopupBgFadeInAnim() { AlphaAnimation anim = new AlphaAnimation(0, 1.0f); anim.setDuration(TIME_IN); anim.setFillAfter(true); return anim; } /** * PopupWindow背景离开动画(透明度渐变) */ public static Animation createPopupBgFadeOutAnim(int duration) { AlphaAnimation anim = new AlphaAnimation(1.0f, 0); anim.setDuration(duration); anim.setFillAfter(true); return anim; } /** * PopupWindow按钮点击动画 */ public static Animation createPopupItemBiggerAnim(Context context) { AnimationSet animationSet = new AnimationSet(context, null); animationSet.setFillAfter(true); //放大(设置缩放的中心点为自己的中心) ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnim.setDuration(TIME_OUT_CLICK); animationSet.addAnimation(scaleAnim); //渐变 AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0); alphaAnim.setInterpolator(new AccelerateInterpolator()); alphaAnim.setDuration(TIME_OUT_CLICK); animationSet.addAnimation(alphaAnim); return animationSet; } /** * PopupWindow按钮点击时其它按钮的动画 */ public static Animation createPopupItemSmallerAnim(Context context) { //放大(设置缩放的中心点为自己的中心) ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 0, 1.0f, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnim.setDuration(TIME_OUT_CLICK); scaleAnim.setFillAfter(true); return scaleAnim; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值