最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Scroll實(shí)現(xiàn)彈性滑動_列表下拉彈性滑動的示例代碼

 更新時間:2018年01月20日 10:58:38   作者:鍵盤舞者113  
下面小編就為大家分享一篇Android Scroll實(shí)現(xiàn)彈性滑動_列表下拉彈性滑動的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我這一次講使用scroll實(shí)現(xiàn)彈性滑動,我不會只有一個例子就說完,因?yàn)閷懳恼碌臅r候我也在學(xué)習(xí),我分幾次講完吧。

首先上一段代碼,

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();
 }
}

這段代碼是實(shí)現(xiàn)彈性滑動的核心,第一個函數(shù)指的是緩慢滑動的意思,但是卻沒有這個滑動的實(shí)際功能。

startScroll這函數(shù)的五個參數(shù)指的是起點(diǎn)x坐標(biāo),起點(diǎn)y坐標(biāo),x位移量,y位移量,這段滑動的時間。這個函數(shù)的內(nèi)部是不斷計(jì)算在滑動時間里x和y坐標(biāo)應(yīng)該是什么值,然后因?yàn)閕nvalidate會調(diào)用computeScroll,這個computeScrollOffset函數(shù)是判斷當(dāng)前滑動是否結(jié)束,如果沒有結(jié)束通過getCurrX和getCurry獲得startScroll函數(shù)計(jì)算的值,在使用scrollTo滑動相應(yīng)的位置,因?yàn)閟tartScroll會運(yùn)算很多次,也就是將滑動時間分成很多段,相應(yīng)的坐標(biāo)也都算出來,跟著給scrollTo去實(shí)現(xiàn)滑動。

這很像是ValueAmition,將時間分成很多段,然后計(jì)算相應(yīng)的值,同時分很多次去實(shí)現(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;
   //計(jì)算滑動的偏移量,產(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)容的上邊緣(其實(shí)就是第一個ChildView的上邊緣)的距離
     * scrollY=上邊緣-View內(nèi)容上邊緣,scrollTo/By方法滑動的知識View的內(nèi)容
     * 往下滑動scrollY是負(fù)值
     */
    int scrollY=getScrollY();
    smoothScrollByScroller(scrollY);
    break;
  }
  mLastY=y;
  return true;
 }
 /**
  * 執(zhí)行滑動效果
  * 使用scroller實(shí)現(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();
  }
 }


 /**
  * 重新計(jì)算子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進(jìn)行測量
   * 此處主要是針對wrap_content這種模式進(jìn)行處理,因?yàn)槟J(rèn)情況下
   * 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();
   //將兩側(cè)的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;
 }

 /**
  * 設(shè)置子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ù),計(jì)算坐標(biāo)的一些事。

它在手指按下時記錄y坐標(biāo),在手指移動時,跟著移動子View,在手指抬起時,使用彈性滑動的函數(shù)smoothScrollByScroller。

大家會發(fā)現(xiàn)為什么一些計(jì)算出的坐標(biāo)要加負(fù)號,因?yàn)樵谖覀內(nèi)搜劾?,我們下拉y坐標(biāo)的位移量是正的,但是在系統(tǒng)認(rèn)為這個值是負(fù)的,原因我太菜不知道,知道的求大神評論留言告訴。

下一次寫一個隨手指彈性滑動的例子。

以上這篇Android Scroll實(shí)現(xiàn)彈性滑動_列表下拉彈性滑動的示例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

霍邱县| 望城县| 海淀区| 田阳县| 化州市| 东莞市| 永兴县| 民丰县| 临朐县| 镇宁| 定南县| 长顺县| 寻甸| 新郑市| 黄龙县| 合肥市| 三穗县| 名山县| 白玉县| 平邑县| 大渡口区| 高阳县| 五原县| 唐海县| 高要市| 义马市| 黎城县| 泽库县| 阆中市| 嘉兴市| 望谟县| 湖州市| 沽源县| 郎溪县| 普格县| 廊坊市| 庐江县| 克拉玛依市| 凯里市| 鄂托克旗| 桂平市|