当前位置: 移动技术网 > IT编程>移动开发>Android > Android ScrollView实现反弹效果的实例

Android ScrollView实现反弹效果的实例

2019年07月24日  | 移动技术网IT编程  | 我要评论

ds90ub964,vj肯尼迪,骷髅天下

 android scrollview实现反弹效果

自定义scrollview控件:

/** 
 * scrollview反弹效果的实现 
 */ 
public class bouncescrollview extends scrollview { 
  private view inner;// 孩子view 
 
  private float y;// 点击时y坐标 
 
  private rect normal = new rect();// 矩形(这里只是个形式,只是用于判断是否需要动画.) 
 
  private boolean iscount = false;// 是否开始计算 
 
  public bouncescrollview(context context, attributeset attrs) { 
    super(context, attrs); 
  } 
 
  /*** 
   * 根据 xml 生成视图工作完成.该函数在生成视图的最后调用,在所有子视图添加完之后. 即使子类覆盖了 onfinishinflate 
   * 方法,也应该调用父类的方法,使该方法得以执行. 
   */ 
  @override 
  protected void onfinishinflate() { 
    if (getchildcount() > 0) { 
      inner = getchildat(0); 
    } 
  } 
 
  /*** 
   * 监听touch 
   */ 
  @override 
  public boolean ontouchevent(motionevent ev) { 
    if (inner != null) { 
      commontouchevent(ev); 
    } 
 
    return super.ontouchevent(ev); 
  } 
 
  /*** 
   * 触摸事件 
   * 
   * @param ev 
   */ 
  public void commontouchevent(motionevent ev) { 
    int action = ev.getaction(); 
    switch (action) { 
    case motionevent.action_down: 
      break; 
    case motionevent.action_up: 
      // 手指松开. 
      if (isneedanimation()) { 
        animation(); 
        iscount = false; 
      } 
      break; 
    /*** 
     * 排除出第一次移动计算,因为第一次无法得知y坐标, 在motionevent.action_down中获取不到, 
     * 因为此时是myscrollview的touch事件传递到到了listview的孩子item上面.所以从第二次计算开始. 
     * 然而我们也要进行初始化,就是第一次移动的时候让滑动距离归0. 之后记录准确了就正常执行. 
     */ 
    case motionevent.action_move: 
      final float prey = y;// 按下时的y坐标 
      float nowy = ev.gety();// 时时y坐标 
      int deltay = (int) (prey - nowy);// 滑动距离 
      if (!iscount) { 
        deltay = 0; // 在这里要归0. 
      } 
 
      y = nowy; 
      // 当滚动到最上或者最下时就不会再滚动,这时移动布局 
      if (isneedmove()) { 
        // 初始化头部矩形 
        if (normal.isempty()) { 
          // 保存正常的布局位置 
          normal.set(inner.getleft(), inner.gettop(), 
              inner.getright(), inner.getbottom()); 
        } 
        log.e("jj", "矩形:" + inner.getleft() + "," + inner.gettop() 
            + "," + inner.getright() + "," + inner.getbottom()); 
        // 移动布局 
        inner.layout(inner.getleft(), inner.gettop() - deltay / 2, 
            inner.getright(), inner.getbottom() - deltay / 2); 
      } 
      iscount = true; 
      break; 
 
    default: 
      break; 
    } 
  } 
 
  /*** 
   * 回缩动画 
   */ 
  public void animation() { 
    // 开启移动动画 
    translateanimation ta = new translateanimation(0, 0, inner.gettop(), 
        normal.top); 
    ta.setduration(200); 
    inner.startanimation(ta); 
    // 设置回到正常的布局位置 
    inner.layout(normal.left, normal.top, normal.right, normal.bottom); 
 
    log.e("jj", "回归:" + normal.left + "," + normal.top + "," + normal.right 
        + "," + normal.bottom); 
 
    normal.setempty(); 
 
  } 
 
  // 是否需要开启动画 
  public boolean isneedanimation() { 
    return !normal.isempty(); 
  } 
 
  /*** 
   * 是否需要移动布局 inner.getmeasuredheight():获取的是控件的总高度 
   * 
   * getheight():获取的是屏幕的高度 
   * 
   * @return 
   */ 
  public boolean isneedmove() { 
    int offset = inner.getmeasuredheight() - getheight(); 
    int scrolly = getscrolly(); 
    log.e("jj", "scrolly=" + scrolly); 
    // 0是顶部,后面那个是底部 
    if (scrolly == 0 || scrolly == offset) { 
      return true; 
    } 
    return false; 
  } 
 
} 

实现反弹效果:

<com.techrare.view.bouncescrollview 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/tab_chart_bg" 
    android:scrollbars="none" > 
 
    <linearlayout 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:gravity="center_horizontal" 
      android:orientation="vertical" 
      android:paddingleft="20dp" 
      android:paddingright="20dp" > 
<span style="white-space:pre">   </span><!-- 这里可以尽情的布局 --> 
    </linearlayout> 
</com.techrare.view.bouncescrollview> 

以上就是 android scrollview实现反弹效果的实例的讲解,本站关于android开发的文章还有很多,欢迎大家搜索查阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网