Android學習Scroller(一)——View調用scrollTo()的理解及使用,scrollerscrollto
MainActivity如下:
package cc.uu;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.app.Activity;/** * Demo描述: * scrollTo()和scrollBy()的理解以及使用 * * 原理說明: * 1 其實View是沒有邊界,在螢幕上看到的只是View的一部分而已 * 2 scrollTo()和scrollBy()的本質一樣只是表現形式略有不同 * 與這兩個方法密切相關的兩個變數mScrollX和mScrollY在 * View的源碼中可以看到: * * //The offset,in pixels,by which the content of this view is scrolled horizontally. * protected int mScrollX; * * //The offset,in pixels,by which the content of this view is scrolled vertically. * protected int mScrollY; * * 通過文檔描述可知: * mScrollX和mScrollY表示:View的內容(content)相對於View本身在水平或垂直方向的位移量. * * scrollTo(int x, int y)方法: * 將一個視圖的內容移動到指定位置.此時位移量 mScrollX,mScrollY就分別等於x,y. * 預設情況下 mScrollX和mScrollY均為0 * * scrollBy(int x, int y)方法: * 在現有的基礎上繼續行動裝置檢視的內容. * 在該方法的源碼很簡單,也體現了這一點,如下: * public void scrollBy(int x, int y) { * scrollTo(mScrollX + x, mScrollY + y); * } * 預設情況下 mScrollX和mScrollY均為0 * * 再次強調和注意: * scrollTo()和scrollBy()移動的只是View的內容,但是View的背景是不移動的. * 為了體現這點,在該樣本中為View添加了背景色. * * 繼續上面問題的延伸: * 假如一個ViewGroup(比如XXXLayout)調用了scrollTo(By)()方法會怎樣? * 它的Content(即它所有的子View)都會移動,這點在下個例子中可以看到. * * * 3 scrollTo(int x,int y)和scrollBy(int x,int y)方法的座標說明 * 比如我們對於一個TextView調用scrollTo(0,25) * 那麼該TextView中的content(比如顯示的文字:Hello)會怎麼移動呢? * 向下移動25個單位?不,恰好相反. * 這是為什麼呢? * 因為調用這兩個方法會導致視圖重繪. * 即調用public void invalidate(int l, int t, int r, int b)方法. * 此處的l,t,r,b四個參數就表示View原來的座標. * 在該方法中最終會調用: * tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY); * p.invalidateChild(this, tmpr); * 其中tmpr是ViewParent,tmpr是Rect,this是原來的View. * 通過這兩行代碼就把View在一個Rect中重繪. * 請注意第一行代碼: * 原來的l和r均減去了scrollX * 原來的t和b均減去了scrollY * 就是說scrollX如果是正值,那麼重繪後的View的寬度反而減少了;反之同理 * 就是說scrollY如果是正值,那麼重繪後的View的高度反而減少了;反之同理 * 所以,TextView調用scrollTo(0,25)和我們的理解相反 * * scrollBy(int x,int y)方法與上類似,不再贅述. * * * 該樣本的說明,請參加下面的代碼注釋 * * * 參考資料: * 1 http://blog.csdn.net/wangjinyu501/article/details/32339379 * 2 http://blog.csdn.net/qinjuning/article/details/7247126 * Thank you very much * * * 備忘說明: * 使用scrollTo(By)()方法移動過程較快而且比較生硬. * 為了最佳化scrollTo(By)()的滑動過程可採用Scroller類. * 該類源碼第一句This class encapsulates scrolling. * 就指明了該類的目的:封裝了滑動過程. * 在後面的樣本中,將學習到Scroller的使用. * */public class MainActivity extends Activity { private TextView mTextView; private Button mLeftButton; private Button mRightButon;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init(){mTextView=(TextView) findViewById(R.id.textView);mLeftButton=(Button) findViewById(R.id.leftButton);mLeftButton.setOnClickListener(new ClickListenerImpl());mRightButon=(Button) findViewById(R.id.rightButton);mRightButon.setOnClickListener(new ClickListenerImpl());}/** * 樣本說明: * 1 每次點擊leftButton的時候 * 1.1 調用scrollBy()讓mTextView的內容(即文字)在原本位移的基礎上往左移30 * 1.2 調用scrollBy()讓mLeftButton的內容(即文字)在原本位移的基礎上也往左移30 * 2 每次點擊rightButton的時候 * 2.1 調用scrollTo()讓mTextView的內容(即文字)直接往右位移30,而不管以前的基礎(即 mScrollX和mScrollY) * 3 連續幾次點擊leftButton會看到mTextView的內容(即文字)每點一次都會往左移動30, * 然後再點擊一次rightButton會看到mTextView的內容(即文字)直接一次性到了往右30的位置,而 * 不是慢慢移動過去. * 這麼操作 * 1 很好的體現了這兩個方法的區別. * 2 直觀地看了scrollTo()方法的效用,它是不管以前的位移量的. * 4 在該例中也可以看到調用這兩個方法時,View的背景是沒有移動.移動的是內容. */private class ClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.leftButton://讓mTextView的內容往左移mTextView.scrollBy(30, 0);//讓mLeftButton的內容也往左移mLeftButton.scrollBy(20, 0);break;case R.id.rightButton://讓mTextView的內容往右移直接到-30的位置mTextView.scrollTo(-30, 0);break;default:break;}}}}
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_centerHorizontal="true" android:layout_marginTop="20dip" android:background="@android:color/darker_gray" /> <Button android:id="@+id/leftButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView移向螢幕的左邊" android:layout_centerHorizontal="true" android:layout_marginTop="150dip"/> <Button android:id="@+id/rightButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView移向螢幕的右邊" android:layout_centerHorizontal="true" android:layout_marginTop="250dip"/></RelativeLayout>
Android 上面一個scrollview,下面一個scrollview,拖動下面一個怎讓上面scrollview也同步滾動?
scrollto的方法放在scrollview內部的前端運行 也許能快些
android怎使用scrollview 實現輸入文本的滾動?
在建立Activity的時候設定為ScrollView布局,然後在Activity裡面添加你自己的TextView控制項