对Scroller中的startScroll方法的理解

API如下:

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

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

      参数

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

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

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

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

我的理解是:

startX 表示起点在水平方向到原点的距离(可以理解为X轴坐标,但与X轴相反),正值表示在原点左边,负值表示在原点右边。

dx 表示滑动的距离,正值向左滑,负值向右滑。

这与我们感官逻辑相反,需要注意。


还有一点需要明白的是scrollTo方法滑动的时候,是控件里面的内容滑动,而非控件自己滑动。


例如:

  <LinearLayout 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"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="55555555555555" 

       android:background="@android:color/black" android:textColor="@android:color/white"/>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>


public class MainActivity extends Activity {
private TextView tv1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1=(TextView) findViewById(R.id.t1);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tv1.scrollTo(-200, 0);
}
});
}
}


点击Button时滑动的是TextView控件里面的字体“55555555555555”,而非TextView

最后的效果是



字体“55555555555555”有滑动,而TextView并没有动,看黑色背景就知道了,所以说scrollTo滑动的是控件本身的内容或者说是控制监督子集,而非控件本身


你可能感兴趣的:(对Scroller中的startScroll方法的理解)