android 使用Scroller实现缓慢移动

转载请声明: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 ()
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 方法
	@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



  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论
Android Scroller 是一个用于实现平滑滚动效果的工具类。它可以用于在 Android 应用中实现滑动的动画效果,如平滑滚动到指定位置或者平滑滚动到顶部。 使用 Android Scroller 需要以下步骤: 1. 创建一个 Scroller 实例:使用 `new Scroller(context)` 创建一个 Scroller 对象。 2. 在 View 的 `computeScroll()` 方法中更新滚动位置:在需要实现滑动效果的 View 类里重写 `computeScroll()` 方法,然后在该方法中调用 `scroller.computeScrollOffset()` 获取当前的滚动位置,并根据需要更新 View 的位置。 3. 处理触摸事件:在触摸事件的回调方法中调用 Scroller 的 `startScroll()` 方法来启动滚动效果。可以根据触摸事件的不同情况调用不同的方法,如 `startScroll(int startX, int startY, int dx, int dy)` 或者 `startScroll(int startX, int startY, int dx, int dy, int duration)` 来指定滚动的起点、偏移量和持续时间。 4. 在 View 的 `invalidate()` 方法中不断重绘:在 `computeScroll()` 方法中更新了 View 的位置后,需要在 View 的 `invalidate()` 方法中调用,以便触发 View 的重新绘制。 需要注意的是,尽管 Android Scroller 提供了平滑滚动的功能,但它仅仅是一个工具类,实际的滚动效果实现还需要结合其他相关的 API 和组件来完成,如使用 `ViewGroup.LayoutParams` 来设置 View 的位置和大小,或者使用 `ViewPropertyAnimator` 实现更复杂的动画效果。 希望这个回答对你有帮助!如果有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C_Wayne

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值