欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android使用Handler实现View弹性滑动

时间:2021-05-04 10:48:06|栏目:Android代码|点击:

弹性滑动原理

将一次大的滑动非为若干次小的滑动,并在一个时间段内完成。更好的用户体验

实现方式很多种,包括用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文件管理器(项目一)

栏    目:Android代码

下一篇:Android仿微信语音对讲录音功能

本文标题:Android使用Handler实现View弹性滑动

本文地址:http://www.codeinn.net/misctech/114250.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有