zl程序教程

您现在的位置是:首页 >  APP

当前栏目

仿QQ空间可拉伸头部

2023-04-18 14:57:47 时间

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

源码效果:

源码片段:

  1. package com.example.tz_demo_6_27; 
  2.   
  3. import android.content.Context; 
  4. import android.util.AttributeSet; 
  5. import android.view.MotionEvent; 
  6. import android.view.View; 
  7. import android.view.animation.Animation; 
  8. import android.view.animation.Transformation; 
  9. import android.widget.ImageView; 
  10. import android.widget.ImageView.ScaleType; 
  11. import android.widget.ListView; 
  12.   
  13. public class ParallaxListView extends ListView { 
  14.   
  15.     private ImageView mImageView; 
  16.     // 初始高度 
  17.     private int mImageViewHeight = -1
  18.     // ***拉伸高度 
  19.     private int mDrawableMaxHeight = -1
  20.   
  21.     public ParallaxListView(Context context, AttributeSet attrs) { 
  22.         super(context, attrs); 
  23.   
  24.     } 
  25.   
  26.     /** 
  27.      * 设置拉伸的图片 
  28.      *  
  29.      * @param imageView 
  30.      */ 
  31.     public void setParallaxImageView(ImageView imageView) { 
  32.         this.mImageView = imageView; 
  33.         // 设置伸缩类型 -- 居中填充 
  34.         this.mImageView.setScaleType(ScaleType.CENTER_CROP); 
  35.     } 
  36.   
  37.     /** 
  38.      * 初始化图片加载进来最初的高度 
  39.      *  
  40.      */ 
  41.     public void setViewBounds() { 
  42.         if (mImageViewHeight == -1) { 
  43.             mImageViewHeight = mImageView.getHeight(); 
  44.             if (mImageViewHeight < 0) { 
  45.                 mImageViewHeight = getContext().getResources() 
  46.                         .getDimensionPixelSize(R.dimen.size_default); 
  47.             } 
  48.         } 
  49.   
  50.     } 
  51.   
  52.     /** 
  53.      * 滑动过头的时候回调 
  54.      */ 
  55.     @Override 
  56.     protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, 
  57.             int scrollY, int scrollRangeX, int scrollRangeY, 
  58.             int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) { 
  59.         // 控制ImageView的高度不断增加 
  60.         boolean isCollapse = resizeOverScrollBy(deltaY); 
  61.   
  62.         // return true:下拉到边界的某个地方的时候不再往下拉 
  63.         return isCollapse ? true : super.overScrollBy(deltaX, deltaY, scrollX, 
  64.                 scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, 
  65.                 maxOverScrollY, isTouchEvent); 
  66.     } 
  67.   
  68.     /** 
  69.      *  监听ListView滑动 
  70.      */ 
  71.     @Override 
  72.     protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
  73.         super.onScrollChanged(l, t, oldl, oldt); 
  74.         // 获得ImageView的父控件 
  75.         View header = (View) mImageView.getParent(); 
  76.         if (header.getTop() < 0 && mImageView.getHeight() > mImageViewHeight) { 
  77.             // 减小ImageView的高度 -- 不能超过图片最原始的高度 
  78.             mImageView.getLayoutParams().height = Math.max( 
  79.                     mImageView.getHeight() + header.getTop(), mImageViewHeight); 
  80.             // ImageView所在的容器的高度也要变化:getTop--->0 
  81.             header.layout(header.getLeft(), 0, header.getRight(), 
  82.                     header.getHeight()); 
  83.             mImageView.requestLayout(); 
  84.         } 
  85.   
  86.     } 
  87.       
  88.   
  89.     private boolean resizeOverScrollBy(int deltaY) { 
  90.         // 下拉的过程当中,不断地控制ImageView的高度 
  91.         /** 
  92.          * deltaY:是在超出滑动的时候每毫秒滑动的距离 -- 增量 (-往下拉过渡,+往上拉过渡) 大小:根据用户滑动的速度决定 一般滑动的速度 
  93.          * -50~50 
  94.          */ 
  95.         if (deltaY < 0) { 
  96.             // 下拉过渡,不断增加ImageView的高度,deltay是负数,增加高度就是减去 
  97.             mImageView.getLayoutParams().height = mImageView.getHeight() 
  98.                     - deltaY; 
  99.             // View重新调整宽高 
  100.             mImageView.requestLayout(); // onMeasure-->onLayout 
  101.         } else { 
  102.             // 上拉过渡,不断减小ImageView的高度,deltay是正数,减小高度还是减去 
  103.             if (mImageView.getHeight()>mImageViewHeight) { 
  104.                 mImageView.getLayoutParams().height = Math.max( 
  105.                         mImageView.getHeight() - deltaY, mImageViewHeight); 
  106.                 // View重新调整宽高 
  107.                 mImageView.requestLayout(); // onMeasure-->onLayout 
  108.             } 
  109.               
  110.         } 
  111.   
  112.         return false
  113.     } 
  114.       
  115.     /** 
  116.      * 监听触摸 -- 松开手 
  117.      */ 
  118.     @Override 
  119.     public boolean onTouchEvent(MotionEvent ev) { 
  120.         // 判断 
  121.         if (ev.getAction()== MotionEvent.ACTION_UP) { 
  122.             // 开启回弹的动画 
  123.             ResetAnimation animation=new ResetAnimation(mImageView,mImageViewHeight); 
  124.             animation.setDuration(300); 
  125.             mImageView.startAnimation(animation); 
  126.         } 
  127.           
  128.         return super.onTouchEvent(ev); 
  129.     } 
  130.       
  131.     public class ResetAnimation extends Animation{ 
  132.           
  133.           
  134.           
  135.         private ImageView mView; 
  136.           
  137.         private int targetHeight; 
  138.         // 动画执行前的高度 
  139.         private int originalHeight; 
  140.         // 高度差 
  141.         private int extraHeight; 
  142.   
  143.         public ResetAnimation(ImageView mImageView,int targetHeight) { 
  144.             this.mView=mImageView; 
  145.             this.targetHeight=targetHeight; 
  146.             this.originalHeight=mImageView.getHeight(); 
  147.             extraHeight=originalHeight-targetHeight; 
  148.         } 
  149.           
  150.         /** 
  151.          * 动画不断地执行的时候会回调该方法 
  152.          * interpolatedTime:范围是0 
  153.          * 0ms-------------->300ms 
  154.          * 当前的图片高度--->动画执行之前的高度-高度差*interpolatedTime 
  155.          * extraHeight------>0 
  156.          */ 
  157.         @Override 
  158.         protected void applyTransformation(float interpolatedTime, 
  159.                 Transformation t) { 
  160.               
  161.             mView.getLayoutParams().height=(int) (originalHeight-extraHeight*interpolatedTime); 
  162.             mView.requestLayout(); 
  163.             super.applyTransformation(interpolatedTime, t); 
  164.         } 
  165.     } 
  166.   

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