`
ithinkfeed
  • 浏览: 94185 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Android开发之-类似iPhone弹性效果的BounceListView

 
阅读更多

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…

分享到:
评论
3 楼 quliangjun 2012-12-24  
上向或者向下滑动时,最上一个item 或者最下一个item 总是闪动一会。
2 楼 lkf871224 2011-10-18  
2.2一下是不行的!!!
1 楼 yang668 2011-10-14  
it's very good,

相关推荐

    Android开发环境搭建--IPHONE开发环境搭建

    AndroidS开发环境搭建---在myeclipse里面开发android环境的大件,包括安装android开发包,和模拟器。 --IPHONE开发环境搭建 OBject-c开发环境的搭建,Xcode和ios sdk和模拟器的安装,还有我使用的是Vmware里面安装...

    Android-自定义声音波纹适合作录音应用的开发模仿iPhone录音效果

    在Android平台上,开发一款录音应用并模仿iPhone录音效果,往往涉及到对用户界面的精心设计以及音频处理技术的应用。本文将详细探讨如何实现自定义声音波纹这一关键元素,以达到类似iPhone录音应用的用户体验。 ...

    Android仿iphone-气泡短信-DEMO.zip

    "Android仿iphone-气泡短信-DEMO.zip" 这个标题表明这是一个针对Android平台的开发项目,其目标是模仿iPhone的气泡短信效果。气泡短信是iOS系统中一种常见的对话界面设计,以气泡的形式展示聊天内容,使用户在视觉上...

    Android仿IOS上拉下拉弹性效果的实例代码

    用过iphone的朋友相信都体验过页面上拉下拉有一个弹性的效果,使用起来用户体验很好;Android并没有给我们封装这样一个效果,我们来看下在Android里如何实现这个效果。先看效果,感觉有些时候还是蛮实用的。 思路:...

    安卓app开发项目-我也模仿了Path效果,效果更接近iphone(源码).zip

    安卓app开发项目-我也模仿了Path效果,效果更接近iphone(源码).zip安卓app开发项目-我也模仿了Path效果,效果更接近iphone(源码).zip安卓app开发项目-我也模仿了Path效果,效果更接近iphone(源码).zip安卓app开发...

    Android类似iphone的选择时间,很有用

    在Android开发中,实现一个类似iPhone的时间选择器可以极大地提升用户体验。这个功能允许用户通过直观的界面来选择特定的时间,通常包括小时和分钟。在iPhone上,这种选择器以一个可滚动的圆环形式呈现,而在Android...

    Android代码-模仿iphone时间滚轮控件源码.zip

    在Android开发中,有时我们可能需要创建一个类似iPhone时间滚轮的控件,来实现用户选择日期、时间或数值的功能。这个"Android代码-模仿iPhone时间滚轮控件源码.zip"文件提供了一种实现这一功能的方法。让我们深入...

    Android应用源码-系统工具类设计安卓源代码(82例).zip

    android tabhost --android UI源码 Android Txt文本阅读器源码 Android Widget快捷拨号程序源码 Android 仓库管理系统源码 Android 仿ES界面文件浏览器源码 Android 仿iPhoneQQ气泡聊天样式源码 Android 仿QQ多级...

    《大话企业级Android开发》--第07部分

    Android系统提供了丰富的API和工具,使得开发者能够创造出与iPhone等其他平台相媲美的界面效果。这包括了自定义视图、动画效果、触摸事件处理等方面的知识。 在控件方面,教程会介绍诸如按钮(Button)、文本框...

    Android-仿Iphone的日期控件

    在Android开发中,为了提供与iOS相似的用户体验,开发者经常需要创建模仿iPhone样式的界面元素。本示例项目“Android-仿Iphone的日期控件”正是为了实现这样的目标,它是一个Android滚动日期选择器控件,允许用户...

    安卓开发-模仿Iphone时间滚轮.zip

    这个“安卓开发-模仿Iphone时间滚轮.zip”文件很可能是包含了一个示例项目或者代码库,用于在Android平台上实现类似于iPhone中的时间选择器滚动效果。 时间滚轮在iOS中通常被称为“Picker View”,它允许用户通过...

    一键生成Android/Android-HD/IOS 多尺寸ICON

    在移动应用开发中,图标(ICON)是用户与应用交互的第一视觉元素,它代表了应用的品牌和功能。针对不同的操作系统,如Android、Android-HD(高清版)和iOS,图标需要适应各自的显示标准和设备分辨率,因此需要制作多...

    Android-仿iPhone风格对话框示例,Jar包及源码.zip

    在当今的移动开发领域中,Android作为全球使用最为广泛的移动操作系统之一,开发人员在进行应用开发时经常会借鉴或模仿其他平台上的设计风格,以提高用户体验。iPhone作为苹果公司的旗舰产品,其界面设计和交互方式...

    安卓app开发项目-仿iphone的listview下拉更新(源码).zip

    本项目"安卓app开发项目-仿iphone的listview下拉更新"是针对这一功能的深入实践,旨在模仿iPhone应用中的下拉刷新效果。这个项目的源码可以帮助开发者了解并掌握如何在Android中实现类似于iOS的下拉刷新功能。 在...

    Android-仿iphone的listview下拉更新(源码).zip

    本项目“仿iphone的listview下拉更新”旨在实现一个在Android平台上模仿苹果iOS风格的ListView组件,使用户在使用Android应用时也能体验到类似iPhone的流畅下拉刷新效果。源码的公开,不仅有利于开发者学习和理解...

    Android--开发--CustomGalleryLikeiPhone(3D相册).rar

    在Android应用开发中,创建一个类似iPhone的3D相册效果可以提升用户体验,使应用程序更加吸引人。"CustomGalleryLikeiPhone(3D相册)"项目正是为了实现这样的功能。这个压缩包文件包含了实现这一效果所需的全部源...

    Android-仿Iphone风格翻页控件,源码及Jar包(源码).zip

    本压缩包文件名为“Android-仿Iphone风格翻页控件,源码及Jar包(源码).zip”,它提供了开发者们在Android平台上实现类似iPhone风格翻页效果的控件。这份资源包括完整的源码,允许开发者研究和学习其内部实现机制,...

    android仿iPhone滚轮效果

    在Android平台上实现类似iPhone的滚轮效果,是一种增强用户交互体验的设计手法,尤其适用于数字选择、日期选择等场景。滚轮通常被称为Picker View,它在iOS系统中是UIPickerView,而在Android中则可以通过自定义View...

    知易Cocos2D-iPhone开发教程-08

    【标题】"知易Cocos2D-iPhone开发教程-08",这是一份针对Cocos2D-iPhone框架的教程,旨在帮助开发者深入理解并掌握如何在iOS平台上使用Cocos2D进行游戏或应用的开发。"08"表示这是系列教程中的第八部分,意味着它...

    ios-iPhone解锁文字效果.zip

    这种效果类似于iPhone解锁屏幕中的数字滑动解锁,可以用于各种场景,如输入密码、滑动验证等。通过使用这个库,开发者无需从头编写复杂的动画代码,大大简化了开发流程。 YQLightLable的关键特性包括: 1. **...

Global site tag (gtag.js) - Google Analytics