av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Android Scroller的使用方法

瀏覽:6日期:2022-09-18 16:14:05

本文實例為大家分享了Android Scroller的使用方法,供大家參考,具體內容如下

1、scrollTo和ScrollBy

View類定義了兩個用于滾動View內容的方法:scrollTo和scrollBy:

/** * Set the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the x position to scroll to * @param y the y position to scroll to */public void scrollTo(int x, int y) { if (mScrollX != x || mScrollY != y) {int oldX = mScrollX;int oldY = mScrollY;mScrollX = x;mScrollY = y;invalidateParentCaches();onScrollChanged(mScrollX, mScrollY, oldX, oldY);if (!awakenScrollBars()) { postInvalidateOnAnimation();} }}/** * Move the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the amount of pixels to scroll by horizontally * @param y the amount of pixels to scroll by vertically */public void scrollBy(int x, int y) { scrollTo(mScrollX + x, mScrollY + y);}

可以看到scrollBy傳入的x和y參數實際上是X方向和Y方向的滾動距離的增量,最終還是調用了scrollTo方法。而scrollTo方法中做了一些刷新和通知操作,最重要的是對mScrollX和mScrollY進行了賦值。

在View的draw方法中,我們可以看到如下代碼:

int sx = 0;int sy = 0;if (!drawingWithRenderNode) { computeScroll(); sx = mScrollX; sy = mScrollY;}...if (offsetForScroll) { canvas.translate(mLeft - sx, mTop - sy);}

也就是說,mScrollX和mScrollY最終是用在了內容繪制的地方,其mLeft和mTop本身都沒有因為scrollTo發生變化。scrollTo作用在View的內容上,而不是View本身。

2、 computeScroll

在上面的View的draw方法的節選中我們看到在對mScrollX和mScrollY取值之前,調用了computeScroll方法。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 {@link android.widget.Scroller Scroller} * object. */public void computeScroll() {}

根據注釋,computeScroll的典型用法是與Scroller結合使用實現內容/字節點的滾動動畫。

3、Scroller的使用

Scroller事實上并不直接操作View的滾動,而是根據設置來計算當前X和Y方向的距離。Scroller的一般使用步驟:

1、初始化Scroller,可以指定插值器,不指定則使用默認的ViscousFluidInterpolator2、調用Scroller#startScroll方法,開始在一段時間內不斷計算X和Y方向的滾動3、通知View刷新4、在View#computeScroll中通過scrollTo實現真正的滾動操作5、通知View刷新

其中在滾動執行完成之前4和5會不斷地循環,直至scroller.computeScrollOffset()返回false。

class ScrollableLinearLayout @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : LinearLayout(context, attrs, defStyleAttr) { private val scroller = Scroller(context, BounceInterpolator()) override fun computeScroll() {if(scroller.computeScrollOffset()) { // 真正實現滾動操作的地方 scrollTo(scroller.currX, scroller.currY) // 刷新 invalidate()} } fun scroll() {// 調用Scroller的startScrollif(scrollX == 0) { scroller.startScroll(scrollX, scrollY, /*dx*/ -500, /*dy*/ 0, /*duration*/ 300)} else { scroller.startScroll(scrollX, scrollY, 500, 0, 300)}// 刷新invalidate() }}

xml布局:

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' tools:context='.scroller.activity.ScrollerSampleActivity'> <com.sahooz.customviewdemo.scroller.view.ScrollableLinearLayoutandroid: android:layout_width='match_parent'android:layout_height='200dp'android:gravity='center_vertical'android:orientation='vertical'android:background='#FFAAAA'><Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='Scroll' /> </com.sahooz.customviewdemo.scroller.view.ScrollableLinearLayout></LinearLayout>

Activity

class ScrollerSampleActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_scroller_sample)val btnScroll = findViewById<Button>(R.id.btnScroll)btnScroll.setOnClickListener { findViewById<ScrollableLinearLayout>(R.id.sll).scroll()} }}

運行結果:

Android Scroller的使用方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 成人免费看 | 国产精品一区在线 | 狠狠操天天操 | 久久精品99久久久久久 | 天天干天天操天天干 | 中文字幕一区二区三区四区视频 | 日韩精品久久 | 欧美色影院 | 国产精品久久久久久无人区 | 免费在线观看黄色片 | 久久久久久免费 | 欧美精品在线视频 | 国产成人一区二区三区 | 日本福利视频 | 久久久午夜精品 | 成人亚洲视频 | 欧美日韩综合 | 欧美日韩一区在线 | av中文在线 | 天天干夜夜撸 | 免费成人在线看 | 国产精品久久久久久99 | 亚洲一区二区 | 中文字幕视频 | 日韩精品视频免费在线观看 | 九九影视理伦片 | 日韩精品久久久久 | 日韩精品一区二区在线 | 国产精品久久一区 | 激情久久av | 不卡免费视频 | 免费av网址在线观看 | 久久a级片 | 亚洲免费观看视频 | 色涩av| 一区两区小视频 | 国产在线欧美 | 国产a毛片 | 中文字幕网址在线 | 国产综合视频 | 日韩精品极品 |