Source code introduction: Imitating QQ space with stretchable header, classic and easy to use, worth a try. Source code effect: Source code snippet: - 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);
-
- }
-
-
-
-
-
-
- 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) {
-
- boolean isCollapse = resizeOverScrollBy(deltaY);
-
-
- return isCollapse ? true : super .overScrollBy(deltaX, deltaY, scrollX,
- scrollY, scrollRangeX, scrollRangeY, maxOverScrollX,
- maxOverScrollY, isTouchEvent);
- }
-
-
-
-
- @Override
- protected void onScrollChanged( int l, int t, int oldl, int oldt) {
- super .onScrollChanged(l, t, oldl, oldt);
-
- View header = (View) mImageView.getParent();
- if (header.getTop() < 0 && mImageView.getHeight() > mImageViewHeight) {
-
- mImageView.getLayoutParams().height = Math.max(
- mImageView.getHeight() + header.getTop(), mImageViewHeight);
-
- header.layout(header.getLeft(), 0 , header.getRight(),
- header.getHeight());
- mImageView.requestLayout();
- }
-
- }
-
-
- private boolean resizeOverScrollBy( int deltaY) {
-
-
-
-
-
- if (deltaY < 0 ) {
-
- mImageView.getLayoutParams().height = mImageView.getHeight()
- - deltaY;
-
- mImageView.requestLayout();
- } else {
-
- if (mImageView.getHeight()>mImageViewHeight) {
- mImageView.getLayoutParams().height = Math.max(
- mImageView.getHeight() - deltaY, mImageViewHeight);
-
- mImageView.requestLayout();
- }
-
- }
-
- 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;
- }
-
-
-
-
-
-
- @Override
- protected void applyTransformation( float interpolatedTime,
- Transformation t) {
-
- mView.getLayoutParams().height=( int ) (originalHeight-extraHeight*interpolatedTime);
- mView.requestLayout();
- super .applyTransformation(interpolatedTime, t);
- }
- }
-
- }
Download address: http://download..com/data/2091668 |