I continued to look into Android's new Overscroll functionality introduced in Gingerbread and discovered some more interesting things. The functionality to make a a view scroll beyond its limits and then bounce back (almost exactly like iOS) is sort of built into the framework, but just hidden. I'm not sure exactly why it has been built like it has been, but I will give a few guesses after an explanation of what is actually going on, but first: DEMO!
So What's There and What Isn't?
I'm glad you asked… If we look into the ViewConfiguration class' source we find two variables of interest: OVERSCROLL_DISTANCE and OVERFLING_DISTANCE. These two variables tell the framework how much a view should be able to scroll beyond its limits. They are hard coded in and there are no methods available to set your own custom ones. OVERSCROLL_DISTANCE is set to 0 (!?) and OVERFLING_DISTANCE is set to 4.
For those that don't know, the ViewConfiguration class holds a set of values that Android uses to store the default timeouts / distances etc for certain UI behaviours. It also does some internal scaling and calculations based on screen density etc. If you're interested, have a look at the source
So with OVERSCROLL_DISTANCE set to 0, the view will never move beyond its limits, but you can do something fairly simple to achieve this behaviour.
In complicated terms, just extend the view you wish to use normally, (e.g. ListView) and override the overScrollBy method. Then within theoverScrollBy method body, simply call the super.overScrollBy but with your own values for maxOverScrollX and/or maxOverScrollY. If you're gonna do this, make sure you scale your values based on the screen density.
Confused? Have a code sample:
And now just use that custom view wherever you would normally have used the standard view!
public class BounceListView extends ListView
{
private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;
private Context mContext;
private int mMaxYOverscrollDistance;
public BounceListView(Context context)
{
super(context);
mContext = context;
initBounceListView();
}
public BounceListView(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = context;
initBounceListView();
}
public BounceListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
mContext = context;
initBounceListView();
}
private void initBounceListView()
{
//get the density of the screen and do some maths with it on the max overscroll distance
//variable so that you get similar behaviors no matter what the screen size
final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
final float density = metrics.density;
mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
}
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent)
{
//This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);
}
}
As promised, some guesses as to why this is happening. My first thought is that the developers had no intention of exposing this functionality and that it is simply meant to be used for the small springback you get after an overfling (remember where OVERFLING_DISTANCE was set to 4). But then is that was the case why set OVERSCROLL_DISTANCE to 0, why not just not include it if that was the case? Maybe they are planning something in the future? But if it was intended to be used, then why not create methods that let you set the overscroll distances for your views? Who knows…
分享到:
相关推荐
AndroidS开发环境搭建---在myeclipse里面开发android环境的大件,包括安装android开发包,和模拟器。 --IPHONE开发环境搭建 OBject-c开发环境的搭建,Xcode和ios sdk和模拟器的安装,还有我使用的是Vmware里面安装...
安卓app开发项目-我也模仿了Path效果,效果更接近iphone(源码).zip安卓app开发项目-我也模仿了Path效果,效果更接近iphone(源码).zip安卓app开发项目-我也模仿了Path效果,效果更接近iphone(源码).zip安卓app开发...
自定义声音波纹,适合作录音应用的开发,模仿iPhone录音效果
Android仿iphone-气泡短信-DEMO.zip
谢伟-基于iPhone开发实践 谢伟-基于iPhone开发实践 谢伟-基于iPhone开发实践
安卓app开发项目-仿iphone的listview下拉更新(源码).zip
Android高级应用源码-模仿Iphone时间滚轮.zip
UI第一章:无限互联iOS开发教程课件-- iPhone开发入门
知易Cocos2D-iPhone开发教程-08知易Cocos2D-iPhone开发教程-08
安卓app开发项目-仿iPhone风格对话框示例,Jar包及源码(源码).zip
安卓app开发项目-仿Iphone风格翻页控件,源码及Jar包(源码).zip
android tabhost --android UI源码 Android Txt文本阅读器源码 Android Widget快捷拨号程序源码 Android 仓库管理系统源码 Android 仿ES界面文件浏览器源码 Android 仿iPhoneQQ气泡聊天样式源码 Android 仿QQ多级...
iOS开发------程序实现国际化Localizable - iPhone手机开发技术文章 - 红黑联盟1
Android tabs at bottom! This repository contains sample of setting android TabLayout at bottom and showing Fragment embedded in ViewPager. The magic happens very simply. You need to arrange them ...
②iphone开发指南-iPhone_Development_Guide
苹果iOS开发中文电子书推荐:5--iPhone开发秘籍中文实践教程PART1
苹果iOS开发中文电子书推荐:4--iPhone开发基础中文教程.part1
安卓开发-模仿Iphone时间滚轮.zip
安卓开发-仿iphone 气泡短信 DEMO.zip
Android 小項目之---Iphone拖动图片特效 (附源码) 一个完整的新浪微博客户端android版OAuth认证示例 超爽的android抽屉效果 65个Android实例教程汇总 基本控件及基本动画效果dem 2011android面试题目及其答案大全...