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>
 
 
是不是很簡單,不用謝,請叫我雷鋒。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM