Android ListView滑动改变标题栏背景渐变效果
作者:u014620028 发布时间:2022-07-01 04:00:59
先上ListView滑动改变标题栏背景渐变效果图,透明转变成不透明效果:
图1:
图2:
图3:
图4:
我用的是小米Note手机,状态栏高度是55px,后面会提到,这里先做个说明:
下面的内容包含了所有代码和一些测试数据:
代码:
代码很简单,也做了注释,这里就不废话了。
先来布局文件:
activity的布局
activity_main_10
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listvew"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--标题栏,这里简单写个textview-->
<TextView
android:id="@+id/title_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp"
android:textSize="30sp"/>
</RelativeLayout>
listView头布局
head_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/head_iv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@mipmap/ch"/>
</LinearLayout>
listView的item布局
listview_item_layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/item_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="15sp">
</TextView>
</FrameLayout>
功能代码:
MainActivity10
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity10 extends Activity {
private TextView title_tv;
private ListView listvew;
//listView的头
private View headView;
//listView头中包含的布局。这里仅仅是一个ImageView
private ImageView head_iv;
private ArrayList<String> dataList;
private MyAdapter myAdapter;
//listview的头部(这里是ImageView)顶部距离屏幕顶部(包含状态栏)的距离
//注:这个高度,是头布局在屏幕里才会计算的,出了屏幕,就不会变了
private int height;
//listView的头部的真实高度。头布局的整体高度,因为这个demo只简单写了个ImageView作为头部,所以ImageView的高度,就是头部的高度
private int headViewHeight;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_10);
context = this;
title_tv = (TextView) findViewById(R.id.title_tv);
listvew = (ListView) findViewById(R.id.listvew);
headView = LayoutInflater.from(this).inflate(R.layout.head_layout, null);
head_iv = (ImageView) headView.findViewById(R.id.head_iv);
//添加listView的头布局
listvew.addHeaderView(headView);
dataList = new ArrayList<>();
for (int i = 0; i < 50; i++) {
dataList.add("==" + i + "==");
}
myAdapter = new MyAdapter();
listvew.setAdapter(myAdapter);
listvew.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int location[] = new int[2];
/**
* public void getLocationInWindow(@Size(2) int[] location)
*
* <p>Computes 计算 the coordinates 坐标 of this view in its window. The argument 参数
* must be an array of two integers. After the method returns, the array
* contains 包含 the x and y location in that order.</p>
*
* @param location an array of two integers in which to hold the coordinates
*/
head_iv.getLocationInWindow(location);
//listview的头部(这里是ImageView)顶部距离屏幕顶部(包含状态栏)的距离
height = location[1];
headViewHeight = head_iv.getHeight();
Utils.printLogData("==location==0==" + location[0]);
Utils.printLogData("==location==1==" + location[1]);
Utils.printLogData("==height==" + height);
Utils.printLogData("==headViewHeight==" + headViewHeight);
//在head_layout.xml中,固定设置了150dp的高度,用于和上面测量的对比
Utils.printLogData("==setHeigth==" + dip2px(150));
handleTitleBarColorEvaluate();
}
});
}
// 处理标题栏颜色渐变
private void handleTitleBarColorEvaluate() {
//比例
float fraction;
if (height > 0) {
fraction = 1f - height * 1f / 60;
if (fraction < 0f) {
fraction = 0f;
}
title_tv.setAlpha(fraction);
return;
}
//高度值是负数,但是负号仅仅是表示方向,取绝对值
float space = Math.abs(height) * 1f;
// 标题栏的高度
fraction = space / headViewHeight;
if (fraction < 0f)
fraction = 0f;
if (fraction > 1f)
fraction = 1f;
title_tv.setAlpha(1f);
if (fraction >= 1f) {
title_tv.setBackgroundColor(0xffec434b);
} else {
//根据比例,生成一个按比例的颜色值
title_tv.setBackgroundColor(ChenColorUtils.getNewColorByStartEndColor(context, fraction, R.color.transparent, R.color.red));
}
if (fraction >= 0.8f) {
title_tv.setTextColor(ChenColorUtils.getNewColorByStartEndColor(context, fraction, R.color.transparent, R.color.black));
title_tv.setText("标题栏");
} else {
title_tv.setText("");
}
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return dataList.size();
}
@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) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.listview_item_layout, null);
holder = new ViewHolder();
holder.item_tv = (TextView) convertView.findViewById(R.id.item_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.item_tv.setText(dataList.get(position));
return convertView;
}
private class ViewHolder {
private TextView item_tv;
}
}
/**
* dip转换px
*/
public int dip2px(int dip) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f);
}
}
颜色:
colors.xml中添加
<color name="red">#ec434b</color>
<color name="transparent">#00000000</color>
<color name="black">#FF000000</color>
工具类代码:
打印日志工具:
import android.util.Log;
public class Utils {
public static void printLogData(String data) {
Log.e("chen", data);
}
}
颜色工具:
import android.content.Context;
public class ChenColorUtils {
// 成新的颜色值
public static int getNewColorByStartEndColor(Context context, float fraction, int startValue, int endValue) {
return evaluate(fraction, context.getResources().getColor(startValue), context.getResources().getColor(endValue));
}
/**
* 成新的颜色值
* @param fraction 颜色取值的级别 (0.0f ~ 1.0f)
* @param startValue 开始显示的颜色
* @param endValue 结束显示的颜色
* @return 返回生成新的颜色值
*/
public static int evaluate(float fraction, int startValue, int endValue) {
int startA = (startValue >> 24) & 0xff;
int startR = (startValue >> 16) & 0xff;
int startG = (startValue >> 8) & 0xff;
int startB = startValue & 0xff;
int endA = (endValue >> 24) & 0xff;
int endR = (endValue >> 16) & 0xff;
int endG = (endValue >> 8) & 0xff;
int endB = endValue & 0xff;
return ((startA + (int) (fraction * (endA - startA))) << 24) |
((startR + (int) (fraction * (endR - startR))) << 16) |
((startG + (int) (fraction * (endG - startG))) << 8) |
((startB + (int) (fraction * (endB - startB))));
}
}
测试数据:
界面刚启动
05-18 16:19:25.386 18718-18718/com.chen E/chen: ==location==0==0
05-18 16:19:25.387 18718-18718/com.chen E/chen: ==location==1==0
05-18 16:19:25.387 18718-18718/com.chen E/chen: ==height==0
05-18 16:19:25.387 18718-18718/com.chen E/chen: ==headViewHeight==0
05-18 16:19:25.387 18718-18718/com.chen E/chen: ==setHeigth==413
从时间上看,启动约150毫秒(0.15秒)后
05-18 16:19:25.531 18718-18718/com.chen E/chen: ==location==0==0
05-18 16:19:25.531 18718-18718/com.chen E/chen: ==location==1==55
05-18 16:19:25.531 18718-18718/com.chen E/chen: ==height==55
05-18 16:19:25.531 18718-18718/com.chen E/chen: ==headViewHeight==413
05-18 16:19:25.531 18718-18718/com.chen E/chen: ==setHeigth==413
小米note,状态栏高度是55像素。所以,一开始的时候,图片距离屏幕顶部的高度就是55
向上滑动,当头布局完全出到屏幕外面后,继续滑动,打印数据不会变。
即:头布局顶部距离屏幕顶部的高度(距离)不变。因为这个高度,只会在view在屏幕中才能获取到。
详见注释
05-18 17:01:02.151 16873-16873/com.chen E/chen: ==height==-412
05-18 17:01:02.167 16873-16873/com.chen E/chen: ==height==-412
05-18 17:01:02.200 16873-16873/com.chen E/chen: ==height==-412
05-18 17:01:02.233 16873-16873/com.chen E/chen: ==height==-412
05-18 17:01:02.316 16873-16873/com.chen E/chen: ==height==-412
05-18 17:01:02.332 16873-16873/com.chen E/chen: ==height==-412
05-18 17:01:02.349 16873-16873/com.chen E/chen: ==height==-412


