国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android界面上拉下拉的回彈效果實(shí)例代碼

2019-12-12 03:31:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

廢話不多說(shuō),具體代碼如下所示:

public class MyScrollView extends ScrollView {  private View childView;  public MyScrollView(Context context) {    super(context);  }  public MyScrollView(Context context, AttributeSet attrs) {    super(context, attrs);  }  public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);  }//  @Override//  protected void onLayout(boolean changed, int l, int t, int r, int b) {//    super.onLayout(changed, l, t, r, b);//  }  //獲取子視圖  @Override  protected void onFinishInflate() {    super.onFinishInflate();    if (getChildCount() > 0) {      childView = getChildAt(0);    }  }  private int lastY;//上一次y軸方向操作的坐標(biāo)位置  private Rect normal = new Rect();//用于記錄臨界狀態(tài)的左、上、右、下  private boolean isFinishAnimation = true;//是否動(dòng)畫結(jié)束  private int lastX, downX, downY;  //攔截:實(shí)現(xiàn)父視圖對(duì)子視圖的攔截  //是否攔截成功,取決于方法的返回值。返回值true:攔截成功。反之,攔截失敗  @Override  public boolean onInterceptTouchEvent(MotionEvent ev) {    boolean isIntercept = false;    int eventX = (int) ev.getX();    int eventY = (int) ev.getY();    switch (ev.getAction()) {      case MotionEvent.ACTION_DOWN:        lastX = downX = eventX;        lastY = downY = eventY;        break;      case MotionEvent.ACTION_MOVE:        //獲取水平和垂直方向的移動(dòng)距離        int absX = Math.abs(eventX - downX);        int absY = Math.abs(eventY - downY);        if(absY > absX && absY >= UIUtils.dp2px(10)){          isIntercept = true;//執(zhí)行攔截        }        lastX = eventX;        lastY = eventY;        break;    }    return isIntercept;  }  @Override  public boolean onTouchEvent(MotionEvent ev) {    if (childView == null || !isFinishAnimation) {      return super.onTouchEvent(ev);    }    int eventY = (int) ev.getY();//獲取當(dāng)前的y軸坐標(biāo)    switch (ev.getAction()) {      case MotionEvent.ACTION_DOWN:        lastY = eventY;        break;      case MotionEvent.ACTION_MOVE:        int dy = eventY - lastY;//微小的移動(dòng)量        if (isNeedMove()) {          if (normal.isEmpty()) {            //記錄了childView的臨界狀態(tài)的左、上、右、下            normal.set(childView.getLeft(), childView.getTop(), childView.getRight(), childView.getBottom());          }          //重新布局          childView.layout(childView.getLeft(), childView.getTop() + dy / 2, childView.getRight(), childView.getBottom() + dy / 2);        }        lastY = eventY;//重新賦值        break;      case MotionEvent.ACTION_UP:        if (isNeedAnimation()) {          //使用平移動(dòng)畫          int translateY = childView.getBottom() - normal.bottom;          TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, -translateY);          translateAnimation.setDuration(200);//        translateAnimation.setFillAfter(true);//停留在最終位置上          translateAnimation.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {              isFinishAnimation = false;            }            @Override            public void onAnimationEnd(Animation animation) {              isFinishAnimation = true;              childView.clearAnimation();//清除動(dòng)畫              //重新布局              childView.layout(normal.left, normal.top, normal.right, normal.bottom);              //清除normal的數(shù)據(jù)              normal.setEmpty();            }            @Override            public void onAnimationRepeat(Animation animation) {            }          });          //啟動(dòng)動(dòng)畫          childView.startAnimation(translateAnimation);        }        break;    }    return super.onTouchEvent(ev);  }  //判斷是否需要執(zhí)行平移動(dòng)畫  private boolean isNeedAnimation() {    return !normal.isEmpty();  }  private boolean isNeedMove() {    int childMeasuredHeight = childView.getMeasuredHeight();//獲取子視圖的高度    int scrollViewMeasuredHeight = this.getMeasuredHeight();//獲取布局的高度    Log.e("TAG", "childMeasuredHeight = " + childMeasuredHeight);    Log.e("TAG", "scrollViewMeasuredHeight = " + scrollViewMeasuredHeight);    int dy = childMeasuredHeight - scrollViewMeasuredHeight;//dy >= 0    int scrollY = this.getScrollY();//獲取用戶在y軸方向上的偏移量 (上 + 下 -)    if (scrollY <= 0 || scrollY >= dy) {      return true;//按照我們自定義的MyScrollView的方式處理    }    //其他處在臨界范圍內(nèi)的,返回false。即表示,仍按照ScrollView的方式處理    return false;  }}

以上所述是小編給大家介紹的Android界面上拉下拉的回彈效果實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 屯留县| 达孜县| 淳化县| 舒兰市| 西藏| 洛川县| 尼木县| 鄂伦春自治旗| 阿拉善左旗| 三穗县| 本溪市| 宁蒗| 青田县| 陕西省| 车险| 融水| 准格尔旗| 延寿县| 昆山市| 舞钢市| 钟祥市| 铁岭县| 沙雅县| 台山市| 屯留县| 襄樊市| 日土县| 石城县| 友谊县| 邵阳市| 阜平县| 建平县| 芦溪县| 商城县| 榕江县| 比如县| 德清县| 新闻| 农安县| 察雅县| 长子县|