Android Scroll實現(xiàn)彈性滑動_列表下拉彈性滑動的示例代碼
我這一次講使用scroll實現(xiàn)彈性滑動,我不會只有一個例子就說完,因為寫文章的時候我也在學習,我分幾次講完吧。
首先上一段代碼,
private void smoothScrollByScroller(int dy){
mScroller.startScroll(0,dy,0,dy*-1,1000);
invalidate();
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
這段代碼是實現(xiàn)彈性滑動的核心,第一個函數(shù)指的是緩慢滑動的意思,但是卻沒有這個滑動的實際功能。
startScroll這函數(shù)的五個參數(shù)指的是起點x坐標,起點y坐標,x位移量,y位移量,這段滑動的時間。這個函數(shù)的內(nèi)部是不斷計算在滑動時間里x和y坐標應該是什么值,然后因為invalidate會調(diào)用computeScroll,這個computeScrollOffset函數(shù)是判斷當前滑動是否結束,如果沒有結束通過getCurrX和getCurry獲得startScroll函數(shù)計算的值,在使用scrollTo滑動相應的位置,因為startScroll會運算很多次,也就是將滑動時間分成很多段,相應的坐標也都算出來,跟著給scrollTo去實現(xiàn)滑動。
這很像是ValueAmition,將時間分成很多段,然后計算相應的值,同時分很多次去實現(xiàn)。
我貼一個類似QQ消息列表的常見的彈性滑動,這里下拉是沒有刷新的,
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public final class PullView extends ViewGroup {
private int mLastY;
private Context mContext;
private Scroller mScroller;
//子View的個數(shù)
private int mChildCount;
public PullView(Context context){
this(context,null);
}
public PullView(Context context, AttributeSet attributeSet){
super(context,attributeSet);
mContext=context;
initView();
}
private void initView(){
mScroller=new Scroller(mContext);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int y=(int)event.getY();
switch (event.getAction()){
//手指按下時,初始化按下位置的X,Y位置值
case MotionEvent.ACTION_DOWN:
mLastY=y;
break;
//計算滑動的偏移量,產(chǎn)生滑動效果
case MotionEvent.ACTION_MOVE:
//手指向下滑動delayY>0,向上滑動delayY<0
int delayY=y-mLastY;
delayY=delayY*-1;
scrollBy(0,delayY);
break;
case MotionEvent.ACTION_UP:
/**
* scrollY是指:View的上邊緣和View內(nèi)容的上邊緣(其實就是第一個ChildView的上邊緣)的距離
* scrollY=上邊緣-View內(nèi)容上邊緣,scrollTo/By方法滑動的知識View的內(nèi)容
* 往下滑動scrollY是負值
*/
int scrollY=getScrollY();
smoothScrollByScroller(scrollY);
break;
}
mLastY=y;
return true;
}
/**
* 執(zhí)行滑動效果
* 使用scroller實現(xiàn)
* @param dy
*/
private void smoothScrollByScroller(int dy){
mScroller.startScroll(0,dy,0,dy*-1,1000);
invalidate();
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
/**
* 重新計算子View的高度和寬度
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measuredWidth;
int measureHeight;
mChildCount = getChildCount();
//測量子View
measureChildren(widthMeasureSpec, heightMeasureSpec);
int widthSpaceSize = MeasureSpec.getSize(widthMeasureSpec);
int widthSpaceMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpaceSize = MeasureSpec.getSize(heightMeasureSpec);
int heightSpaceMode = MeasureSpec.getMode(heightMeasureSpec);
//獲取橫向的padding值
int paddingLeft=getPaddingLeft();
int paddingRight=getPaddingRight();
final View childView = getChildAt(0);
/**
* 如果子View的數(shù)量是0,就讀取LayoutParams中數(shù)據(jù)
* 否則就對子View進行測量
* 此處主要是針對wrap_content這種模式進行處理,因為默認情況下
* wrap_content等于match_parent
*/
if (mChildCount == 0) {
ViewGroup.LayoutParams layoutParams=getLayoutParams();
if(layoutParams!=null){
setMeasuredDimension(layoutParams.width,layoutParams.height);
}else {
setMeasuredDimension(0, 0);
}
} else if (heightSpaceMode == MeasureSpec.AT_MOST && widthSpaceMode == MeasureSpec.AT_MOST) {
measuredWidth = childView.getMeasuredWidth() * mChildCount;
measureHeight = getChildMaxHeight();
//將兩側的padding值加上去
measuredWidth=paddingLeft+measuredWidth+paddingRight;
setMeasuredDimension(measuredWidth, measureHeight);
} else if (heightSpaceMode == MeasureSpec.AT_MOST) {
measureHeight = getChildMaxHeight();
setMeasuredDimension(widthSpaceSize, measureHeight);
} else if (widthSpaceMode == MeasureSpec.AT_MOST) {
measuredWidth = childView.getMeasuredWidth() * mChildCount;
measuredWidth=paddingLeft+measuredWidth+paddingRight;
setMeasuredDimension(measuredWidth, heightSpaceSize);
}
}
/**
* 獲取子View中最大高度
* @return
*/
private int getChildMaxHeight(){
int maxHeight=0;
for (int i = 0; i < mChildCount; i++) {
View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
int height = childView.getMeasuredHeight();
if(height>maxHeight){
maxHeight=height;
}
}
}
return maxHeight;
}
/**
* 設置子View的布局
* @param changed
* @param l
* @param t
* @param r
* @param b
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childLeft = 0;
for (int i = 0; i < mChildCount; i++) {
View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
int childWidth = childView.getMeasuredWidth();
childView.layout(childLeft, 0, childLeft + childWidth, childView.getMeasuredHeight());
childLeft += childWidth;
}
}
}
}
<android.com.listfragment.PullView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1500dp"
android:background="#806363"></LinearLayout>
</android.com.listfragment.PullView>
這里的ViewGroup的繪畫和測量我就不多說,我就說一下它獲取函數(shù),計算坐標的一些事。
它在手指按下時記錄y坐標,在手指移動時,跟著移動子View,在手指抬起時,使用彈性滑動的函數(shù)smoothScrollByScroller。
大家會發(fā)現(xiàn)為什么一些計算出的坐標要加負號,因為在我們?nèi)搜劾铮覀兿吕瓂坐標的位移量是正的,但是在系統(tǒng)認為這個值是負的,原因我太菜不知道,知道的求大神評論留言告訴。
下一次寫一個隨手指彈性滑動的例子。
以上這篇Android Scroll實現(xiàn)彈性滑動_列表下拉彈性滑動的示例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
從零開始使用gradle配置即可執(zhí)行的Hook庫詳解
這篇文章主要為大家介紹了從零開始使用gradle配置即可執(zhí)行的Hook庫詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Android開發(fā)系列二之窗口Activity的生命周期
這篇文章主要介紹了Android學習系列二之窗口Activity的生命周期的相關資料,需要的朋友可以參考下2016-05-05
Android解析JSON格式數(shù)據(jù)的兩種方式(JSONObject和Gson)
json數(shù)據(jù)的解析相對而言,還是比較容易的,實現(xiàn)的代碼也十分簡單,下面這篇文章主要給大家介紹了關于Android解析JSON格式數(shù)據(jù)的兩種方式,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07
Android自定義View實現(xiàn)shape圖形繪制
這篇文章主要為大家詳細介紹了Android使用自定義View實現(xiàn)shape圖形繪制,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

