Android ListView 头部缩小放大的效果

这里写图片描述

这里写代码片


![这里写图片描述](http://img.blog.csdn.net/20160722181258519)

ListView 头部缩小放大的效果

package com.example.draglistview;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MyLayout extends LinearLayout {

    private int mDefaultHeadHeight;

    private ImageView headView;

    private ListView twoChildView;

    private int height, translationY;

    private LayoutParams params;

    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
    }

    private float downY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {

        case MotionEvent.ACTION_DOWN:

            downY = ev.getY();

            break;

        case MotionEvent.ACTION_MOVE:

            float moveY = ev.getY();

            float distanceY = moveY - downY;

            if (distanceY > 0 && twoChildView.getFirstVisiblePosition() == 0) {

                return true;

            }

            if (distanceY < 0
                    && twoChildView.getTranslationY() > -mDefaultHeadHeight) {

                return true;
            }

            break;
        }

        return false;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        twoChildView = (ListView) getChildAt(1);

        twoChildView.measure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        headView = (ImageView) getChildAt(0);

        mDefaultHeadHeight = headView.getMeasuredHeight();

        height = mDefaultHeadHeight;

        params = (LayoutParams) headView.getLayoutParams();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

        case MotionEvent.ACTION_MOVE:

            float moveY = event.getY();

            float distanceY = moveY - downY;

            if (distanceY > 0) {

                if (twoChildView.getTranslationY() >= 0) {

                    height += distanceY / 2.5f;

                    params.height = height;

                    headView.setLayoutParams(params);

                } else {

                    translationY += distanceY;

                    if (translationY > 0) {

                        translationY = 0;
                    }

                    twoChildView.setTranslationY(translationY);
                }

            } else {

                if (height > mDefaultHeadHeight) {

                    height += distanceY / 2.5f;

                    params.height = height;

                    headView.setLayoutParams(params);

                } else {

                    // 等于0 的情况也包括在内
                    if (twoChildView.getTranslationY() >= -mDefaultHeadHeight) {

                        translationY += distanceY;

                        if (translationY < -mDefaultHeadHeight) {

                            translationY = -mDefaultHeadHeight;
                        }

                        twoChildView.setTranslationY(translationY);

                    }

                }
            }

            downY = moveY;

            break;

        case MotionEvent.ACTION_UP:

            if (height > mDefaultHeadHeight) {

                ValueAnimator animator = ValueAnimator.ofInt(height,
                        mDefaultHeadHeight).setDuration(1000);

                animator.addUpdateListener(new AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {

                        height = (Integer) animation.getAnimatedValue();

                        params.height = height;

                        headView.setLayoutParams(params);

                    }
                });
                animator.start();
            }

            break;

        }

        return true;
    }

}

<com.example.draglistview.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="150dip"
        android:src="@drawable/banner1" android:scaleType="fitXY"/>

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@android:color/transparent" 
        android:cacheColorHint="@android:color/transparent" 
        android:background="@android:color/white">
    </ListView>

</com.example.draglistview.MyLayout>

下载链接http://download.csdn.net/detail/luo446718254/9583686

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值