Android之scrollview滑动使标题栏渐变背景色的实例代码
作者:岁月LICHENGAN 发布时间:2022-10-24 19:26:11
之前也是在网上看到这种效果,不过是滚动listview来改变标题栏的颜色,感觉那个应用的比较少,比如我要滚动scrollview来实现呢,那么问题就来了,废话少说,看一下要实现的效果先(这是在项目应用的效果)。
直接上源代码:
一、核心类(ObservableScrollView.java)
package com.jukopro.titlebarcolor;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/**
* 带滚动监听的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);
}
}
}
二、具体使用(MainActivity.java)
package com.jukopro.titlebarcolor;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.jukopro.titlebarcolor.ObservableScrollView.ScrollViewListener;
public class MainActivity extends Activity implements ScrollViewListener{
private ObservableScrollView scrollView;
private ListView listView;
private ImageView imageView;
private TextView textView;
private int imageHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ObservableScrollView) findViewById(R.id.scrollview);
listView = (ListView) findViewById(R.id.listview);
imageView = (ImageView) findViewById(R.id.imageview);
textView = (TextView) findViewById(R.id.textview);
initListeners();
initData();
}
private void initListeners() {
// 获取顶部图片高度后,设置滚动监听
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
imageHeight = imageView.getHeight();
scrollView.setScrollViewListener(MainActivity.this);
}
});
}
private void initData() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.data));
listView.setAdapter(adapter);
}
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy) {
// TODO Auto-generated method stub
// Log.i("TAG", "y--->" + y + " height-->" + height);
if (y <= 0) {
textView.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得,或者美工提供
} else if (y > 0 && y <= imageHeight) {
float scale = (float) y / imageHeight;
float alpha = (255 * scale);
// 只是layout背景透明(仿知乎滑动效果)
textView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));
} else {
textView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));
}
}
}
三、XML(activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <com.jukopro.titlebarcolor.ObservableScrollView android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="200dp" android:background="@drawable/zuqiu" /> <com.jukopro.titlebarcolor.MyListview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" > </com.jukopro.titlebarcolor.MyListview> </LinearLayout> </com.jukopro.titlebarcolor.ObservableScrollView> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center" android:text="我是标题" android:textSize="18sp" android:textColor="@android:color/white" android:background="#00000000" /> </RelativeLayout>
还不懂的童鞋可以下载源代码.
来源:https://blog.csdn.net/sinat_21283073/article/details/51315926
猜你喜欢
- 队列是其元素按照先进先出(FIFO)的方式来处理的集合。队列使用System.Collections.Generic名称空间中的泛型类Que
- 概述在使用maven进行Java项目的开发过程中,难免会有些公共的私有库,这些库是不太方便放到中央仓库的,可以通过Nexus搭建一个私有仓库
- menu部分xml代码<?xml version="1.0" encoding="utf-8"
- 最近没事写了一个简易浏览器,在刚开始写的时候遇到一些问题,主要的问题就是如何在自己的webview中显示所有的网页数据,不过不指
- 位运算 位运算的运算分量只能是整型或字符型数据,位运算把运算对象看作是由二进位组成的位串信息,按位完成指
- java * 的方法总结AOP的拦截功能是由java中的 * 来实现的。说白了,就是在目标类的基础上增加切面逻辑,生成增强的目标类(该
- 本文实例分析了采用C#实现软件自动更新的方法,是非常实用的功能,值得学习和借鉴。具体如下:1.问题概述:长期以来,广大程序员为到底是使用Cl
- 前言锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) 。
- 如题,主要使用AsReadOnly这个方法就可以了List<int> a = new List<int> {1, 2
- 今天把Android Studio 2.3 更新为了3.0 遇到一个蛋疼的问题如图:格式化完代码后发现不会自动换行了,看着真心不爽。后来发现
- 创建一个列表消息卡片到目前为止,我们只有一个消息的卡片,看上去有点单调,所以让我们来改善它,让它拥有多条信息。我们需要创建一个能够显示多条消
- 一本书的页码从自然数1开始顺序编码直到自然数n。书的页码按照通常的习惯编排,每个页码都不含多余的前导数字0。例如第6页用6表示而不是06或0
- 框架的概述JDBC存在的问题:我们要想研究mybatis就必须知道jdbc所存在的问题,那我那么我们首先来复习一下jdbc操作数据库的大致流
- 目录断言对象、数组、集合ObjectUtilsStringUtilsCollectionUtils文件、资源、IO 流FileCopyUti
- 本文实例分析了C# SQlite操作方法。分享给大家供大家参考,具体如下:最近项目需求用C#保存一些数据,如此先总结一下。需要下载Sqlit
- Jar包加密工具 ClassFinal介绍ClassFinal 是一款 java class 文件安全加密工具,支持直接加密jar包或war
- 前面几篇案例已经将常用的交换器(DirectExchange、TopicExchange、FanoutExchange)的用法介绍完了,现在
- MyBatis查询返回null可能原因SQL语句查询条件有问题数据库中没数据返回字段与Entity的属性不对应解决方案针对 SQL
- 配置文件中使用${}注入值方式在springboot中使用System.setProperty设置参数user: user-na
- spring boot诞生的背景在spring boot出现以前,使用spring框架的程序员是这样配置web应用环境的,需要大量的xml配