对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滑动的是控件本身的内容或者说是控制监督子集,而非控件本身


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值