android用户界面之scroller

发布时间:2020-08-10 10:02:07 作者:lp5345
来源:网络 阅读:1032

一、结构
    public class Scroller extends Object

二、概述

    这个类封装了滚动操作。滚动的持续时间可以通过构造函数传递,并且可以指定滚动动作的持续的最长时间。经过这段时间,滚动会自动定位到最终位置,并且通过computeScrollOffset()会得到的返回值为false,表明滚动动作已经结束。

三、构造函数

  public Scroller (Context context)

  使用缺省的持续持续时间和动画插入器创建一个Scroller。(译者注:interpolator这里翻译为动画插入器,见这里。)

  public Scroller (Context context, Interpolator interpolator)

  根据指定的动画插入器创建一个Scroller,如果指定的动画插入器为空,则会使用缺省的动画插入器(粘滞viscous)创建。

四、公共方法

  public void abortAnimation ()

  停止动画。与forceFinished(boolean)相反,Scroller滚动到最终x与y位置时中止动画。

  参见

        forceFinished(boolean)

  public boolean computeScrollOffset ()

  当想要知道新的位置时,调用此函数。如果返回true,表示动画还没有结束。位置改变以提供一个新的位置。

  public void extendDuration (int extend)

  延长滚动动画时间。此函数允许当使用setFinalX(int) or setFinalY(int) 时,卷动动作持续更长时间并且卷动更长距离。

          参数

              extend 卷动事件延长的时间,以毫秒为单位

          参见

              setFinalX(int)

              setFinalY(int)

  public void fling (int startX, int startY, int velocityX, int velocityY, intminX, int maxX, int minY, int maxY)

  在fling(译者注:快滑,用户按下触摸屏、快速移动后松开)手势基础上开始滚动。滚动的距离取决于fling的初速度。

      参数

          startX 滚动起始点X坐标

  startY 滚动起始点Y坐标

  velocityX   当滑动屏幕时X方向初速度,以每秒像素数计算

  velocityY   当滑动屏幕时Y方向初速度,以每秒像素数计算

  minX    X方向的最小值,scroller不会滚过此点。

  maxX    X方向的最大值,scroller不会滚过此点。

  minY    Y方向的最小值,scroller不会滚过此点。

  maxY    Y方向的最大值,scroller不会滚过此点。

  public final void forceFinished (boolean finished)

  强制终止的字段到特定值。(译者注:立即停止滚动?)

      参数

          finished    新的结束值

  public final int getCurrX ()

  返回当前滚动X方向的偏移

      返回值

          距离原点X方向的绝对值

  public final int getCurrY ()

  返回当前滚动Y方向的偏移

      返回值

          距离原点Y方向的绝对值

  public final int getDuration ()

  返回滚动事件的持续时间,以毫秒计算。

      返回值

          滚动持续的毫秒数

  public final int getFinalX ()

  返回滚动结束位置。仅针对“fling”手势有效

      返回值

          最终位置X方向距离原点的绝对距离

  public final int getFinalY ()

  返回滚动结束位置。仅针对“fling”操作有效

      返回值

          最终位置Y方向距离原点的绝对距离

  public final int getStartX ()

  返回滚动起始点的X方向的偏移

      返回值

          起始点在X方向距离原点的绝对距离

  public final int getStartY ()

  返回滚动起始点的Y方向的偏移

      返回值

          起始点在Y方向距离原点的绝对距离

  public final boolean isFinished ()

  返回scroller是否已完成滚动。

      返回值

          停止滚动返回true,否则返回false

  public void setFinalX (int newX)

  设置scroller的X方向终止位置

      参数

          newX    新位置在X方向距离原点的绝对偏移。

      参见

          extendDuration(int)

         setFinalY(int)

  public void setFinalY (int newY)

  设置scroller的Y方向终止位置

      参数

          newY    新位置在Y方向距离原点的绝对偏移。

      参见

          extendDuration(int)

          setFinalY(int)

  public void startScroll (int startX, int startY, int dx, int dy)

  以提供的起始点和将要滑动的距离开始滚动。滚动会使用缺省值250ms作为持续时间。

      参数

          startX 水平方向滚动的偏移值,以像素为单位。正值表明滚动将向左滚动

  startY 垂直方向滚动的偏移值,以像素为单位。正值表明滚动将向上滚动

  dx 水平方向滑动的距离,正值会使滚动向左滚动

  dy 垂直方向滑动的距离,正值会使滚动向上滚动

  public void startScroll (int startX, int startY, int dx, int dy, int duration)

  以提供的起始点和将要滑动的距离开始滚动。

      参数

          startX 水平方向滚动的偏移值,以像素为单位。正值表明滚动将向左滚动

  startY 垂直方向滚动的偏移值,以像素为单位。正值表明滚动将向上滚动

  dx 水平方向滑动的距离,正值会使滚动向左滚动

  dy 垂直方向滑动的距离,正值会使滚动向上滚动

        duration    滚动持续时间,以毫秒计。

  public int timePassed ()

  返回自滚动开始经过的时间

      返回值

            经过时间以毫秒为单位

