关于View的ScrollTo, getScrollX 和 getScrollY
当利用 Scroller 去滑动屏幕或者扩展 ScrollView 的时候,总是会用到 getScrollX 和 getScrollY 去获取当前View 滑动到的位置,那么getScrollX() 和 getScrollY() 获取的究竟是什么呢?
因为getScrollX 和 getScrollY 本质 上是一样的东西。以下仅仅说明一下getScrollX, 通常是在屏幕上面左右划动的时候会去获取这个值。
请看下图:
图上面,褐色的框,事实上就是我们眼睛看到的手机界面。就是一个窗体。
而绿色的长方体呢,就是一块能够左右拉动的幕布啦,事实上也就是我们要显示在窗体上面的内容,它事实上是能够非常大的,大到无限大,仅仅是没在窗体中间的,所以我们就看不到。
而getScrollX 事实上获取的值,就是这块 幕布在窗体左边界时候的值了,而幕布上面哪个点是原点(0,0)呢?就是初始化时内容显示的位置。
所以当我们将幕布往右推动的时候,幕布在窗体左边界的值就会在0的左边(-100),而向左推动,则其值会是在0的右边(100)。
以下以一个实际样例来看一下。
我们在一个LinearLayout 里面定义了三个TextView 来显示 getScrollX() 的值,三个ImageView来显示图片, 都是指向同一张图片,布局例如以下:
<?xml version="1.0" encoding="utf-8"?
><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:orientation="vertical" >
<TextView android:id="@+id/textView" android:textColor="#FFFFFF" android:textSize="16sp" android:layout_height="wrap_content" android:layout_width="match_parent"/> <ImageView android:id="@+id/scrollImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/scroll_testing" android:contentDescription="Testing Scrolling"/> <TextView android:id="@+id/textViewToRight" android:textColor="#FFFFFF" android:textSize="16sp" android:layout_height="wrap_content" android:layout_width="match_parent"/> <ImageView android:id="@+id/scrollImageViewToRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/scroll_testing" android:contentDescription="Testing Scrolling"/> <TextView android:id="@+id/textViewToLeft" android:textColor="#FFFFFF" android:textSize="16sp" android:layout_height="wrap_content" android:layout_width="match_parent"/> <ImageView android:id="@+id/scrollImageViewToLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/scroll_testing" android:contentDescription="Testing Scrolling"/> </LinearLayout>
然后我们在Activity 中,分别对以下两张ImageView 进行 scrollTo 操作。然后获取其getScrollX() 的值,放到相应的TextView 上面,其代码例如以下:
public class ScrollActivity extends Activity{ protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.scroll_layout); ImageView imageView = (ImageView) findViewById(R.id.scrollImageView); TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("getScrollX() = " + imageView.getScrollX()); ImageView imageViewToRight = (ImageView) findViewById(R.id.scrollImageViewToRight); imageViewToRight.scrollTo(-100, 0); TextView textViewToRight = (TextView) findViewById(R.id.textViewToRight);
textViewToRight.setText("getScrollX() = " + imageViewToRight.getScrollX()); ImageView imageViewToLeft = (ImageView) findViewById(R.id.scrollImageViewToLeft); imageViewToLeft.scrollTo(100, 0); TextView textViewToLeft = (TextView) findViewById(R.id.textViewToLeft);
textViewToLeft.setText("getScrollX() = " + imageViewToLeft.getScrollX()); }}
以下是执行后的效果
能够看到。正如上面所说的,向右滚动的时候。等于是把背后的幕布向右推动,使得没有内容的幕布(X < 0)显示出来, 而向左滚动,则是把幕左向左推动,让右边的内容(X > 0)移到窗体的左边缘上。
在View上面还一个叫ScrollBy的函数,跟ScrollTo的差别在于。ScrollTo 是 到那个位置,ScrollBy 是经过这段位置,这个从英文的To 跟 By 来理解就非常easy了。
关于View的ScrollTo, getScrollX 和 getScrollY的更多相关文章
- 图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
https://blog.csdn.net/bigconvience/article/details/26697645 Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向 ...
- View:Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()的理解
Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向想反.提供了 getLeft(), getTop(), getBottom(), getRight() 这些API来获取 ...
- [学习总结]1、View的scrollTo 和 scrollBy 方法使用说明和区别
参考资料:http://blog.csdn.net/vipzjyno1/article/details/24577023 非常感谢这个兄弟! 先查看这2个方法的源码: scrollTo: 1 /** ...
- getRawX、getRawY与getX、getY、getScrollX、getScrollY,getTop,getLeft区别
这篇文章写的不错,Mark一下 http://blog.csdn.net/sinat_29912455/article/details/51073537
- 你真的了解View的坐标吗?
闲聊 View,对我们来说在熟悉不过了,从接触 Android 开始,我们就一直在接触 View,界面当中到处都是 View,比如我们经常用到的 TextView,Button,LinearLayou ...
- Android学习笔记(四)之碎片化Fragment实现仿人人客户端的侧边栏
其实一种好的UI布局,可以使用户感到更加的亲切与方便.最近非常流行的莫过于侧边栏了,其实我也做过很多侧边栏的应用,但是那些侧边栏的使用我 都不是很满意,现在重新整理,重新写了一个相对来说我比较满意的侧 ...
- getX,getY,getScrollX,getScrollY,ScrollTo(),ScrollBy()辨析
前言:前两天看了自定义控件,其中有一些东西我觉得有必要深入理解一下 以下图为例: getX(),getY()返回的是触摸点A相对于view的位置 getRaw(),getRawY()返回的是触摸点B相 ...
- Android View 的事件体系
android 系统虽然提供了很多基本的控件,如Button.TextView等,但是很多时候系统提供的view不能满足我们的需求,此时就需要我们根据自己的需求进行自定义控件.这些控件都是继承自Vie ...
- Android -- View移动的六种方法
layout() 如果你将滑动后的目标位置的坐标传递给layout(),这样子就会把view的位置给重新布置了一下,在视觉上就是view的一个滑动的效果. public class DragView ...
随机推荐
- TCP排查常用命令
1.查看TCP连接状态命令 netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t" ...
- git的常用命令。。
git的常用命令.. git help <command> 显示command的help git show 显示某次提交的内容 git show $id git co -- <f ...
- 运行Django项目指定IP和端口
默认IP和端口 python manage.py runserver 指定端口: python manage.py runserver 192.168.12.12:8080 此时会报错,我们需要修改配 ...
- A - Petya and Strings
Problem description Little Petya loves presents. His mum bought him two strings of the same size for ...
- 关于如何将_variant_t型转化为int型和string类型
1)将_variant_t型转化为int型 关于将_variant_t型转化为int型,网上有好多好多参考,但好多都很复杂并且还不对,其实整个转化过程就只一行代码可以搞定: _variant_t a; ...
- 利用POPAnimatableProperty属性来实现动画倒计时
POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"countdown" initial ...
- CSS清除浮动_清除float浮——详解overflow:hidden 与clear:both属性
最近刚好碰到这个问题,看完这个就明白了.写的很好,所以转载了! CSS清除浮动_清除float浮动 CSS清除浮动方法集合 一.浮动产生原因 - TOP 一般浮动是什么情况呢?一般是一个盒子里 ...
- php正则表达式应用
正则表达式 1.替换“/\d/”,“#”,$str:正则表达式\d 数字,替换为#,字符串 $str = "2hello 5li 6lei"; echo preg_replace( ...
- hibernate_06_单表操作_组件属性
什么是组件属性? 比如address是students的其中一个属性,而address又有三个属性:邮编.电话和地址.address就是hibernate的组件属性. 首先建立Address类: pa ...
- [实战经验][SQL Sever 2008 (R)解决方法累积
SQL Sever 2008 (R)的安装图解及配置 http://www.soft6.com/v9/2009/jcsj_1030/115821.html 产品密钥,选择“输入产品密钥”,输入:PTT ...