Scroller彈性滑動對象(—)

轉載請聲明:http://bbs.niuzhi.cc/thread-24-1-1.html


在Launcher中的Workspace中實現了左右屏幕切換效果,裏面就用到了Scroller記錄滑動軌跡,實現一種緩慢地向左或向右移動的效果,這裏我對這種效果進行總結:

我們先看一個例子:點擊按鈕時紅經塊會從左邊緩慢地移向左右,這個該怎麼實現呢

紅色塊在右邊 

我們先來看一下,Scroller,這個對象裏有startScroll方法 

 void Android.widget.Scroller.startScroll(int startX, int startY, int dx, int dy, int duration)
第一個參數是起始移動的x座標值,第二個是起始移動的y座標值,第三個第四個參數都是移到某點的座標值,而duration 當然就是執行移動的時間。這個有什麼用呢。要知道有什麼用還得再看一個方法

boolean android.widget.Scroller.computeScrollOffset()

當startScroll執行過程中即在duration時間內,computeScrollOffset  方法會一直返回true,但當動畫執行完成後會返回返加false


有了這兩個方法還不夠,我們還需要再重寫viewGroup的一個方法,

computeScroll 這個方法什麼時候會被調用呢

官網上這樣說的

public void computeScroll ()

Since: API Level 1

Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary. This will typically be done if the child is animating a scroll using a Scroller object.

當我們執行ontouch或invalidate()或postInvalidate()都會導致這個方法的執行

所以我們像下面這樣調用,postInvalidate執行後,會去調computeScroll 方法,而這個方法裏再去調postInvalidate,這樣就可以不斷地去調用scrollTo方法了,直到mScroller動畫結束,當然第一次時,我們需要手動去調用一次postInvalidate纔會去調用 

computeScroll 方法

[java] view plain copy
  1. @Override  
  2. public void computeScroll() {  
  3.     if (mScroller.computeScrollOffset()) {  
  4.         scrollTo(mScroller.getCurrX(), 0);  
  5.         postInvalidate();  
  6.     }  
  7. }  

下面附上上面那個例子的源代碼

首先是MyViewGroup.java

[java] view plain copy
  1. package com.wb;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.LinearLayout;  
  8. import android.widget.Scroller;  
  9.   
  10. public class MyViewGroup extends LinearLayout {  
  11.     private boolean s1=true;  
  12.     Scroller mScroller=null;  
  13.     public MyViewGroup(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         mScroller=new Scroller(context);  
  16.         // TODO Auto-generated constructor stub  
  17.     }  
  18.     @Override  
  19.     public void computeScroll() {  
  20.         if (mScroller.computeScrollOffset()) {  
  21.             scrollTo(mScroller.getCurrX(), 0);  
  22.             postInvalidate();  
  23.         }  
  24.     }  
  25.     public void beginScroll(){  
  26.         if (!s1) {  
  27.             mScroller.startScroll(00001000);  
  28.             s1 = true;  
  29.         } else {  
  30.             mScroller.startScroll(00, -50001000);  
  31.             s1 = false;  
  32.         }  
  33.         invalidate();  
  34.     }  
  35. }  

然後是WheelActivity.java


[java] view plain copy
  1. package com.wb;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.view.Gravity;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.AbsListView;  
  10. import android.widget.AbsListView.LayoutParams;  
  11. import android.widget.AbsListView.OnScrollListener;  
  12. import android.widget.Adapter;  
  13. import android.widget.BaseAdapter;  
  14. import android.widget.ListView;  
  15. import android.widget.TextView;  
  16.   
  17. public class WheelActivity extends Activity {  
  18.   
  19.     private ListView listView = null;  
  20.     private MyViewGroup myViewGroup;  
  21.   
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.         myViewGroup = (MyViewGroup) findViewById(R.id.myviewGroup);  
  27.   
  28.     }  
  29.   
  30.     public void scroll(View view) {  
  31.   
  32.         myViewGroup.beginScroll();  
  33.   
  34.     }  
  35.   
  36. }  
main.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="scroll"  
  11.         android:onClick="scroll" />  
  12.   
  13.     <com.wb.MyViewGroup  
  14.         xmlns:android="http://schemas.android.com/apk/res/android"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="fill_parent"  
  17.         android:orientation="vertical" android:id="@+id/myviewGroup">  
  18.   
  19.         <TextView  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="fill_parent"  
  22.             android:background="#ff0000"  
  23.             android:text="我在這"/>  
  24.   
  25.     
  26.     </com.wb.MyViewGroup>  
  27.   
  28. </LinearLayout>  
發佈了8 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章