猜你喜欢
- 1,什么是Eureka,什么是服务注册与发现 Spring Boot作为目前最火爆的web框架。那么它与Eureka又有什么关联呢?Eure
- 题目描述:一个农夫带着一匹狼、一只羊、一颗白菜要过河,只有一条船而且农夫每次最多只能带一个动物或物品过河,并且当农夫不在的时候狼会吃羊,羊会
- C语言 MD5源码md5c.h:/* POINTER defines a generic pointer type */ typedef u
- 一、实验题目二、分析实验要求为:实现一个界面,界面中包含一个文本显示区和两个按钮(存档和读档)读档按钮作用是打开文件并读取内容,将内容显示在
- Vector实现班级信息管理系统,供大家参考,具体内容如下代码如下:import java.util.*;public class Demo
- 本文实例讲述了Android编程简单实现九宫格。分享给大家供大家参考,具体如下:实现的步骤1. 一个整体的容器部分。就是上图中包括整个图片项
- 查看apiDemos,找到View/Animation/shake找到对应的动画代码,直接拷贝过来当导入一个项目的时候,报R文件不存在,很多
- 软硬件环境Macbook Pro MGX 72Android studio 2.1.2Android 5.1.1前言上一篇介绍了如何获取et
- 继承"基类"跟继承"接口"都能实现某些相同的功能,但有些接口能够完成的功能是只用基类无法实现的1.接
- 1. 概述官方JavaDocsApi: javax.swing.JTextAreaJTextArea,文本区域。JTextArea 用来编辑
- 本文介绍了Spring Boot Admin监控服务上下线邮件通知,分享给大家,具体如下:微服务架构下,服务的数量少则几十,多则上百,对服务
- SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案。标准的MVC设计模式,将整个系统划分为显示层、Co
- 引言这一篇文章我们就通过介绍滑动冲突的规则和一个实例来更加深入的学习View的事件分发机制。1、外部滑动方向和内部滑动方向不一致考虑这样一种
- 1. F1弹出帮助文档先找个后缀是pdf、docx等格式的的帮助文档,将它放在项目的helpDoc下。设置其“复制到输出
- 在winform里拖入一个datagridview控件,跟一个openfiledialog控件using System;using Syst
- 好久没有写过博客了,最近因项目需求,需要用到Socket来进行通信,简单写了几个例子,记录一下,代码很简单,无非就是接收与发送,以及接收到数
- 前言Jetpack Compose(简称 Compose )是 Google 官方推出的基于 Kotlin 语言的 Android 新一代
- 本文实例为大家分享了Android实现底部滚轮式选择弹跳框的具体代码,供大家参考,具体内容如下先看效果:调用方法:SlideDialog s
- 在为什么阿里巴巴不建议在for循环中使用”+”进行字符串拼接一文中,我们介绍了几种Java中字符串拼接的方式,以及优缺点。其中还有一个重要的
- 在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载。所以自己写了一个压缩文件的工具类。该工具类支持单个文件和文件夹压缩。放代码: