Scroller 简单应用

Scroller记录滑动轨迹,实现一种缓慢地向左或向右移动的效果 

效果图:

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

方法:

public boolean computeScrollOffset ()

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

public final int getCurrX ()

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

      返回值

          距离原点X方向的绝对值


  public final int getCurrY ()

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

      返回值

          距离原点Y方向的绝对值

 

  public final int getDuration ()

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

      返回值

          滚动持续的毫秒数


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    滚动持续时间,以毫秒计。

需要复写方法:computeScroll

computeScroll()函数正是我们大展身手的地方,在这个函数里我们可以去取得事先设置好的成员变量mScroller中的位置信息、速度信息等等,用这些参数来做我们想做的事情。在computeScroll移动到x位置执行:scrollTo(mScroller.getCurrX(),0) ;

具体代码:

首先实现一个继承LinearLayout的类:

public class MyLinearLayout extends LinearLayout {
    private Scroller mScroller = null ;
    private boolean isClick = false ;


    public MyLinearLayout(Context context, AttributeSet attrs ) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    mScroller = new Scroller(context) ;
    }
   
    @Override
    public void computeScroll() {
    // TODO Auto-generated method stub
    if(mScroller.computeScrollOffset()){

//移动到想要的位置
    scrollTo(mScroller.getCurrX(),0) ;
    //刷新界面
    invalidate() ;
    }
    }
   
    public void scrollToPlace(int dx){
    if(!isClick){

//滚动
    mScroller.startScroll(0, 0, dx, 0,1000) ;
    isClick = true; 
    }else{
    mScroller.startScroll(0, 0, -dx, 0,1000) ;
    isClick = false ;
    }
    }
}

main.xml 文件:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <com.example.scrollerdemo.MyLinearLayout
        android:id="@+id/linear1"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@color/palegoldenrod"
        android:orientation="vertical" >


        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        
    </com.example.scrollerdemo.MyLinearLayout>


    <com.example.scrollerdemo.MyLinearLayout
        android:id="@+id/linear2"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginTop="86dp"
        android:background="@color/blanchedalmond"
        android:orientation="vertical" >
<Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </com.example.scrollerdemo.MyLinearLayout>


</RelativeLayout>


main.java


public class MainActivity extends Activity {
private MyLinearLayout linear1 ;
private MyLinearLayout linear2 ;
private Button but1 ;
private Button but2 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        but1 = (Button)findViewById(R.id.button1) ;
        but2 = (Button)findViewById(R.id.button2) ;
        linear1 = (MyLinearLayout)findViewById(R.id.linear1) ;
        linear2 = (MyLinearLayout)findViewById(R.id.linear2) ;
        but1.setOnClickListener(new OnClickListener(){


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//负数向右,正数向左
linear1.scrollToPlace(-200) ;
}
       
        }) ;
        but2.setOnClickListener(new OnClickListener(){


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
linear2.scrollToPlace(-100) ;
}
       
        }) ;
    }  
}

 需要注意的是Scroller 移动的是它的内容,比如上面的MyLinearLayout 移动的是Button 因为Btton 才是它的内容,所以移动的时候Button 发生移动

Scroller 移动一般到mScroller.computeScrollOffset()返回false才停下来




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值