五、简单用法:
  Android里Scroller类是为了实现View平滑滚动的一个Helper 类。通常在自定义的View时使用,在View中定义一个私有成员mScroller =   new Scroller(context)。设置mScroller滚动的位置时,并不会导致View的滚动,通常是用mScroller记录/计算View滚 动的位置,再重写View的computeScroll(),完成实际的滚动。

  

自定义一个CustomView,使用Scroller实现滚动

import android.content.Context;

importandroid.util.AttributeSet;

import android.util.Log;

import android.view.View;

importandroid.widget.LinearLayout;

import android.widget.Scroller;

public class CustomView extendsLinearLayout {

    privatestatic final String TAG = "Scroller";

    privateScroller mScroller;

    publicCustomView(Context context, AttributeSet attrs) {

       super(context,attrs);

       mScroller= new Scroller(context);

    }

    //调用此方法滚动到目标位置

    publicvoid smoothScrollTo(int fx, int fy) {

       intdx = fx - mScroller.getFinalX();

       intdy = fy - mScroller.getFinalY();

       smoothScrollBy(dx,dy);

    }

    //调用此方法设置滚动的相对偏移

    publicvoid smoothScrollBy(int dx, int dy) {

       //设置mScroller的滚动偏移量

       mScroller.startScroll(mScroller.getFinalX(),mScroller.getFinalY(), dx, dy);

       invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果

    }

   

    @Override

    publicvoid computeScroll() {

   

       //先判断mScroller滚动是否完成

       if(mScroller.computeScrollOffset()) {

      

           //这里调用View的scrollTo()完成实际的滚动

           scrollTo(mScroller.getCurrX(),mScroller.getCurrY());

           

           //必须调用该方法,否则不一定能看到滚动效果

           postInvalidate();

       }

       super.computeScroll();

    }

}

六、深度用法:
        android使用Scroller实现缓慢移动
          在Launcher中的Workspace中实现了左右屏幕切换效果,里面就用到了Scroller记录滑动轨迹,实现一种缓慢地向左或向右移动的效果,这里我对这种效果进行总结:
我们先看一个例子:点击按钮时红经块会从左边缓慢地移向左右,这个该怎么实现呢
android用户界面之scrollerandroid用户界面之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 这个方法什么时候会被调用呢
             官网上这样说的

[size=0.9em]             public void computeScroll [size=0.9em]()[size=0.8em]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.javapackage 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>


推荐阅读:
  1. Egret之eui.Scroller
  2. js 性能优化之快速响应的用户界面

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android roller roi

上一篇:目标检测入门系列手册二:RCNN训练教程

下一篇:MySQL主从不同步问题分析与处理思路

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》