View的getScrollX(),getScrollY(),getScrollBy(),getScrollTo()


图上面,褐色的框,其实就是我们眼睛看到的手机界面,就是一个窗口。

而绿色的长方体呢,就是一块可以左右拉动的幕布啦,其实也就是我们要显示在窗口上面的内容,它其实是可以很大的,大到无限大,只是没在窗口中间的,所以我们就看不到。

而getScrollX 其实获取的值,就是这块 幕布在窗口左边界时候的值了,而幕布上面哪个点是原点(0,0)呢?就是初始化时内容显示的位置。

所以当我们将幕布往右推动的时候,幕布在窗口左边界的值就会在0的左边(-100),而向左推动,则其值会是在0的右边(100)。

下面以一个实际例子来看一下。

我们在一个LinearLayout 里面定义了三个TextView 来显示 getScrollX() 的值,三个ImageView来显示图片, 都是指向同一张图片,布局如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#000000"  
  6.     android:orientation="vertical" >  
  7.     <TextView  
  8.         android:id="@+id/textView"  
  9.         android:textColor="#FFFFFF"  
  10.         android:textSize="16sp"  
  11.         android:layout_height="wrap_content"   
  12.         android:layout_width="match_parent"/>  
  13.     <ImageView   
  14.         android:id="@+id/scrollImageView"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:src="@drawable/scroll_testing"  
  18.         android:contentDescription="Testing Scrolling"/>  
  19.     <TextView  
  20.         android:id="@+id/textViewToRight"  
  21.         android:textColor="#FFFFFF"  
  22.         android:textSize="16sp"  
  23.         android:layout_height="wrap_content"   
  24.         android:layout_width="match_parent"/>  
  25.     <ImageView   
  26.         android:id="@+id/scrollImageViewToRight"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:src="@drawable/scroll_testing"  
  30.         android:contentDescription="Testing Scrolling"/>  
  31.     <TextView  
  32.         android:id="@+id/textViewToLeft"  
  33.         android:textColor="#FFFFFF"  
  34.         android:textSize="16sp"  
  35.         android:layout_height="wrap_content"   
  36.         android:layout_width="match_parent"/>  
  37.     <ImageView   
  38.         android:id="@+id/scrollImageViewToLeft"  
  39.         android:layout_width="wrap_content"  
  40.         android:layout_height="wrap_content"  
  41.         android:src="@drawable/scroll_testing"  
  42.         android:contentDescription="Testing Scrolling"/>  
  43.   
  44. </LinearLayout>  

然后我们在Activity 中,分别对下面两张ImageView 进行 scrollTo 操作,然后获取其getScrollX()  的值,放到对应的TextView 上面,其代码如下:

[java]  view plain copy
  1. public class ScrollActivity extends Activity{  
  2.   
  3.     protected void onCreate(Bundle savedInstanceState){  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.scroll_layout);  
  6.           
  7.         ImageView imageView = (ImageView) findViewById(R.id.scrollImageView);  
  8.         TextView textView = (TextView) findViewById(R.id.textView);  
  9.         textView.setText("getScrollX() = " + imageView.getScrollX());  
  10.           
  11.         ImageView imageViewToRight = (ImageView) findViewById(R.id.scrollImageViewToRight);  
  12.         imageViewToRight.scrollTo(-1000);  
  13.         TextView textViewToRight = (TextView) findViewById(R.id.textViewToRight);  
  14.         textViewToRight.setText("getScrollX() = " + imageViewToRight.getScrollX());  
  15.           
  16.         ImageView imageViewToLeft = (ImageView) findViewById(R.id.scrollImageViewToLeft);  
  17.         imageViewToLeft.scrollTo(1000);  
  18.         TextView textViewToLeft = (TextView) findViewById(R.id.textViewToLeft);  
  19.         textViewToLeft.setText("getScrollX() = " + imageViewToLeft.getScrollX());  
  20.     }  
  21. }  

下面是运行后的效果

可以看到,正如上面所说的,向右滚动的时候,等于是把背后的幕布向右推动,使得没有内容的幕布(X < 0)显示出来, 而向左滚动,则是把幕左向左推动,让右边的内容(X > 0)移到窗口的左边缘上。

在View上面还一个叫ScrollBy的函数,跟ScrollTo的区别在于,ScrollTo 是 到那个位置,ScrollBy 是经过这段位置,这个从英文的To 跟 By 来理解就很简单了。getScrollY(),getScrollX()获取的是以新幕布为准距离屏幕远点的x,y 轴的距离。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值