栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

解析ScrollView--仿QQ空间标题栏渐变

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解析ScrollView--仿QQ空间标题栏渐变

先看一下效果图:




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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158832.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号