本文实例为大家分享了android scrollview实现水平滑动回弹的具体代码,供大家参考,具体内容如下
在研究了view的一些属性之后做了个scroll的水平滑动回弹。
效果图:
主要代码:
import android.content.context; import android.graphics.rect; import android.util.attributeset; import android.view.motionevent; import android.view.view; import android.view.animation.translateanimation; import android.widget.horizontalscrollview; /** * scrollview水平滑动回弹 * * @author qhg * @date 2014年3月12日 * */ public class mhorizontalscrollview extends horizontalscrollview { private view view; /** * 移动时的增量 */ private static final int deltax = 1; private rect normalrt = new rect(); public mhorizontalscrollview(context context) { super(context); } public mhorizontalscrollview(context context, attributeset attrs) { super(context, attrs); } /** * 在xml布局执行完后执行此方法 */ protected void onfinishinflate() { if (getchildcount() > 0) { view = getchildat(0); } } @override public boolean ontouchevent(motionevent event) { if (view != null) { ontoucheventimpl(event); } return super.ontouchevent(event); } private void ontoucheventimpl(motionevent event) { switch (event.getaction()) { case motionevent.action_move: // 在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位 scrollby(deltax, 0); // 当滚动到最左或最右时就不会再滚动,这时移动布局达到回弹效果 if (islayoutmove()) { if (normalrt.isempty()) { // 保存当前正常的布局位置,拉过头才能回弹到正常位置 normalrt.set(view.getleft(), view.gettop(), view.getright(), view.getbottom()); } // 移动布局 view.layout(view.getleft() - deltax, view.gettop(), view.getright() - deltax, view.getbottom()); } break; case motionevent.action_up: if (isneedanimation()) { animationimpl(); } break; default: break; } } /** * 动画移动 */ private void animationimpl() { // 移动动画 translateanimation ta = new translateanimation(view.getleft(), normalrt.left, 0, 0); // 动画持续时间 ta.setduration(50); view.startanimation(ta); // 设置回到当前正常的布局位置 view.layout(normalrt.left, normalrt.top, normalrt.right, normalrt.bottom); normalrt.setempty(); } /** * 是否需要开启动画 * * @return */ private boolean isneedanimation() { return !normalrt.isempty(); } /** * 是否需要移动布局 * * @return */ private boolean islayoutmove() { int offset = view.getmeasuredwidth() - getwidth(); if (offset <= 0) { return false; } // 上面已固定deltax=1,scrollx永远等于1所以向右拉不动 // 但当向左拉动到内容布局的最右端时scrollx == offset时还可以继续拉动 int scrollx = getscrollx(); if (scrollx == 0 || scrollx == offset) { return true; } return false; } }
在xml布局文件里直接使用:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/background" > <cn.qhg.mhorizontalscrollview android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none" > <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingtop="100dp" android:id="@+id/ll_test" android:onclick="test" > <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/house_1" android:layout_marginright="40dp" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/house_4" android:layout_marginright="40dp" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/house_2" android:layout_marginright="40dp" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/house_3" android:layout_marginright="40dp" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/house_1" android:layout_marginright="40dp" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/house_4" android:layout_marginright="40dp" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/house_2" android:layout_marginright="40dp" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/house_3" android:layout_marginright="40dp" /> <!-- 使右边多空一点 --> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginright="20dp" /> </linearlayout> </cn.qhg.mhorizontalscrollview> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。