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. ...
随机推荐
- 在Mac上配置/使用Github
文/天才晓波(简书作者)原文链接:http://www.jianshu.com/p/20eee155bbee著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 先简单介绍一下Git和Git ...
- (Problem 3)Largest prime factor
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...
- 我也来说说C#中的异步:async/await
序 最近看了一些园友们写的有关于异步的文章,受益匪浅,写这篇文章的目的是想把自己之前看到的文章做一个总结,同时也希望通过更加通俗易懂的语言让大家了解"异步"编程. 1:什么是异步 ...
- Delphi启动/停止Windows服务,启动类型修改为"自动"
unit U_StartServices; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contr ...
- U+00A0 (Non-breaking space)无法被正确压缩
Code Glyph Decimal HTML Description #U+00A0 Non-breaking space 0096 https://zh.wikipedia.org/wik ...
- Jquery的text()和html()方法在li与div取值结果解析
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Python文本处理(1)
每次处理一个字符 解决方法: 创建列表 thestring='abcdefg' thelist=list(thestring) print thelist 结果 ['a', 'b', 'c', 'd' ...
- 基于visual Studio2013解决C语言竞赛题之0413同构数
题目 解决代码及点评 该题目与水仙花数类似,只是条件不同,循环还是一样的 /***************************************************** ...
- 红黑树和AVL树的实现与比较-----算法导论
一.问题描述 实现3种树中的两种:红黑树,AVL树,Treap树 二.算法原理 (1)红黑树 红黑树是一种二叉查找树,但在每个结点上增加一个存储位表示结点的颜色,可以是red或black.红黑树满足以 ...
- 正则表达式验证数字、汉字、电话号码,email,整数,浮点数
验证数字的正则表达式集 验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0- ...