类似iPhone的弹性ListView滚动

来源:互联网 发布:阿里云服务器的ip地址 编辑:程序博客网 时间:2024/06/07 10:49
从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方法中做如下实现
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,   boolean clampedY) {    this.scrollBy(0, delY / 2);    if (action_up) {      this.scrollTo(0, 0);  }}



--------------------------------------------------------------------------------------
类似iPhone弹性效果的BounceListView
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);      }     }





附送:
用字符串显示图片
做项目过程中遇到一个问题,从数据库里读取图片名称,然后调用图片。直接用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);
0 0
原创粉丝点击