zoukankan      html  css  js  c++  java
  • android中随着ScrollView的滑动,titleBar状态的改变

    今天项目有一个需求,,类是于QQ空间里面的一个功能,于是就研究了一下,嗯,说这么多,可能还有人不知道指的是那个,直接上效果图。见谅,不会弄动态图:

     

    对,就是这种效果,我研究了一下,思路如下:
    1.监听ScrollView的滑动
    2.通过判断滑动的距离,然后给titleBar设置相应的颜色渐变
     
    但是后来我发现我找不到ScrollView的监听器,也就是说,ScrollView没有对外提供相应的监听接口,不提供就算了嘛,我我自己提供于是就出现了下面这段代码:
     
    package com.example.myscrollview;
     
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.ScrollView;
     
    public class MyScrollView extends ScrollView {
        private ScrollViewListener scrollViewListener = null;
     
        public MyScrollView(Context context) {
            super(context);
        }
     
        public MyScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
     
        @Override
        protected void onScrollChanged(int x, int y, int oldx, int oldy) {
            // x为当前滑动条的横坐标,y表示当前滑动条的纵坐标,oldx为前一次滑动的横坐标,oldy表示前一次滑动的纵坐标
            super.onScrollChanged(x, y, oldx, oldy);
            if (scrollViewListener != null) {
                // 在这里将方法暴露出去
                scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
            }
        }
     
        // 是否要其弹性滑动
        @Override
        protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
                int scrollY, int scrollRangeX, int scrollRangeY,
                int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
     
            // 弹性滑动关键则是maxOverScrollX, 以及maxOverScrollY,
            // 一般默认值都是0,需要弹性时,更改其值即可
            // 即就是,为零则不会发生弹性,不为零(>0,负数未测试)则会滑动到其值的位置
            return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,
                    scrollRangeX, scrollRangeY, 0, 0, isTouchEvent);
        }
     
        // 接口
        public interface ScrollViewListener {
     
            void onScrollChanged(View scrollView, int x, int y, int oldx, int oldy);
     
        }
     
        public void setScrollViewListener(ScrollViewListener listener) {
            scrollViewListener = listener;
        }
    }
     
     
    自定义完成之后,就可以在MainActivity中进行获取了:
     
    package com.example.myscrollview;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.RelativeLayout;
     
    import com.example.myscrollview.MyScrollView.ScrollViewListener;
     
    public class MainActivity extends Activity {
        MyScrollView scroll;
        RelativeLayout title;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            scroll = (MyScrollView) findViewById(R.id.scroll);
            title = (RelativeLayout) findViewById(R.id.title);
     
            // 实现自定义控件中的监听器
            scroll.setScrollViewListener(new ScrollViewListener() {
     
                @Override
                public void onScrollChanged(View scrollView, int x, int y,
                        int oldx, int oldy) {
                    // TODO Auto-generated method stub
                    titleAnima(y);
                }
     
            });
        }
     
        /**
         * 出现渐变效果
         */
        public void titleAnima(int y) {
            int scrollHeight = scroll.getChildAt(0).getHeight()
                    - scroll.getHeight();
            float scrollPercent = (float) y / scrollHeight;
            title.getBackground().setAlpha((int) (255 * scrollPercent));
     
            // 如果有文字的话,还可以设置文字的颜色渐变
            // int color = topText.getTextColors().getDefaultColor();
            // int r = Color.red(color);
            // int g = Color.green(color);
            // int b = Color.blue(color);
            // int changeToColor = Color.argb((int) (255 * (1 - scrollPercent)), r,
            // g, b);
            // topText.setTextColor(changeToColor);
        }
     
    }
     
     
    我的布局文件activity_main.xml是这样的:
     
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
     
        <RelativeLayout
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentTop="true"
            android:background="@android:color/holo_red_dark" >
     
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="标题" />
        </RelativeLayout>
     
        <com.example.myscrollview.MyScrollView
            android:id="@+id/scroll"
            android:layout_below="@id/title"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
     
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
     
                <LinearLayout
                    android:id="@+id/lin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
     
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#fff"
                        android:padding="16dp"
                        android:text="hh阿迪和法国hius大hiugfhsdiuhgudhsiuahiudshfuihdsiuhhfsdkjhuiagfhsduiah加快速度哈更何况江苏打工记得书法家挥洒加快速度奋斗哈反抗集散地户籍卡hh"
                        android:textSize="40sp" />
     
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#fff"
                        android:padding="16dp"
                        android:text="hh阿迪和法国hius大hiugfhsdiuhgudhsiuahiudshfuihdsiuhhfsdkjhuiagfhsduiah加快速度哈更何况江苏打工记得书法家挥洒加快速度奋斗哈反抗集散地户籍卡hh"
                        android:textSize="40sp" />
     
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#fff"
                        android:padding="16dp"
                        android:text="hh阿迪和法国hius大hiugfhsdiuhgudhsiuahiudshfuihdsiuhhfsdkjhuiagfhsduiah加快速度哈更何况江苏打工记得书法家挥洒加快速度奋斗哈反抗集散地户籍卡hh"
                        android:textSize="40sp" />
     
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#fff"
                        android:padding="16dp"
                        android:text="hh阿迪和法国hius大hiugfhsdiuhgudhsiuahiudshfuihdsiuhhfsdkjhuiagfhsduiah加快速度哈更何况江苏打工记得书法家挥洒加快速度奋斗哈反抗集散地户籍卡hh"
                        android:textSize="40sp" />
                </LinearLayout>
            </RelativeLayout>
        </com.example.myscrollview.MyScrollView>
     
    </RelativeLayout>
     
     
    是不是很简单,不用谢,请叫我雷锋。
     
    Android交流面试群:684619033 ----- 注意:转载请注明出处:https://www.cnblogs.com/huangjialin/
  • 相关阅读:
    Appium学习笔记(一)--安装与配置
    google软件测试之道--读后笔记
    Web测试中常见分享问题
    730工作总结
    Java源码分析系列
    LeetCode 21 -- Merge Two Sorted Lists
    LeetCode 20 -- Valid Parentheses
    LeetCode 15 -- 3Sum
    LeetCode 11 -- Container With Most Water
    ASP.NET Core MVC中Hangfire及EF Core的简单使用
  • 原文地址:https://www.cnblogs.com/huangjialin/p/5613413.html
Copyright © 2011-2022 走看看