仿QQ空间可拉伸头部

移动开发
仿QQ空间可拉伸头部,经典好用,值得一试。

源码简介:仿QQ空间可拉伸头部,经典好用,值得一试。

源码效果:

源码片段:

package com.example.tz_demo_6_27; 
  
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.Transformation; 
import android.widget.ImageView; 
import android.widget.ImageView.ScaleType; 
import android.widget.ListView; 
  
public class ParallaxListView extends ListView { 
  
    private ImageView mImageView; 
    // 初始高度 
    private int mImageViewHeight = -1
    // ***拉伸高度 
    private int mDrawableMaxHeight = -1
  
    public ParallaxListView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
  
    } 
  
    /** 
     * 设置拉伸的图片 
     *  
     * @param imageView 
     */ 
    public void setParallaxImageView(ImageView imageView) { 
        this.mImageView = imageView; 
        // 设置伸缩类型 -- 居中填充 
        this.mImageView.setScaleType(ScaleType.CENTER_CROP); 
    } 
  
    /** 
     * 初始化图片加载进来最初的高度 
     *  
     */ 
    public void setViewBounds() { 
        if (mImageViewHeight == -1) { 
            mImageViewHeight = mImageView.getHeight(); 
            if (mImageViewHeight < 0) { 
                mImageViewHeight = getContext().getResources() 
                        .getDimensionPixelSize(R.dimen.size_default); 
            } 
        } 
  
    } 
  
    /** 
     * 滑动过头的时候回调 
     */ 
    @Override 
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, 
            int scrollY, int scrollRangeX, int scrollRangeY, 
            int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) { 
        // 控制ImageView的高度不断增加 
        boolean isCollapse = resizeOverScrollBy(deltaY); 
  
        // return true:下拉到边界的某个地方的时候不再往下拉 
        return isCollapse ? true : super.overScrollBy(deltaX, deltaY, scrollX, 
                scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, 
                maxOverScrollY, isTouchEvent); 
    } 
  
    /** 
     *  监听ListView滑动 
     */ 
    @Override 
    protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
        super.onScrollChanged(l, t, oldl, oldt); 
        // 获得ImageView的父控件 
        View header = (View) mImageView.getParent(); 
        if (header.getTop() < 0 && mImageView.getHeight() > mImageViewHeight) { 
            // 减小ImageView的高度 -- 不能超过图片最原始的高度 
            mImageView.getLayoutParams().height = Math.max( 
                    mImageView.getHeight() + header.getTop(), mImageViewHeight); 
            // ImageView所在的容器的高度也要变化:getTop--->0 
            header.layout(header.getLeft(), 0, header.getRight(), 
                    header.getHeight()); 
            mImageView.requestLayout(); 
        } 
  
    } 
      
  
    private boolean resizeOverScrollBy(int deltaY) { 
        // 下拉的过程当中,不断地控制ImageView的高度 
        /** 
         * deltaY:是在超出滑动的时候每毫秒滑动的距离 -- 增量 (-往下拉过渡,+往上拉过渡) 大小:根据用户滑动的速度决定 一般滑动的速度 
         * -50~50 
         */ 
        if (deltaY < 0) { 
            // 下拉过渡,不断增加ImageView的高度,deltay是负数,增加高度就是减去 
            mImageView.getLayoutParams().height = mImageView.getHeight() 
                    - deltaY; 
            // View重新调整宽高 
            mImageView.requestLayout(); // onMeasure-->onLayout 
        } else { 
            // 上拉过渡,不断减小ImageView的高度,deltay是正数,减小高度还是减去 
            if (mImageView.getHeight()>mImageViewHeight) { 
                mImageView.getLayoutParams().height = Math.max( 
                        mImageView.getHeight() - deltaY, mImageViewHeight); 
                // View重新调整宽高 
                mImageView.requestLayout(); // onMeasure-->onLayout 
            } 
              
        } 
  
        return false
    } 
      
    /** 
     * 监听触摸 -- 松开手 
     */ 
    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
        // 判断 
        if (ev.getAction()== MotionEvent.ACTION_UP) { 
            // 开启回弹的动画 
            ResetAnimation animation=new ResetAnimation(mImageView,mImageViewHeight); 
            animation.setDuration(300); 
            mImageView.startAnimation(animation); 
        } 
          
        return super.onTouchEvent(ev); 
    } 
      
    public class ResetAnimation extends Animation{ 
          
          
          
        private ImageView mView; 
          
        private int targetHeight; 
        // 动画执行前的高度 
        private int originalHeight; 
        // 高度差 
        private int extraHeight; 
  
        public ResetAnimation(ImageView mImageView,int targetHeight) { 
            this.mView=mImageView; 
            this.targetHeight=targetHeight; 
            this.originalHeight=mImageView.getHeight(); 
            extraHeight=originalHeight-targetHeight; 
        } 
          
        /** 
         * 动画不断地执行的时候会回调该方法 
         * interpolatedTime:范围是0 
         * 0ms-------------->300ms 
         * 当前的图片高度--->动画执行之前的高度-高度差*interpolatedTime 
         * extraHeight------>0 
         */ 
        @Override 
        protected void applyTransformation(float interpolatedTime, 
                Transformation t) { 
              
            mView.getLayoutParams().height=(int) (originalHeight-extraHeight*interpolatedTime); 
            mView.requestLayout(); 
            super.applyTransformation(interpolatedTime, t); 
        } 
    } 
  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.

下载地址:http://down.51cto.com/data/2091668

责任编辑:倪明 来源: devstore
相关推荐

2015-08-07 15:32:19

欢迎界面仿微信仿qq空间

2022-05-26 00:06:19

CSSFirefoxElectron

2017-05-03 16:30:38

AndroidScrollView滚动视图

2015-03-31 18:13:09

swipelistvi

2015-09-07 10:57:38

qq未读消息

2015-01-19 12:19:04

iOS源码ActionSheet仿QQ音乐

2009-05-22 10:11:19

2018-12-18 13:41:40

2015-10-27 11:14:03

qq腾讯95后

2017-05-02 09:12:20

QQ空间

2017-06-08 11:23:24

电子皮肤中科院皮肤

2013-07-24 18:34:59

iOS图片拉伸iOS开发学习resizableIm

2017-05-02 09:34:49

QQ空间

2017-05-02 09:51:39

QQ空间

2017-05-27 13:03:28

互联网

2012-12-25 13:16:56

AndroidQQ2012UI

2015-04-28 13:34:52

phpqq空间发表文章

2015-07-22 10:45:02

QQ数据大数据分析数据泄露

2013-09-02 15:04:25

QQ高仿版GG叽叽

2013-08-15 10:09:39

红米手机QQ空间大数据
点赞
收藏

51CTO技术栈公众号