標籤:
今天項目有一個需求,,類是於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中隨著ScrollView的滑動,titleBar狀態的改變