android仿知乎标题栏随ScrollView滚动变色

本文实例为大家分享了android标题栏随ScrollView滚动变色的具体代码,供大家参考,具体内容如下

参考:Android之scrollview滑动使标题栏渐变背景色的实例代码

效果图:

核心类:ObservableScrollView

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) {
    super(context, attrs);
  }

  public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

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

}

MyListview

解决ScrollView嵌套Listview的滑动冲突:

public class MyListview extends ListView {

  public MyListview(Context context) {
    super(context);
  }

  public MyListview(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public MyListview(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
  }
}

MainActivity

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

  /**
   * ScrollView滚动监听
   *
   * @param scrollView:滚动控件
   * @param x:x轴坐标
   * @param y:y轴坐标
   * @param oldx:上一个x轴坐标
   * @param oldy:上一个y轴坐标
   */
  @Override
  public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
    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));
    }
  }
}

布局:

<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">

  <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/ic_launcher" />

      <com.jukopro.titlebarcolor.MyListview
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>
  </com.jukopro.titlebarcolor.ObservableScrollView>

  <TextView
    android:id="@+id/textview"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#00000000"
    android:gravity="center"
    android:text="我是标题"
    android:textColor="@android:color/white"
    android:textSize="18sp" />

</RelativeLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • android仿知乎ScrollView滚动改变标题栏透明度

    刷知乎的时候看到,专题栏里面 往下滚动标题栏会由透明逐渐变蓝色,觉得这个效果不错,就想自己写一下 这是自己实现的效果图: 说下实现思路: 1.先获取顶部图片的高度height,这个有3种方式获取,我用的是监听onGlobalLayout方法的回调 2.监听scrollview的滚动坐标,原生的没有这个监听,需要我们自己写个view继承scrollview,然后重写onScrollChanged()方法,创建一个监听,在这个方法里面回调 3.根据图片高度height和滚动的纵坐标y进行判断,算出比

  • Android 使用CoordinatorLayout实现滚动标题栏效果的实例

    在Material Design里,CoordinatorLayout通常用来作为顶层视图,来协调处理各个子View之间的动作,从而实现各种动画效果,如Snackbar与FloatingActionButton的配合显示效果,就是以CoordinatorLayout作为根布局来实现的 CoordinatorLayout提供Behaviors接口,子View通过实现Behaviors接口来协调和其它View之间的显示效果,可以这么理解: CoordinatorLayout让其子View之间互相知道

  • Android Support Library 标题栏(Toolbar)滚动效果实现方法

    首先来个效果图  布局文件代码 在布局文件中,CoordinatorLayout作为布局文件根节点,AppBarLayout包含在CoordinatorLayout中,toolbar在AppBarLayout节点下include进来. <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="

  • android仿知乎标题栏随ScrollView滚动变色

    本文实例为大家分享了android标题栏随ScrollView滚动变色的具体代码,供大家参考,具体内容如下 参考:Android之scrollview滑动使标题栏渐变背景色的实例代码 效果图: 核心类:ObservableScrollView package com.jukopro.titlebarcolor; import android.content.Context; import android.util.AttributeSet; import android.widget.Scrol

  • Android仿知乎悬浮功能按钮FloatingActionButton效果

    前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,所以就来实践实践.效果基本出来了,大家可以自己去完善. 首先看一下效果图: 我们看到点击FloatingActionButton后会展开一些item,然后会有一个蒙板效果,这都是这个View的功能.那么这整个View肯定是个ViewGroup,我们一部分一部分来看. 首先是这个最小的Tag: 这个Tag带文字,可以是一个TextView,但为了美观,我们使用CardView,CardView是一个FrameLayout,我们要让它具有显

  • Android仿知乎客户端关注和取消关注的按钮点击特效实现思路详解

    先说明一下,项目代码已上传至github,不想看长篇大论的也可以先去下代码,对照代码,哪里不懂点哪里. 代码在这https://github.com/zgzczzw/ZHFollowButton 前几天发现知乎关注的点击效果确实赞,查了一下实现方式,刚好看到这个问题,花了一天时间终于把这个效果实现了,现在来回答一下,很不幸,楼上各位的答案都不全对,且听我一一道来. 首先,我先详细观察了一些知乎的效果,其中有一个很神奇的地方,如图: 注意看第二张图,这个圆形在扩散的时候,圆形底下的字还在,而且新的

  • Android仿知乎日报开屏页效果

    先看看知乎日报开屏页的效果,非常漂亮的开屏效果 ezgif.com-resize (2).gif 然后我来一个 ezgif.com-resize (1).gif 也不错~感觉可以以假乱真了~ 很简单,直接开始. 实现这个效果先制定个三步走策略 底部布局上滑展示. 画一个知弧. 显示图片 底部布局上滑展示 直接上代码吧,属性动画基本使用 private void startAnimation() { //位移动画,从底部滑出,Y方向移动,mHeight是底部布局的高度 ObjectAnimator

  • Android仿微博加载长图滚动查看效果

    本文实例为大家分享了Android加载长图片的具体代码,供大家参考,具体内容如下 解决步骤 1.将图片缩放到与控件等宽 2.判断缩放后的图片高度,如果高度大于控件高度较多(这里设置的是1.5倍),认定为长图,可滑动查看图片                      |-如果高度小于控件高度的1.5倍,以控件高度为基准,重新缩放图片 package org.wandcf_ces.fairproject.widgets; import android.annotation.TargetApi; im

  • Android ScrollView滑动实现仿QQ空间标题栏渐变

    今天来研究的是ScrollView-滚动视图,滚动视图又分横向滚动视图(HorizontalScrollView)和纵向滚动视图(ScrollView),今天主要研究纵向的.相信大家在开发中经常用到,ScrollView的功能已经很强大了,但是仍然满足不了我们脑洞大开的UI设计师们,所以我们要自定义-本篇文章主要讲监听ScrollView的滑动实现仿QQ空间标题栏渐变,先看一下效果图: 好了我们切入主题. 有可能你不知道的那些ScrollView属性  •android:scrollbars 设

  • Android仿淘宝商品拖动查看详情及标题栏渐变功能

    绪论 最近一直比较忙,也没抽出时间来写博客,也不得不说是自己犯了懒癌,人要是一懒就什么事都不想做了,如果不能坚持下来的话,那么估计就废了,��.最近自己攒了好多东西,接下来的时间我会慢慢都分享出来的.好了废话不多说了,下面我们开始正题: 今天要分享的是淘宝的详情页,之前在淘宝上买东西的时候看到淘宝的详情页效果比较不错,所以今天就来仿一下它的效果吧,可能没有淘宝的好,希望见谅啊. 先上效果图: 这是淘宝的: 我自己做的: 怎么样效果还差不多吧?GIF图效果看的不太清楚,见谅. 下面我们来看看怎么实

  • Android实现美团、大众点评的购买悬浮效果(ScrollView滚动监听)

    随着移动互联网的快速发展,它已经和我们的生活息息相关了,在公交地铁里面都能看到很多人的人低头看着自己的手机屏幕,从此"低头族"一词就产生了,作为一名移动行业的开发人员,我自己也是一名"低头族",上下班时间在公交地铁上看看新闻来打发下时间,有时候也会看看那些受欢迎的App的一些界面效果,为什么人家的app那么受欢迎?跟用户体验跟UI设计也有直接的关系,最近在美团和大众点评的App看到如下效果,我感觉用户好,很人性化,所以自己也尝试着实现了下,接下来就讲解下实现思路!

  • Android仿UC浏览器左右上下滚动功能

    本文要解决在侧滑菜单右边加个文本框,并能实现文本的上下滑动和菜单的左右滚动.这里推荐可以好好看看android的触摸事件的分发机制,这里我就不详细讲了,我只讲讲这个应用.要实现的功能就像UC浏览器(或其它手机浏览器)的左右滚动,切换网页,上下滚动,拖动内容. 本文的效果: 一.功能要求与实现 1.功能要求: (1)手指一开始按着屏幕左右移动时,只能左右滚动菜单,如果这时手指一直按着,而且上下移动了,那么菜单显示部分保持不变,但文本框也不上下移动!                       (2

随机推荐