安卓彈性滑動的幾種實現方式

2021-09-08 07:55:07 字數 1451 閱讀 1804

實現彈性滑動的原始碼如下

scroller scroller=new scroller(mcontext);

private void smoothscroolby(int destx, int desty)

@override

public void computescroll()

}

原理:

1.構造乙個scroller物件,並呼叫startscroll方法。scroller僅用來儲存引數,無實際作用

2.invalidate會導致view重繪,draw又會呼叫computescroll

3.computescroll在view中為空方法,所以需要自己實現

4.postinvalidate再次重繪。並且會再次呼叫computescroll

另外比較重要的還有computescrolloffset方法的原始碼

/**

* call this when you want to know the new location. if it returns true,

* the animation is not yet finished.

*/ public boolean computescrolloffset()

int timepassed = (int)(animationutils.currentanimationtimemillis() - mstarttime);

if (timepassed < mduration)

mcurrvelocity = velocitycoef * mdistance / mduration * 1000.0f;

mcurrx = mstartx + math.round(distancecoef * (mfinalx - mstartx));

// pin to mminx <= mcurrx <= mmaxx

mcurrx = math.min(mcurrx, mmaxx);

mcurrx = math.max(mcurrx, mminx);

mcurry = mstarty + math.round(distancecoef * (mfinaly - mstarty));

// pin to mminy <= mcurry <= mmaxy

mcurry = math.min(mcurry, mmaxy);

mcurry = math.max(mcurry, mminy);

if (mcurrx == mfinalx && mcurry == mfinaly)

break;}}

else

return true;

}

posted @

2018-10-18 17:34

藍冷然 閱讀(

...)

編輯收藏

安卓資料儲存的幾種方式

android系統一般提供了四種不同的資料儲存方式。分別是 1.sqlite sqlite是乙個輕量級的資料庫,支援基本sql語法,是常被採用的一種資料儲存方式。android為此資料庫提供了乙個名為sqlitedatabase的類,封裝了一些運算元據庫的api。2.sharedpreference...

安卓左右滑動的例項

這段時間一直在忙android的專案,總算抽出點時間休息一下,準備把一些專案用到的android經驗分享一下。在android開發過程中,經常會碰到activity之間的切換效果的問題,下面介紹一下如何實現左右滑動的切換效果,首先了解一下activity切換的實現,從android2.0開始在act...

安卓中的Activity的幾種跳轉方式

安卓中的activity的幾種跳 一 顯式呼叫方法 方法一 intent intent new intent 本類,將要跳轉的類 startactivity intent 方法二 intent intent2 new intent intent2.setclass 本類,另外將要跳轉的類 inten...