Android使用Handler实现View弹性滑动
弹性滑动原理
将一次大的滑动非为若干次小的滑动,并在一个时间段内完成。更好的用户体验
实现方式很多种,包括用Scroller,动画,延时策略.
使用Handler实现弹性滑动
效果可以看到按钮Button向滑动。注意这里是将View的内容改变。
你可以试一试将Button外层的RelitiveLayout去掉,把id放在Button下。发现是Button的文字滑动
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="300dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Button" /> </RelativeLayout> </RelativeLayout> import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.RelativeLayout; public class MainActivity extends Activity { private static final int MESSAGE_SCROLL_TO = 1; private static final int FRAME_OUT = 30; private static final int DELAYED_TIME = 30; private RelativeLayout button; private int mcount; private Handler handler = new Handler(){ public void handleMessage(Message msg){ switch (msg.what) { case MESSAGE_SCROLL_TO: mcount++; if (mcount <= FRAME_OUT) { float fraction = mcount / (float)FRAME_OUT; int scrollx =(int) (fraction * 100); button.scrollTo(scrollx, 0); handler.sendEmptyMessageDelayed(MESSAGE_SCROLL_TO, DELAYED_TIME); } break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (RelativeLayout) findViewById(R.id.button1); handler.sendEmptyMessageDelayed(MESSAGE_SCROLL_TO, DELAYED_TIME); } }
以上所述是小编给大家介绍的Android使用Handler实现View弹性滑动,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
相关文章
Android 用Time和Calendar获取系统当前时间源码分享(年月日时分秒周几)
这篇文章主要介绍了Android 用Time和Calendar获取系统当前时间源码分享,包括年月日时分秒周几的源码,非常不错,具有参考借鉴价值,需要的朋友参考下2017-01-01Material Design系列之Behavior实现Android知乎首页
这篇文章主要为大家详细介绍了Material Design系列之Behavior实现Android知乎首页的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-09-09Android程序开发之ListView实现横向滚动(带表头与固定列)
这篇文章主要介绍了Android程序开发之ListView实现横向滚动(带表头与固定列)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下2016-07-07Android隐藏和沉浸式虚拟按键NavigationBar的实现方法
今天小编就为大家分享一篇Android隐藏和沉浸式虚拟按键NavigationBar的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-07-07
最新评论