您的位置:首页 > 其它

类似iPhone的弹性ListView滚动

2011-08-18 08:40 211 查看
从2.3开始,android开始支持listview的overscroll, 应该很方便可以做出类似iPhone的弹性滚动,及越过list顶端或者底端,然后弹性滚回。昨天google了半天的例子,一个没找到,今天又试了试,发现用很简单的方式就可以实现这个效果。大致如下:

继承ListView

private int delY;

private boolean action_up;

在 onTouchEvent(){

...

case MotionEvent.ACTION_MOVE:

delY = (int) (preY - y);

preY = y;

break;

case MotionEvent.ACTION_UP:

action_up = true;

break;

}

然后在2.3新增的onOverScrolled方法中做如下实现

Java代码


protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,

boolean clampedY) {

this.scrollBy(0, delY / 2);

if (action_up) {

this.scrollTo(0, 0);

}

}

附送:

用字符串显示图片

做项目过程中遇到一个问题,从数据库里读取图片名称,然后调用图片。直接用R.drawable.?无法调用。查了好多地方最后找到了个方法,分享给大家,希望有帮助。

主要由两种方法,个人建议第二种。

1. 不把图片放在res/drawable下,而是存放在src某个package中(如:com.drawable.resource),这种情况下的调用方法为:

String path = "com/drawable/resource/image.png";

InputStream is = getClassLoader().getResourceAsStream(path);

Drawable.createFromStream(is, "src");

2. 如果还是希望直接使用res/drawable中的图片,就需要通过下面的方法了:

假设创建工程的时候,填写的package名字为:com.test.image

int resID = getResources().getIdentifier("goto_radar", "drawable", "com.test.image");

Drawable image = getResources().getDrawable(resID);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: