Android滑屏 mScrollX mScrollY scrollTo() scrollBy()
首先 ,我们必须明白在Android View视图是没有边界的,Canvas是没有边界的,只不过我们通过绘制特定的View时对
Canvas对象进行了一定的操作,例如 : translate(平移)、clipRect(剪切)等,以便达到我们的对该Canvas对象绘制的要求 ,
我们可以将这种无边界的视图称为“视图坐标”-----它不受物理屏幕限制。通常我们所理解的一个Layout布局文件只是该视
图的显示区域,超过了这个显示区域将不能显示到父视图的区域中 ,对应的,我们可以将这种无边界的视图称为“布局坐标”
------ 父视图给子视图分配的布局(layout)大小。 而且, 一个视图的在屏幕的其实坐标位于视图坐标起始处,如下图所示。
这么来说吧 ,世界本是无边无界的,可是我们的眼睛我们的心约束了我们所看到的“世界” 。
如下所示:
黑色框框表示该子视图的布局坐标, 褐色框框表示该子视图的视图坐标--该坐标是无限的,超过了父视图给子视图
规定的区域后,不再显示该超出内容。
那么下面的问题就是:如何将我们的视图的任意坐标能显示到该视图的中心坐标上呢? 由于该布局位置是只能显示特定的
一块视图内容 ,因此我们需要通过scrollTo()或者scrollBy()方法将我们期望的视图“滚动”至布局坐标上。
在View.java中提供了了如下两个变量以及相应的属性方法去读取滚动值 ,如下: View.java类中
01
|
/**
|
02
|
* The offset, in pixels, by which the content of this view is scrolled
|
03
|
* horizontally.
|
04
|
* {@hide}
|
05
|
*/
|
06
|
protected int mScrollX; //该视图内容相当于视图起始坐标的偏移量 , X轴 方向
|
07
|
/**
|
08
|
* The offset, in pixels, by which the content of this view is scrolled
|
09
|
* vertically.
|
10
|
* {@hide}
|
11
|
*/
|
12
|
protected int mScrollY; //该视图内容相当于视图起始坐标的偏移量 , Y轴方向
|
13
|
14
|
/**
|
15
|
* Return the scrolled left position of this view. This is the left edge of
|
16
|
* the displayed part of your view. You do not need to draw any pixels
|
17
|
* farther left, since those are outside of the frame of your view on
|
18
|
* screen.
|
19
|
*
|
20
|
* @return The left edge of the displayed part of your view, in pixels.
|
21
|
*/
|
22
|
public final int getScrollX() {
|
23
|
return mScrollX;
|
24
|
}
|
25
|
26
|
/**
|
27
|
* Return the scrolled top position of this view. This is the top edge of
|
28
|
* the displayed part of your view. You do not need to draw any pixels above
|
29
|
* it, since those are outside of the frame of your view on screen.
|
30
|
*
|
31
|
* @return The top edge of the displayed part of your view, in pixels.
|
32
|
*/
|
33
|
public final int getScrollY() {
|
34
|
return mScrollY;
|
35
|
}
|
注意,所谓的“by which the content of this view is scrolled”表示该偏移量只针对于该View中onDraw()方法里的
具体内容实现,而不针对背景图片等 。具体原因可参考<Android中View绘制流程以及invalidate()等相关方法分析>
提示:下文中提到的当前视图内容是在绘制在布局坐标处的内容。
public void scrollTo(int x, int y)
说明:在当前视图内容偏移至(x , y)坐标处,即显示(可视)区域位于(x , y)坐标处。
方法原型为: View.java类中
01
|
/**
|
02
|
* Set the scrolled position of your view. This will cause a call to
|
03
|
* {@link #onScrollChanged(int, int, int, int)} and the view will be
|
04
|
* invalidated.
|
05
|
* @param x the x position to scroll to
|
06
|
* @param y the y position to scroll to
|
07
|
*/
|
08
|
public void scrollTo( int x, int y) {
|
09
|
//偏移位置发生了改变
|
10
|
if (mScrollX != x || mScrollY != y) {
|
11
|
int oldX = mScrollX;
|
12
|
int oldY = mScrollY;
|
13
|
mScrollX = x; //赋新值,保存当前便宜量
|
14
|
mScrollY = y;
|
15
|
//回调onScrollChanged方法
|
16
|
onScrollChanged(mScrollX, mScrollY, oldX, oldY);
|
17
|
if (!awakenScrollBars()) {
|
18
|
invalidate(); //一般都引起重绘
|
19
|
}
|
20
|
}
|
21
|
}
|
public voidscrollBy(int x, int y)
说明:在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位。
方法原型为: View.java类中
01
|
/**
|
02
|
* Move the scrolled position of your view. This will cause a call to
|
03
|
* {@link #onScrollChanged(int, int, int, int)} and the view will be
|
04
|
* invalidated.
|
05
|
* @param x the amount of pixels to scroll by horizontally
|
06
|
* @param y the amount of pixels to scroll by vertically
|
07
|
*/
|
08
|
// 看出原因了吧 。。 mScrollX 与 mScrollY 代表我们当前偏移的位置 , 在当前位置继续偏移(x ,y)个单位
|
09
|
public void scrollBy( int x, int y) {
|
10
|
scrollTo(mScrollX + x, mScrollY + y);
|
11
|
}
|
第一个小Demo非常简单 ,大家重点理解与掌握scrollTo() 与 scrollBy()函数的用法和区别。
第二个小Demo则有了Launcher的模样,能够左右切换屏幕 。实现功能如下: 采用了一个自定义ViewGroup,该ViewGroup
对象包含了3个LinearLayout子视图,并且以一定的布局坐标(由layout()方法指定)显示在ViewGroup上。 接下来,即可调用该
ViewGroup对象的scrollTo或者scrollBy()方法切换指定视图内容了,即切换屏幕。 呵呵 ,挺好玩的吧 。
如果对View绘制流程不懂的,可以参考我的这篇博客<Android中View绘制流程以及invalidate()等相关方法分析> 。
Android滑屏 mScrollX mScrollY scrollTo() scrollBy()相关推荐
- android滑屏两三事
一个滑屏Demo,所包含的知识点: Android相关: How to create a ViewGroup Usage of Scroller OnTouchEvent & onInterc ...
- android 滑屏功能,Android 滑屏效果实现方式汇总
Android的滑屏,最近在研究这个东西,发现新浪微博的客户端.腾讯微博的客户端都有这个功能.目前到今天为止,腾讯和微博的客户端都是使用viewPager控件写的,可能重写了一些地方.而腾讯老版本的q ...
- android滑屏效果,Android使用ViewFlipper和GestrueDetector共同实现滑屏效果实例
本文实例讲述了Android使用ViewFlipper和GestrueDetector共同实现滑屏效果.分享给大家供大家参考,具体如下: 关于GestureDetector的相关知识,前面已经介绍过了 ...
- android滑屏设计规范,Android使用ViewFlipper和GestrueDetector共同实现滑屏效果实例
本文实例讲述了Android使用ViewFlipper和GestrueDetector共同实现滑屏效果.分享给大家供大家参考,具体如下: 关于GestureDetector的相关知识,前面已经介绍过了 ...
- Android滑屏速度,让安卓手机流畅度瞬间飙升60%,这个小技巧厉害了
原标题:让安卓手机流畅度瞬间飙升60%,这个小技巧厉害了 6月份各大国产手机品牌相继推出自己的旗舰机,华为小米等厂商都推出自己的旗舰机,就连最近几年一直主打中端手机的ov厂商也推出了顶级旗舰机,vov ...
- android滑屏设计规范,安卓设计如何实现滑动屏幕出现如图的效果
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 import java.util.ArrayList; import android.content.Context; import android.gr ...
- android滑屏的设计思路,安卓设计如何实现滑动屏幕出现如图的效果
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 import java.util.ArrayList; import android.content.Context; import android.gr ...
- android滑屏轨迹运动,Android Animation实现元素在屏幕上按照指定轨迹运动,以及出现NullPointerException的解决方案...
因项目需要,在Android中实现了一个动画,当在Activity中点击特定按钮时,会在屏幕上添加一个ImageView,并按照指定的起点.终点,沿着特定的轨迹运动(例如直线). 实现方法 实现思路是 ...
- 手机桌面左右滑屏不成功问题log分析
问题背景 滑动测试,单手握持测试机,大拇指在测试机桌面做左右滑屏操作,偶现滑屏不成功. 问题分析步骤 (1)首先根据"视频"或者"log中关于问题现象的关键log&quo ...
最新文章
- SDUT-3378_数据结构实验之查找六:顺序查找
- Llinux 磁盘配额的搭建和常规问题解答
- java js中 function函数报错_浅析JS中对函数function的理解(基础篇)
- linux+硬盘rd5,Raid磁盘阵列
- ICCV 2019 | 厦大提出快速NAS检索方法,四小时搜索NN结构
- shell中设置文字输出的颜色及字体格式
- centos 安装mysql5.6.20_Linux学习15-CentOS安装mysql5.6环境
- iOS 常见的JS与iOS交互的需求与解决方案
- 学维修电脑要多久_学古筝难吗?古筝要多久才能学会?
- nginx 5xx 状态码分析
- 人工智能商业实战应用:金融知识图谱构建与实战【企业内训现场实录】
- 基于Lagrange-Newton法的SQP局部算法python实现
- 中学生应具备的良好的学习习惯
- 苹果推送通知办事教程 Apple Pushnb
- C语言实现任意函数求不定积分
- 微众银行大数据爽约? 回应:这是一种误解
- 软件工程中五种常用的软件开发模型整理
- 小白c++ C6386缓冲溢出问题请教
- 关于微信小程序开发个人总结
- GAT原论文阅读笔记
热门文章
- PHP面向对象(OOP)编程入门教程
- C#中读取带有Xmlns命名空间的XML文件
- 集群环境下的Session共享
- linux下如何产生core,调试core
- shopt_Linux命令
- protel99se 问题汇总(不定期更新)
- |Vijos|贪心|P1414 Dejected Birthday-盗窃
- 7.2 DOM方法(以动态方式实时创建标记,实质在改变DOM节点树)
- No output streams registered, so nothing to execute
- linux下修改rm命令防止误删除