android 使用Scroller实现缓慢移动
在Launcher中的Workspace中实现了左右屏幕切换效果,里面就用到了Scroller记录滑动轨迹,实现一种缓慢地向左或向右移动的效果,这里我对这种效果进行总结:
我们先看一个例子:点击按钮时红经块会从左边缓慢地移向左右,这个该怎么实现呢

我们先来看一下,Scroller,这个对象里有startScroll方法
void android.widget.Scroller.startScroll(int startX, int startY, int dx, int dy, int duration)
第一个参数是起始移动的x坐标值,第二个是起始移动的y坐标值,第三个第四个参数都是移到某点的坐标值,而duration 当然就是执行移动的时间。这个有什么用呢。要知道有什么用还得再看一个方法
当startScroll执行过程中即在duration时间内,computeScrollOffset 方法会一直返回false,但当动画执行完成后会返回返加true.
有了这两个方法还不够,我们还需要再重写viewGroup的一个方法,
computeScroll 这个方法什么时候会被调用呢
官网上这样说的
public void computeScroll ()
Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary. This will typically be done if the child is animating a scroll using a Scroller object.
当我们执行ontouch或invalidate()或postInvalidate()都会导致这个方法的执行
所以我们像下面这样调用,postInvalidate执行后,会去调computeScroll 方法,而这个方法里再去调postInvalidate,这样就可以不断地去调用scrollTo方法了,直到mScroller动画结束,当然第一次时,我们需要手动去调用一次postInvalidate才会去调用
computeScroll 方法
- @Override
- public void computeScroll() {
- if (mScroller.computeScrollOffset()) {
- scrollTo(mScroller.getCurrX(), 0);
- postInvalidate();
- }
- }
下面附上上面那个例子的源代码
首先是MyViewGroup.java
- package com.wb;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
- import android.widget.Scroller;
- public class MyViewGroup extends LinearLayout {
- private boolean s1=true;
- Scroller mScroller=null;
- public MyViewGroup(Context context, AttributeSet attrs) {
- super(context, attrs);
- mScroller=new Scroller(context);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void computeScroll() {
- if (mScroller.computeScrollOffset()) {
- scrollTo(mScroller.getCurrX(), 0);
- postInvalidate();
- }
- }
- public void beginScroll(){
- if (!s1) {
- mScroller.startScroll(0, 0, 0, 0, 1000);
- s1 = true;
- } else {
- mScroller.startScroll(0, 0, -500, 0, 1000);
- s1 = false;
- }
- invalidate();
- }
- }
然后是WheelActivity.java
- package com.wb;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.AbsListView.LayoutParams;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.Adapter;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- public class WheelActivity extends Activity {
- private ListView listView = null;
- private MyViewGroup myViewGroup;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myViewGroup = (MyViewGroup) findViewById(R.id.myviewGroup);
- }
- public void scroll(View view) {
- myViewGroup.beginScroll();
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="scroll"
- android:onClick="scroll" />
- <com.wb.MyViewGroup
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" android:id="@+id/myviewGroup">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:background="#ff0000"
- android:text="我在這"/>
- </com.wb.MyViewGroup>
- </LinearLayout>
源代码下载地址:http://download.csdn.net/detail/c_weibin/4208751
android 使用Scroller实现缓慢移动的更多相关文章
- 【转】Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果
原文网址:http://blog.csdn.net/xiaanming/article/details/17539199 转帖请注明本文出自xiaanming的博客(http://blog.csdn. ...
- [转]Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17539199),请尊重他人的辛勤劳动成果,谢谢! 我在上一 ...
- Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17539199),请尊重他人的辛勤劳动成果,谢谢! 我在上一 ...
- [Android Pro] Scroller使用分析
reference to : http://blog.csdn.net/a910626/article/details/51548840 一.Scroller是什么? Android里 Scrolle ...
- Android学习Scroller(五)——具体解释Scroller调用过程以及View的重绘
PS: 该篇博客已经deprecated,不再维护.详情请參见 站在源代码的肩膀上全解Scroller工作机制 http://blog.csdn.net/lfdfhl/article/detail ...
- 【Android】Scroller分析
mScroller.getCurrX() //获取mScroller当前水平滚动的位置 mScroller.getCurrY() //获取mScroller当前竖直滚动的位置 mScroller.ge ...
- Android学习Scroller(三)——控件平移划过屏幕 (Scroller简单使用)
MainActivity例如以下: package cc.cn; import android.os.Bundle; import android.view.View; import android. ...
- Android中Scroller类的分析
今天看了一下项目中用到的ViewFlow控件,想弄明白其工作原理.从头开始分析,卡在"滚动"这儿了. 做android也快两年了,连最基本的滚动都不熟悉,真是惭愧...遂网上找资料 ...
- Android得知Scroller(两)——ViewGroup转让scrollTo()
MainActivity例如下列: package cc.ac; import android.os.Bundle; import android.view.View; import android. ...
随机推荐
- VS 2013上Python的配置
最近有点不务正业,去看了下Python (主要是学校OJ有这个语言,然后可以轻松解决大数据问题,不要说我太坑~~~) 目前感觉python和matlab有些类似,缺少了变量类型声明,总感觉自己写出来的 ...
- 一个库搞定各种分享--ShareSDK
ShareSDK是为iOS.Android.WindowsPhone提供社会功能的一个组件,开发者只需10分钟即可集成到自己的APP中,它不仅支持分享给QQ好友.微信好友.微信朋友圈.新浪微博.腾迅微 ...
- Javascript 运动基础 01
JS运动基础 运动基础 让Div运动起来 速度——物体运动的快慢 运动中的Bug 不会停止 速度取某些值会无法停止 到达位置后再点击还会运动 重复点击速度加快 匀速运动 速度不变 <s ...
- vhost文件设置
#例子<VirtualHost *.82> #设置端口号为82 ServerName localhost #服务器名称 DocumentRoot "d:/Web" #文 ...
- Win7 x64安装Paramiko出问题
今天上午windows下配置paramiko环境时出现问题,随手记录下来. 先说一下我的环境: win7 x64 旗舰版.Python3.5.0.pip8.1.0 pip install para ...
- php unset 数组陷阱
我们删除一个array, unset($arr); 想删除某个元素 unsert($arr[i]) 一个陷阱是: unset() 函数允许删除数组中的某个键.但要注意数组将不会重建索引.如果需要删除后 ...
- 设计模式(四)原型模式Prototype(创建型)
设计模式(四)原型模式Prototype(创建型) 1. 概述 我们都知道,创建型模式一般是用来创建一个新的对象,然后我们使用这个对象完成一些对象的操作,我们通过原型模式可以快速的创建一个对象 ...
- 基于visual Studio2013解决算法导论之055拓扑排序
题目 拓扑排序 解决代码及点评 // 拓扑排序.cpp : 定义控制台应用程序的入口点. // // 深度优先.cpp : 定义控制台应用程序的入口点. // // 图的邻接表表示.cpp : ...
- MongoDB 操作手冊CRUD查询指针
枚举遍历指针 概述 前面已经讲过,db.collection.find()假设没有指定给一个var声明的变量.将自己主动枚举前20条记录. 手动枚举指针 在mongo控制台中.将查询赋给一个var声明 ...
- Jsp分页实例---假分页
今天总结一个JSP假分页的实例,由基本功能由js实现. 相较前一篇真分页中程序的功能,丰富了一些.具备首页尾页,和页面跳转功能. 首先还是来总结一下真假分页的优缺点和特性吧. 假分页:从数据库中取出所 ...