android 中文 api (64) —— Scroller

前言html

  本章内容是 android.widget.Scroller,版本为Android 2.3 r1,翻译来自"pengyouhong",再次感谢"pengyouhong"!期待你一块儿参与Android中文API的翻译,联系我over140@gmail.com。java

 

声明android

  欢迎转载,但请保留文章原始出处:) 
web

    博客园:<A style="COLOR: rgb(26,139,200); TEXT-DECORATION: none" title=http://www.cnblogs.com href="http://www.cnblogs.com/">http://www.cnblogs.com/app

    Android中文翻译组:<A style="COLOR: rgb(26,139,200); TEXT-DECORATION: none" title=http://code.taobao.org/project/view/404/ href="http://code.taobao.org/project/view/404/">http://code.taobao.org/project/view/404/
ide

 

正文函数

  1、结构post

    public class Scroller extends Object动画

 

    java.lang.Objectthis

      android.widget.Scroller

 

  2、概述

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

 

  3、构造函数

  public Scroller (Context context)

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

 

  public Scroller (Context context, Interpolator interpolator)

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

 

  4、公共方法

  public void abortAnimation ()

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

  参见

        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, int minX, 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 ()

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

      返回值

            通过时间以毫秒为单位

 

  5、补充

    文章精选

      Scroller 粗浅理解

      ScrollTextView - scrolling TextView for Android

    示例代码

      建立工程MyScroler,或者将下类名“MyScroler”改成本身建立的工程,将下面代码直接覆盖生成的.java文件运行便可:

复制代码

package my.Scroller;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class MyScroler extends Activity {
    
/** Called when the activity is first created. */
    LinearLayout lay1,lay2,lay;
     
private Scroller mScroller;
     
private boolean s1,s2;
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        mScroller 
= new Scroller(this);
         lay1 
= new LinearLayout(this){
             @Override 
             
public void computeScroll() { 
                 
if (mScroller.computeScrollOffset()) { 
                     scrollTo(mScroller.getCurrX(), 
0); 
                     postInvalidate(); 
                 } 
             } 
         };
         lay2 
= new LinearLayout(this){
             @Override 
             
public void computeScroll() { 
                 
if (mScroller.computeScrollOffset()) { 
                    
// mScrollX = mScroller.getCurrX(); 
                     scrollTo(mScroller.getCurrX(), 0); 
                     postInvalidate(); 
                 } 
             } 
         };
      lay1.setBackgroundColor(
this.getResources().getColor(android.R.color.darker_gray));
        lay2.setBackgroundColor(
this.getResources().getColor(android.R.color.white));
        lay 
= new LinearLayout(this);
        lay.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams p0 
= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);     
        
this.setContentView(lay, p0);
        
        LinearLayout.LayoutParams p1 
= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);     
        p1.weight
=1;
        lay.addView(lay1,p1);
        LinearLayout.LayoutParams p2 
= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);     
        p2.weight
=1;
        lay.addView(lay2,p2);
        Button tx 
= new Button(this);
        Button tx2 
= new Button(this);
        tx.setText(
"Button1");  
        tx2.setText(
"Button2");
        tx.setOnClickListener(
new OnClickListener(){
            @Override
            
public void onClick(View v) {
                
if(!s1){
                    mScroller.startScroll(
0051010);
                    s1 
= true;
                }
else{
                    mScroller.startScroll(
00-50-10,10);
                    s1 
= false;
                }
            }
            
        });
        tx2.setOnClickListener(
new OnClickListener(){
            @Override
            
public void onClick(View v) {
                
if(!s2){
                    mScroller.startScroll(
00520,10);
                    s2
=true;
                }
else{
                    mScroller.startScroll(
2020-50-20,10);
                    s2
=false;
                }
            }
        });
        lay1.addView(tx);
        lay2.addView(tx2);
    }
}

复制代码


结束

  本文中文资料较少,内容也较长,再次感谢"pengyouhong"是辛勤付出 :)

相关文章
相关标签/搜索