Android中的滑屏功能的原理是很值得我们去研究的,在知道这两个原理之前,有必要先说说View的两个重要方法,它们就是scrollTo 和scrollBy。
Android View视图是没有边界的,Canvas是没有边界的,只不过我们通过绘制特定的View时对
Canvas对象进行了一定的操作,例如 : translate(平移)、clipRect(剪切)等,以便达到我们的对该Canvas对象绘制的要求 ,
我们可以将这种无边界的视图称为“视图坐标”-----它不受物理屏幕限制。通常我们所理解的一个Layout布局文件只是该视
图的显示区域,超过了这个显示区域将不能显示到父视图的区域中 ,对应的,我们可以将这种无边界的视图称为“布局坐标”
------ 父视图给子视图分配的布局(layout)大小。 而且, 一个视图的在屏幕的其实坐标位于视图坐标起始处,如下图所示。
这么来说吧 ,世界本是无边无界的,可是我们的眼睛我们的心约束了我们所看到的“世界” 。
如下所示:

黑色框框表示该子视图的布局坐标, 褐色框框表示该子视图的视图坐标--该坐标是无限的,超过了父视图给子视图
规定的区域后,不再显示该超出内容。
那么下面的问题就是:如何将我们的视图的任意坐标能显示到该视图的中心坐标上呢? 由于该布局位置是只能显示特定的
一块视图内容 ,因此我们需要通过scrollTo()或者scrollBy()方法将我们期望的视图“滚动”至布局坐标上。
在View.java中提供了了如下两个变量以及相应的属性方法去读取滚动值 ,如下: View.java类中
02 |
*
The offset, in pixels, by which the content of this view is scrolled |
06 |
protected int mScrollX; //该视图内容相当于视图起始坐标的偏移量
, X轴 方向 |
08 |
*
The offset, in pixels, by which the content of this view is scrolled |
12 |
protected int mScrollY; //该视图内容相当于视图起始坐标的偏移量
, Y轴方向 |
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 |
20 |
*
@return The left edge of the displayed part of your view, in pixels. |
22 |
public final int getScrollX()
{ |
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. |
31 |
*
@return The top edge of the displayed part of your view, in pixels. |
33 |
public final int getScrollY()
{ |
注意,所谓的“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类中
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 |
05 |
*
@param x the x position to scroll to |
06 |
*
@param y the y position to scroll to |
08 |
public void scrollTo(int x, int y)
{ |
10 |
if (mScrollX
!= x || mScrollY != y) { |
13 |
mScrollX
= x; //赋新值,保存当前偏移量 |
16 |
onScrollChanged(mScrollX,
mScrollY, oldX, oldY); |
17 |
if (!awakenScrollBars())
{ |
18 |
invalidate(); //一般都引起重绘 |
public void scrollBy(int
x, int y)
说明:在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位。
方法原型为: View.java类中
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 |
05 |
*
@param x the amount of pixels to scroll by horizontally |
06 |
*
@param y the amount of pixels to scroll by vertically |
08 |
//
看出原因了吧 。。 mScrollX 与 mScrollY 代表我们当前偏移的位置 , 在当前位置继续偏移(x ,y)个单位 |
09 |
public void scrollBy(int x, int y)
{ |
10 |
scrollTo(mScrollX
+ x, mScrollY + y); |
第一个小Demo非常简单 ,大家重点理解与掌握scrollTo() 与 scrollBy()函数的用法和区别。
第二个小Demo则有了Launcher的模样,能够左右切换屏幕 。实现功能如下: 采用了一个自定义ViewGroup,该ViewGroup
对象包含了3个LinearLayout子视图,并且以一定的布局坐标(由layout()方法指定)显示在ViewGroup上。 接下来,即可调用该
ViewGroup对象的scrollTo或者scrollBy()方法切换指定视图内容了,即切换屏幕。 呵呵 ,挺好玩的吧 。
如果对View绘制流程不懂的,可以参考我的这篇博客<Android中View绘制流程以及invalidate()等相关方法分析>
。
截图如下:

自定义ViewGroup如下:
01 |
//自定义ViewGroup
, 包含了三个LinearLayout控件,存放在不同的布局位置,通过scrollBy或者scrollTo方法切换 |
02 |
public class MultiViewGroup extends ViewGroup
{ |
04 |
private Context
mContext; |
06 |
private static String
TAG = "MultiViewGroup"; |
08 |
public MultiViewGroup(Context
context) { |
14 |
public MultiViewGroup(Context
context, AttributeSet attrs) { |
15 |
super(context,
attrs); |
21 |
//
初始化3个 LinearLayout控件 |
22 |
LinearLayout
oneLL = new LinearLayout(mContext); |
23 |
oneLL.setBackgroundColor(Color.RED); |
26 |
LinearLayout
twoLL = new LinearLayout(mContext); |
27 |
twoLL.setBackgroundColor(Color.YELLOW); |
30 |
LinearLayout
threeLL = new LinearLayout(mContext); |
31 |
threeLL.setBackgroundColor(Color.BLUE); |
37 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{ |
39 |
Log.i(TAG, "---
start onMeasure --"); |
42 |
int width
= MeasureSpec.getSize(widthMeasureSpec); |
43 |
int height
= MeasureSpec.getSize(heightMeasureSpec); |
44 |
setMeasuredDimension(width,
height); |
46 |
int childCount
= getChildCount(); |
47 |
Log.i(TAG, "---
onMeasure childCount is -->" +
childCount); |
48 |
for (int i
= 0;
i < childCount; i++) { |
49 |
View
child = getChildAt(i); |
51 |
child.measure(MultiScreenActivity.screenWidth,
MultiScreenActivity.scrrenHeight); |
57 |
protected void onLayout(boolean changed, int l, int t, int r, int b)
{ |
58 |
//
TODO Auto-generated method stub |
59 |
Log.i(TAG, "---
start onLayout --"); |
60 |
int startLeft
= 0; //
每个子视图的起始布局坐标 |
61 |
int startTop
= 10; //
间距设置为10px 相当于 android:marginTop= "10px" |
62 |
int childCount
= getChildCount(); |
63 |
Log.i(TAG, "---
onLayout childCount is -->" +
childCount); |
64 |
for (int i
= 0;
i < childCount; i++) { |
65 |
View
child = getChildAt(i); |
66 |
child.layout(startLeft,
startTop, |
67 |
startLeft
+ MultiScreenActivity.screenWidth, |
68 |
startTop
+ MultiScreenActivity.scrrenHeight); |
69 |
startLeft
= startLeft + MultiScreenActivity.screenWidth ; //校准每个子View的起始布局位置 |
70 |
//三个子视图的在屏幕中的分布如下
[0 , 320] / [320,640] / [640,960] |
PS :大家可以分别给这几个LinearLayout试着添加几个子View,例如TextView, Button等 。
至于Launcher上滑屏功能的实现,我尝试着去掌握,可能天资愚钝吧,对Scoller类很是感冒,现今还没有掌握好,不过在此
给大家推荐几个不错的学习资源 。
1、 Scoller类介绍:android
中文 api (64) —— Scroller
2、相关资源汇总:http://blog.csdn.net/dellheng/article/details/7164275
3,launcher修改--左右滑动屏幕切换源码追踪
- 【Android 界面效果16】关于android四大组件的总结
Android四大组件:Activity.Service.Broadcast receiver.Content provider 在Android中,一个应用程序可以使用其它应用程序的组件,这是And ...
- 【Android 界面效果26】listview android:cacheColorHint,android:listSelector属性作用
ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉, 原因是,滚动时,列表里面的view重绘时,用的依 ...
- 【Android 界面效果31】Android--侧滑菜单应用的实现
侧滑菜单应用现在非常多,而且实现方式也多种多样.通过在网上的多方查找,我找到郭霖少侠的这篇文章:http://blog.csdn.net/guolin_blog/article/details/874 ...
- 【Android 界面效果17】Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
记得我之前参与开发过一个华为的项目,要求程序可以支持好几种终端设备,其中就包括Android手机和Android Pad.然后为了节省人力,公司无节操地让Android手机和Android Pad都由 ...
- 【Android 界面效果25】android中include标签的使用
在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include ...
- 【Android 界面效果21】Android ViewPager使用详解
这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api.而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等.那如 ...
- 【Android 界面效果18】Android软件开发之常用系统控件界面整理
[java] view plaincopyprint? <span style="font-size:18px">1.文本框TextView TextView的作用 ...
- 【Android 界面效果47】RecyclerView详解
RecylerView作为 support-library发布出来,这对开发者来说绝对是个好消息.因为可以在更低的Android版本上使用这个新视图.下面我们看如何获取 RecylerView.首先打 ...
- 【Android 界面效果45】ViewPager源码分析
ViewPager概述: Layout manager that allows the user to flip left and right through pages of data. You s ...
随机推荐
- MSSQL手札四 MSSQL的函数
和oracle一样,sql也可以自己定义函数 一个返回值,引用DEMO如下: 编写一个函数,该函数,可以通过输入借书时间来判断是否到期,当借阅时间大于30天,返回已经过期:否则返回还未到期. CREA ...
- HDU 1392 Surround the Trees 构造凸包
又是一道模板题 #include <iostream> #include <cstring> #include <cstdlib> #include <cst ...
- junit4学习(Annotation)
在一个测试类中,所有被@Test注解修饰的public,void方法都是testcase,可以被JUNIT执行. @Retention(value=RUNTIME) @Target(value=MET ...
- C#扫描仪编程、条形码识别编程资料
扫描仪编程资料:http://www.cnblogs.com/wubh/archive/2011/11/07/2239178.html 图片条形码识别资料:http://www.codeproject ...
- js 控制DIV 预览打印
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- 国内外从事CV相关的企业[转]
提示:本文为笔者原创,转载请注明出处:blog.csdn.net/carson2005 经常碰到朋友问我国内从事计算机视觉(CV)领域的公司的发展情况,产品情况,甚至找工作等问题,这里,我给出自己收集 ...
- linux tail命令的使用方法详解 (转载)
本文介绍Linux下tail命令的使用方法.linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新 ...
- IIS配置(安装IIS、.Net、更改IIS Log目录位置)
#安装IIS..NetFramework 3.5 Import-Module servermanager Get-WindowsFeature web-* | ? {$_.Name -ne " ...
- .Net连接到SAP【转载】
刚开始接触SAP了,感觉很陌生,清一色的TCode,不过里面的功能确实强大,不得不佩服啊,之前我一直是搞WinForm和WebForm的,现在能够接触到SAP那我还是想多学习一下,看了一下ABAP的语 ...
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分 解决方案
但web启用了md5加密后 有可能出现这样的错误 This implementation is not part of the Windows Platform FIPS validated cryp ...