android 使用Scroller实现缓慢移动 (笔记)
来源:互联网 发布:mysql语句优化总结 编辑:程序博客网 时间:2024/05/16 17:49
转载请声明:http://bbs.niuzhi.cc/thread-24-1-1.html
在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 ()
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.
所以我们像下面这样调用,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();
- }
- }
- <?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实现缓慢移动
- android 使用Scroller实现缓慢移动
- Android通过scroller实现缓慢移动
- GeneralSanguo工作笔记2——使用Scroller实现背景的拖拽移动
- Scroller 实现控件缓慢平移的效果
- Android 使用Scroller 实现渐变式滑动
- Android Scroller 源码笔记
- Android Scroller学习笔记
- 使用Scroller实现滚动
- android Scroller使用
- Android Scroller使用详解
- Android Scroller OverScroller使用
- Android View学习笔记(三):Scroller的原理剖析及使用(上)
- Android View学习笔记(四):Scroller的原理剖析及使用(下)
- Android学习笔记-使用Scroller来滚动视图
- Android 使用Scroller自动滚动第一种实现
- Android 使用Scroller自动滚动第二种实现
- 在Eclipse中使用JUnit4进行单元测试(高级篇)
- 学习学习再学习-作者:李笑来
- DbXImObXHh
- 如何在MyEclipse中设置JVM内存
- BIEE11g积分卡
- android 使用Scroller实现缓慢移动 (笔记)
- 递归
- sed替换整行
- 项目二 圆柱体表面积
- Android开发之MediaPlayer和SurfaceView播放视频
- Android 存储路径相关知识小结
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- 安装MS SQL Server 2008 “性能计数器注册表配置单元一致性”失败的解决办法
- SpringMvc项目的完整配置