轉載請聲明: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 當然就是執行移動的時間。這個有什麼用呢。要知道有什麼用還得再看一個方法
有了這兩個方法還不夠,我們還需要再重寫viewGroup的一個方法,
computeScroll 這個方法什麼時候會被調用呢
官網上這樣說的
public void computeScroll ()
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.
所以我們像下面這樣調用,postInvalidate執行後,會去調computeScroll 方法,而這個方法裏再去調postInvalidate,這樣就可以不斷地去調用scrollTo方法了,直到mScroller動畫結束,當然第一次時,我們需要手動去調用一次postInvalidate纔會去調用
computeScroll 方法
- @Override
- public void computeScroll() {
- if (mScroller.computeScrollOffset()) {
- scrollTo(mScroller.getCurrX(), 0);
- postInvalidate();
- }
- }
下面附上上面那個例子的源代碼
首先是MyViewGroup.java
- package com.wb;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
- import android.widget.Scroller;
- public class MyViewGroup extends LinearLayout {
- private boolean s1=true;
- Scroller mScroller=null;
- public MyViewGroup(Context context, AttributeSet attrs) {
- super(context, attrs);
- mScroller=new Scroller(context);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void computeScroll() {
- if (mScroller.computeScrollOffset()) {
- scrollTo(mScroller.getCurrX(), 0);
- postInvalidate();
- }
- }
- public void beginScroll(){
- if (!s1) {
- mScroller.startScroll(0, 0, 0, 0, 1000);
- s1 = true;
- } else {
- mScroller.startScroll(0, 0, -500, 0, 1000);
- s1 = false;
- }
- invalidate();
- }
- }
然後是WheelActivity.java
- package com.wb;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.AbsListView.LayoutParams;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.Adapter;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- public class WheelActivity extends Activity {
- private ListView listView = null;
- private MyViewGroup myViewGroup;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myViewGroup = (MyViewGroup) findViewById(R.id.myviewGroup);
- }
- public void scroll(View view) {
- myViewGroup.beginScroll();
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="scroll"
- android:onClick="scroll" />
- <com.wb.MyViewGroup
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" android:id="@+id/myviewGroup">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:background="#ff0000"
- android:text="我在這"/>
- </com.wb.MyViewGroup>
- </LinearLayout>