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 当然就是执行移动的时间。这个有什么用呢。要知道有什么用还得再看一个方法

boolean android.widget.Scroller.computeScrollOffset()

当startScroll执行过程中即在duration时间内,computeScrollOffset  方法会一直返回false,但当动画执行完成后会返回返加true.

有了这两个方法还不够,我们还需要再重写viewGroup的一个方法,

computeScroll 这个方法什么时候会被调用呢

官网上这样说的

public void computeScroll ()

Since: API Level 1

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 方法

[java] view plaincopy

  1. @Override
  2. public void computeScroll() {
  3. if (mScroller.computeScrollOffset()) {
  4. scrollTo(mScroller.getCurrX(), 0);
  5. postInvalidate();
  6. }
  7. }

下面附上上面那个例子的源代码

首先是MyViewGroup.java

[java] view plaincopy

  1. package com.wb;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.LinearLayout;
  7. import android.widget.Scroller;
  8. public class MyViewGroup extends LinearLayout {
  9. private boolean s1=true;
  10. Scroller mScroller=null;
  11. public MyViewGroup(Context context, AttributeSet attrs) {
  12. super(context, attrs);
  13. mScroller=new Scroller(context);
  14. // TODO Auto-generated constructor stub
  15. }
  16. @Override
  17. public void computeScroll() {
  18. if (mScroller.computeScrollOffset()) {
  19. scrollTo(mScroller.getCurrX(), 0);
  20. postInvalidate();
  21. }
  22. }
  23. public void beginScroll(){
  24. if (!s1) {
  25. mScroller.startScroll(0, 0, 0, 0, 1000);
  26. s1 = true;
  27. } else {
  28. mScroller.startScroll(0, 0, -500, 0, 1000);
  29. s1 = false;
  30. }
  31. invalidate();
  32. }
  33. }

然后是WheelActivity.java

[java] view plaincopy

  1. package com.wb;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AbsListView;
  9. import android.widget.AbsListView.LayoutParams;
  10. import android.widget.AbsListView.OnScrollListener;
  11. import android.widget.Adapter;
  12. import android.widget.BaseAdapter;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. public class WheelActivity extends Activity {
  16. private ListView listView = null;
  17. private MyViewGroup myViewGroup;
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. myViewGroup = (MyViewGroup) findViewById(R.id.myviewGroup);
  23. }
  24. public void scroll(View view) {
  25. myViewGroup.beginScroll();
  26. }
  27. }

main.xml

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <Button
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="scroll"
  10. android:onClick="scroll" />
  11. <com.wb.MyViewGroup
  12. xmlns:android="http://schemas.android.com/apk/res/android"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:orientation="vertical" android:id="@+id/myviewGroup">
  16. <TextView
  17. android:layout_width="wrap_content"
  18. android:layout_height="fill_parent"
  19. android:background="#ff0000"
  20. android:text="我在這"/>
  21. </com.wb.MyViewGroup>
  22. </LinearLayout>

源代码下载地址:http://download.csdn.net/detail/c_weibin/4208751

时间: 2024-08-10 19:19:05

android 使用Scroller实现缓慢移动的相关文章

【转】Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果

原文网址:http://blog.csdn.net/xiaanming/article/details/17539199 我在上一篇文章中Android 带你从源码的角度解析Scroller的滚动实现原理从源码的角度介绍了Scroller的滚动实现原理,相信大家对Scroller的使用有一定的了解,这篇文章就给大家带来使用Scroller的小例子,来帮助大家更加熟悉的掌握Scroller的使用,掌握好了Scroller的使用我们就能实现很多滑动的效果.例如侧滑菜单,launcher,ListVi

[转]Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果

我在上一篇文章中Android 带你从源码的角度解析Scroller的滚动实现原理从源码的角度介绍了Scroller的滚动实现原理,相信大家对Scroller的使用有一定的了解,这篇文章就给大家带来使用Scroller的小例子,来帮助大家更加熟悉的掌握Scroller的使用,掌握好了Scroller的使用我们就能实现很多滑动的效果.例如侧滑菜单,launcher,ListView的下拉刷新等等效果,我今天实现的是ListView的item的左右滑动删除item的效果,现在很多朋友看到这个效果应该

Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果

我在上一篇文章中Android 带你从源码的角度解析Scroller的滚动实现原理从源码的角度介绍了Scroller的滚动实现原理,相信大家对Scroller的使用有一定的了解,这篇文章就给大家带来使用Scroller的小例子,来帮助大家更加熟悉的掌握Scroller的使用,掌握好了Scroller的使用我们就能实现很多滑动的效果.例如侧滑菜单,launcher,ListView的下拉刷新等等效果,我今天实现的是ListView的item的左右滑动删除item的效果,现在很多朋友看到这个效果应该

Android学习Scroller(五)——详解Scroller调用过程以及View的重绘

MainActivity如下: package cc.ww; import android.os.Bundle; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; import android.app.Activity;

Android学习Scroller(四)——实现拉动后回弹的布局

MainActivity如下: package cc.testscroller2; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 实现可以拉动后回弹的布局. * 类似于下拉刷新的. * * 参考资料: * 1 http://gundumw100.iteye.com/blog/1884373 * 2 http://blog.csdn.net/gemmem/article/details/7321910

Android学习Scroller(三)——控件平移划过屏幕 (Scroller简单使用)

MainActivity如下: package cc.cn; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; /** * Demo描述: * Scroller使用示例--让控件平移划过屏幕 * * 参考资料: * http://blog.cs

Android学习Scroller(二)——ViewGroup调用scrollTo()

MainActivity如下: package cc.ac; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.app.Activity; /** * Demo描述: * 对ViewGroup调用sc

Android学习Scroller(一)——View调用scrollTo()的理解及使用

MainActivity如下: package cc.uu; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.app.Activity; /** * Demo描述: * scrollTo()和scrollB

Android学习Scroller(三)

MainActivity如下: package cc.testscroller2; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 实现可以拉动后回弹的布局. * 类似于下拉刷新的. * * 参考资料: * 1 http://gundumw100.iteye.com/blog/1884373 * 2 http://blog.csdn.net/gemmem/article/details/7321910