[置顶] android listview仿iphone弹簧特效

     IOS常见视图有一个弹性回退的动画效果,Android 从2.3开始实现了下冲到底部和上冲到底部时颜色渐变动画效果。View类有两个很重要的方法scrollTo和scrollBy,允许移动视图的可见区域。这就是我们今天实现动画效果的理论基础,我们可以在android数据列表顶部往下拖动列表或底部拖动相应的移动数据列表,当停止拖动时再让它弹回去。下面是代码部分:
 
package com.linghu; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.ListView; 

public class Pull_ListView extends ListView implements Runnable { 
    private float mLastDownY = 0f; 
    private int mDistance = 0; 
    private int mStep = 10; 
    private boolean mPositive = false; 
 

    public Pull_ListView (Context context, AttributeSet attrs) { 
          super(context, attrs); 
    } 
 
    public Pull_ListView (Context context, AttributeSet attrs, int defStyle) { 
          super(context, attrs, defStyle); 
    } 
 
    public Pull_ListView (Context context) { 
          super(context); 
    } 
 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
         switch (event.getAction()) { 
              case MotionEvent.ACTION_DOWN: 
                   if (mLastDownY == 0f && mDistance == 0) { 
                         mLastDownY = event.getY(); 
                   return true; 
              } 
              break; 
 
    case MotionEvent.ACTION_CANCEL: 
           break;

    case MotionEvent.ACTION_UP: 
          if (mDistance != 0) { 
           mStep = 1; 
           mPositive = (mDistance >= 0); 
           this.post(this); 
           return true; 
        } 
        mLastDownY = 0f; 
        mDistance = 0; 
        break; 
 
    case MotionEvent.ACTION_MOVE: 
        if (mLastDownY != 0f) { 
              mDistance = (int) (mLastDownY - event.getY()); 
              if ((mDistance < 0 && getFirstVisiblePosition() == 0 && getChildAt(0).getTop() == 0) || (mDistance > 0 && getLastVisiblePosition() == getCount() - 1)) { 
                   mDistance /= 2; 
                   scrollTo(0, mDistance); 
                   return true; 
               } 
        } 
        mDistance = 0; 
        break; 
        } 
        return super.onTouchEvent(event); 
    } 
 
    @Override 
    public void run() { 
          mDistance += mDistance > 0 ? -mStep : mStep; 
          scrollTo(0, mDistance); 
          if ((mPositive && mDistance <= 0) || (!mPositive && mDistance >= 0)) { 
                scrollTo(0, 0); 
                mDistance = 0; 
                mLastDownY = 0f; 
                return; 
           } 
          mStep += 1; 
          this.postDelayed(this, 10); 
     } 
} // end class 
 

上面是比较老的代码,效果可能会卡一点不是很理想,看下面这个效果很流畅的

地址:http://download.csdn.net/detail/linghu_java/5899421

你可能感兴趣的:(android,ListView,iPhone,Class,float)