QQ space stretchable head

QQ space stretchable head

Source code introduction: Imitating QQ space with stretchable header, classic and easy to use, worth a try.

Source code effect:

Source code snippet:

  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. // Initial height  
  17. private   int mImageViewHeight = - 1 ;
  18. //***Stretch height  
  19. private   int mDrawableMaxHeight = - 1 ;
  20.   
  21. public ParallaxListView(Context context, AttributeSet attrs) {
  22. super (context, attrs);
  23.   
  24. }
  25.   
  26. /**
  27. * Set the stretched image
  28. *
  29. * @param imageView
  30. */  
  31. public   void setParallaxImageView(ImageView imageView) {
  32. this .mImageView = imageView;
  33. // Set the telescopic type -- center fill  
  34. this .mImageView.setScaleType(ScaleType.CENTER_CROP);
  35. }
  36.   
  37. /**
  38. * Initialize the initial height of the loaded image
  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. * Callback when sliding too far
  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. // Control the height of ImageView to continue to increase  
  60. boolean isCollapse = resizeOverScrollBy(deltaY);
  61.   
  62. // return true: When you pull down to a certain place on the border, you will no longer pull down  
  63. return isCollapse ? true : super .overScrollBy(deltaX, deltaY, scrollX,
  64. scrollY, scrollRangeX, scrollRangeY, maxOverScrollX,
  65. maxOverScrollY, isTouchEvent);
  66. }
  67.   
  68. /**
  69. * Listen for ListView sliding
  70. */  
  71. @Override  
  72. protected   void onScrollChanged( int l, int t, int oldl, int oldt) {
  73. super .onScrollChanged(l, t, oldl, oldt);
  74. // Get the parent control of ImageView  
  75. View header = (View) mImageView.getParent();
  76. if (header.getTop() < 0 && mImageView.getHeight() > mImageViewHeight) {
  77. // Reduce the height of ImageView -- cannot exceed the original height of the image  
  78. mImageView.getLayoutParams().height = Math.max(
  79. mImageView.getHeight() + header.getTop(), mImageViewHeight);
  80. // The height of the container where the ImageView is located also needs to change: 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. // During the pull-down process, constantly control the height of ImageView  
  91. /**
  92. * deltaY: is the distance slid per millisecond when exceeding the sliding limit -- Increment (- for pull-down transition, + for pull-up transition) Size: Determined by the user's sliding speed General sliding speed
  93. * -50~50
  94. */  
  95. if (deltaY < 0 ) {
  96. // Pull down transition, continuously increase the height of ImageView, deltay is a negative number, increasing the height is subtracting  
  97. mImageView.getLayoutParams().height = mImageView.getHeight()
  98. - deltaY;
  99. // View re-adjusts width and height  
  100. mImageView.requestLayout(); // onMeasure-->onLayout  
  101. } else {
  102. // Pull up transition, continuously reduce the height of ImageView, deltay is a positive number, reduce the height or subtract  
  103. if (mImageView.getHeight()>mImageViewHeight) {
  104. mImageView.getLayoutParams().height = Math.max(
  105. mImageView.getHeight() - deltaY, mImageViewHeight);
  106. // View re-adjusts width and height  
  107. mImageView.requestLayout(); // onMeasure-->onLayout  
  108. }
  109.               
  110. }
  111.   
  112. return   false ;
  113. }
  114.       
  115. /**
  116. * Listen for touch -- release your hand
  117. */  
  118. @Override  
  119. public   boolean onTouchEvent(MotionEvent ev) {
  120. // judge  
  121. if (ev.getAction()== MotionEvent.ACTION_UP) {
  122. // Start the rebound animation  
  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. // Height before animation execution  
  139. private   int originalHeight;
  140. // Height difference  
  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. * This method will be called back when the animation is continuously executed
  152. * interpolatedTime: range is 0
  153. * 0ms-------------->300ms
  154. * Current image height ---> height before animation execution - height difference * 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.   
  167. }

Download address: http://download..com/data/2091668

<<:  3D parallax guide page

>>:  Chinese jailbreakers, start running tonight

Recommend

The 4 user quadrants of product promotion!

When we promote our products , the first thing we...

Example analysis: Performance optimization of "Qichu Encyclopedia"

Qiqubaike has undergone a major revision after th...

Be careful! Don’t wear headphones like this! The harm is beyond imagination...

This article was reviewed by Tao Ning, PhD, Assoc...

iOS: How to submit apps to the App Store

The process of submitting a new application is re...