付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】android中怎么实现ScrollView滚动改变标题栏透明度
本篇文章给大家分享的是有关android中怎么实现ScrollView滚动改变标题栏透明度,在下觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着在下一起来看看吧。实现思路:1、先获取顶部图片的高度height,这个有3种方式获取,我用的是监听onGlobalLayout方法的回调2、监听scrollview的滚动坐标,原生的没有这个监听,需要我们自己写个view继承scrollview,然后重写onScrollChanged()方法,创建一个监听,在这个方法里面回调3、根据图片高度height和滚动的纵坐标y进行判断,算出比例,透明度范围0~255,根据比例设置改变的透明度,当y>height是不做改变实现代码:import
com.example.d_changealphabyscroll.widget.ObservableScrollView;
import
com.example.d_changealphabyscroll.widget.ObservableScrollView.ScrollViewListener;
import
android.app.Activity;
import
android.graphics.Color;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.view.View;
import
android.view.ViewTreeObserver;
import
android.view.ViewTreeObserver.OnGlobalLayoutListener;
import
android.webkit.WebSettings;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
import
android.widget.ImageView;
import
android.widget.ScrollView;
public
class
MainActivity
extends
Activity
implements
ScrollViewListener{
private
View
layoutHead;
private
ObservableScrollView
scrollView;
private
ImageView
imageView;
private
WebView
webView;
private
int
height
;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private
void
initView()
{
webView
=
(WebView)
findViewById(R.id.webview1);
scrollView
=
(ObservableScrollView)
findViewById(R.id.scrollview);
layoutHead
=
findViewById(R.id.view_head);
imageView
=
(ImageView)
findViewById(R.id.imageView1);
layoutHead.setBackgroundColor(Color.argb(0,
0xfd,
0x91,
0x5b));
//初始化webview
//启用支持javascript
WebSettings
settings
=
webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.loadUrl("http://www.topit.me/");
//覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
webView.setWebViewClient(new
WebViewClient(){
@Override
public
boolean
shouldOverrideUrlLoading(WebView
view,
String
url)
{
//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
view.loadUrl(url);
return
true;
}
});
//获取顶部图片高度后,设置滚动监听
ViewTreeObserver
vto
=
imageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new
OnGlobalLayoutListener()
{
@Override
public
void
onGlobalLayout()
{
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
height
=
imageView.getHeight();
imageView.getWidth();
scrollView.setScrollViewListener(MainActivity.this);
}
});
}
@Override
public
void
onScrollChanged(ObservableScrollView
scrollView,
int
x,
int
y,
int
oldx,
int
oldy)
{
//
Log.i("TAG","y>"+y+"
height-->"+height);
if(y<=height){
float
scale
=(float)
y
/height;
float
alpha
=
(255
*
scale);
//
Log.i("TAG","alpha>"+alpha);
//layout全部透明
//
layoutHead.setAlpha(scale);
//只是layout背景透明(仿知乎滑动效果)
layoutHead.setBackgroundColor(Color.argb((int)
alpha,
0xfd,
0x91,
0x5b));
}
}
}带滚动监听的scrollviewimport
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
voi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 绿色改装维修船舶项目规划选址论证报告
- 一次性医用耗材生产项目可行性研究报告
- 抹茶粉及衍生产品项目环境影响报告书
- 2026年起重信号司索工(建筑特殊工种)考试试题题库及答案
- 老旧小区运维服务运营方案
- 2026年通信系统分析与维护通信工程师考试模拟试卷及答案
- 阀门井砌筑施工方案
- 小学三年级科学上册青岛版《叶的蒸腾作用》教学设计
- 2026年渗透检测二级人员考核题库(含答案)
- 2026年智慧景区系统管理员题库(含答案)
- 安全伴我行-大学生安全教育智慧树知到期末考试答案章节答案2024年哈尔滨工程大学
- 个人年度医德医风档案模版范文
- JT-T-961-2020交通运输行业反恐怖防范基本要求
- 水利工程安全技术措施和专项施工方案编制导则
- 支气管哮喘病例讨论课件
- 2023-2024学年广西百色市高二(上)期末数学试卷(含解析)
- 南沙国际仲裁中心仲裁通则
- 资产评估学教程(第八版)习题及答案 乔志敏
- 兽医传染病学:衣原体病
- GB/T 25854-2010一般起重用D形和弓形锻造卸扣
- 半条被子(红军长征时期故事) PPT
评论
0/150
提交评论