android 防腾讯新闻标题栏,解析ScrollView--仿QQ空间标题栏渐变

本文介绍了Android实现仿腾讯新闻标题栏效果的方法。通过XML布局设置界面元素,创建自定义的GradationScrollView类监听滚动事件。获取顶部图片高度并设置滚动监听,根据滚动距离改变标题栏颜色透明度和字体颜色透明度,实现渐变效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看一下效果图:

270363e45af26d47b001fc3edcd86cd3.gif

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.hankkin.gradationtitlebar.QQSpeakActivity">

android:id="@+id/scrollview"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scrollbars="none">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/iv_banner"

android:scaleType="fitXY"

android:src="@drawable/banner3"

android:layout_width="match_parent"

android:layout_height="200dp" />

android:id="@+id/listview"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:paddingBottom="10dp"

android:id="@+id/textview"

android:layout_width="match_parent"

android:layout_height="55dp"

android:gravity="center|bottom"

android:text="我是标题"

android:textSize="18sp"

android:textColor="@color/transparent"

android:background="#00000000" />

public class GradationScrollView extends ScrollView {

public interface ScrollViewListener {

void onScrollChanged(GradationScrollView scrollView, int x, int y,

int oldx, int oldy);

}

private ScrollViewListener scrollViewListener = null;

public GradationScrollView(Context context) {

super(context);

}

public GradationScrollView(Context context, AttributeSet attrs,

int defStyle) {

super(context, attrs, defStyle);

}

public GradationScrollView(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);

}

}

}

我们需要获取图片的高度,并且设置滚动监听,随着滚动的距离来设置标题栏的颜色透明度和字体颜色的透明度

/**

* 获取顶部图片高度后,设置滚动监听

*/

private void initListeners() {

ViewTreeObserver vto = ivBanner.getViewTreeObserver();

vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

textView.getViewTreeObserver().removeGlobalOnLayoutListener(

this);

height = ivBanner.getHeight();

scrollView.setScrollViewListener(QQSpeakActivity.this);

}

});

}

/**

* 滑动监听

* @param scrollView

* @param x

* @param y

* @param oldx

* @param oldy

*/

@Override

public void onScrollChanged(GradationScrollView scrollView, int x, int y,

int oldx, int oldy) {

// TODO Auto-generated method stub

if (y <= 0) { //设置标题的背景颜色

textView.setBackgroundColor(Color.argb((int) 0, 144,151,166));

} else if (y > 0 && y <= height) { //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变

float scale = (float) y / height;

float alpha = (255 * scale);

textView.setTextColor(Color.argb((int) alpha, 255,255,255));

textView.setBackgroundColor(Color.argb((int) alpha, 144,151,166));

} else { //滑动到banner下面设置普通颜色

textView.setBackgroundColor(Color.argb((int) 255, 144,151,166));

}

}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持找一找教程网!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值