根据listview的手指移动事件,动态设置listview上面的图片的宽高,并在手指放开的时候

实现图片的动画(随时间变化的动画值) ValueAnimator.ofInt ( )

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * Created by lxj on 2017/1/5.
 */

public class ParallaxListView extends ListView {
    public ParallaxListView(Context context) {
        super(context);
    }

    public ParallaxListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ParallaxListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    int originalHeight;//最初的高度,就是120dp
    ImageView image;
    int maxHeight;//小孩的最大高度,就是设定为图片本身的高度
    public void setParallaxImage(ImageView image){
        this.image = image;

        //从dimens文件中读取image_height的值,并自动转为像素
        originalHeight = getResources().getDimensionPixelSize(R.dimen.image_height);

        maxHeight = this.image.getDrawable().getIntrinsicHeight();
    }

    /**
     * 该方法是在listview滑动到头的时候执行,并且可以在该方法中获取到手指移动的距离
     * @param deltaY    手指移动的距离,顶部到头是负值,底部到头是正值
     * @param scrollY   scrollTo滚动的坐标
     * @param maxOverScrollY    listview滑到头之后可以继续滑动的最大距离
     * @param isTouchEvent   是否是手指拖动到头,true:是, false:表示是靠惯性滑动到头
     * @return
     */
    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY,
                                   int scrollRangeX, int scrollRangeY,
                                   int maxOverScrollX, int maxOverScrollY,
                                   boolean isTouchEvent) {

        //如果是手指拖动到头,并且是顶部到头,其他忽略不处理
        if(isTouchEvent && deltaY<0){
            //让ImageView的高度随着手指移动而增高
            int newHeight = image.getHeight() + Math.abs(deltaY)/3;
            //限制newHeight
            if(newHeight>maxHeight){
                newHeight = maxHeight;
            }

            ViewGroup.LayoutParams params = image.getLayoutParams();
            params.height = newHeight;
            image.setLayoutParams(params);
        }

        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, 
scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent); } @Override public boolean onTouchEvent(MotionEvent ev) { if(ev.getAction()==MotionEvent.ACTION_UP){ //让ImageView的高度缓慢恢复到最初的120高度 ValueAnimator animator = ValueAnimator.ofInt(image.getHeight(),originalHeight); //监听动画值的变化,实现自己的动画逻辑 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int value = (int) animation.getAnimatedValue(); //我们需要将value设置给高度 ViewGroup.LayoutParams params = image.getLayoutParams(); params.height = value; image.setLayoutParams(params); } }); animator.setInterpolator(new OvershootInterpolator()); animator.setDuration(500); animator.start(); } return super.onTouchEvent(ev); } }
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ParallaxListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ParallaxListView) findViewById(R.id.listview);

        //去掉listview滑动到头的时候边缘的蓝色阴影
        listview.setOverScrollMode(AbsListView.OVER_SCROLL_NEVER);

        //添加header
        View header = View.inflate(this, R.layout.header, null);
        ImageView image = (ImageView) header.findViewById(R.id.image);
        listview.addHeaderView(header);
        //设置ImageView
        listview.setParallaxImage(image);

        listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
                Constant.NAMES));

    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"

    tools:context="com.zh.parallax97.MainActivity">

    <com.itheima.parallax97.ParallaxListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

仿QQ空间视差效果,ListView.setHeader( )的更多相关文章

  1. JS仿QQ空间鼠标停在长图片时候图片自动上下滚动效果

    JS仿QQ空间鼠标停在长图片时候图片自动上下滚动效果 今天是2014年第一篇博客是关于类似于我们的qq空间长图片展示效果,因为一张很长的图片不可能全部把他展示出来,所以外层用了一个容器给他一个高度,超 ...

  2. 仿QQ空间长图效果简易版--母亲节感恩

    手机网站 母亲节最火的两件事 1.NBA 杜兰特在获MVP催泪致辞献给母亲:她才是真的MVP. 2.QQ空间长图 ------------------------------------------- ...

  3. 仿QQ空间和微信朋友圈,高解耦高复用高灵活

    先看看效果: 用极少的代码实现了 动态详情 及 二级评论 的 数据获取与处理 和 UI显示与交互,并且高解耦.高复用.高灵活. 动态列表界面MomentListFragment支持 下拉刷新与上拉加载 ...

  4. 仿QQ空间动态界面分享

    先看看效果: 用极少的代码实现了 动态详情 及 二级评论 的 数据获取与处理 和 UI显示与交互,并且高解耦.高复用.高灵活. 动态列表界面MomentListFragment支持 下拉刷新与上拉加载 ...

  5. Fragment,仿QQ空间

    转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9023451          在今天的这篇文章当中,我依然会以实战加理论结合 ...

  6. iOS传感器集锦、飞机大战、开发调试工具、强制更新、Swift仿QQ空间头部等源码

    iOS精选源码 飞机大作战 MUPhotoPreview -简单易用的图片浏览器 LLDebugTool是一款针对开发者和测试者的调试工具,它可以帮... 多个UIScrollView.UITable ...

  7. Html - 仿QQ空间右下角工具浮动块

    仿QQ空间右下角工具浮动块 <style type="text/css"> .cy-tp-area>.cy-tp-fixbtn>.cy-tp-text { ...

  8. 仿QQ空间根据位置弹出PopupWindow显示更多操作效果

    我们打开QQ空间的时候有个箭头按钮点击之后弹出PopupWindow会根据位置的变化显示在箭头的上方还是下方,比普通的PopupWindow弹在屏幕中间显示好看的多. 先看QQ空间效果图:       ...

  9. ScrollView的阻尼回弹效果实现(仿qq空间)

    玩过新浪微博,qq空间等手机客户端的童鞋,都应该清楚,在主界面向下滑动时,会有一个阻尼回弹效果,看起来挺不错,接下来我们就来实现一下这种效果,下拉后回弹刷新界面,先看效果图: 这个是编辑器里面的界面效 ...

随机推荐

  1. 使用ServletContext实现数据共享和获得web.xml中的参数

    //适用于:很多文件需要初始化参数时,例如数据库账号和密码,不可能使用config这个对象,因为如果使用config对象去配置的话,那么每个servlet类都必须写一个参数,这时候就必须采用conte ...

  2. GitHub 简易使用

    笔记内容 学习笔记-段玉磊 Github Github 命令 写这篇文章主要写一下如何运用终端命令,进行Git的配置以及使用,由于本人我不太习惯使用图形IDE,效率没有命令行高,我还是推荐使用命令行进 ...

  3. [Usaco2008 Feb]Meteor Shower流星雨[BFS]

    Description 去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽, 届时将会对它撞到的 ...

  4. Mac 下ll命令 command not found

    在linux下习惯使用ll.la.l等ls别名的童鞋到mac os提示command not found -461deMacBook-Pro:~ root# cd ~ -461deMacBook-Pr ...

  5. [置顶] 学习JDK源码:可进一步优化的代码

    1.参数化类型的构造函数比较啰嗦 new HashMap<String, List<String>>() 如果你调用参数化类的构造函数,那么很不幸,你必须要指定类型参数,即便上 ...

  6. T4模板与数据访问层的分离

    当在企业级应用中使用EF时,会发现实体类库与数据访问层是分离的. 来一张效果图. 具体步骤: 1.运用EF生成原始的实体类 在程序集中添加完ADO.NET实体数据模型后,生成相应的实体类,此时,T4模 ...

  7. 通过Thrift访问HDFS分布式文件系统的性能瓶颈分析

    通过Thrift访问HDFS分布式文件系统的性能瓶颈分析 引言 Hadoop提供的HDFS布式文件存储系统,提供了基于thrift的客户端访问支持,但是因为Thrift自身的访问特点,在高并发的访问情 ...

  8. 查找算法---find运算,find_first_of的使用

    查找算法---find运算,find_first_of的使用 一.find运算 假设有一个int型的vector对象,名为vec,我们想知道其中是否包含某个特定值. 解决这个问题最简单的方法时使用标准 ...

  9. myeclipse maven编译出错

    从.net 到java  快一年了.这一年学了很多东西.从开发角度来说俩个语言查不到.部署上差异较大.不过java处理问题上确实不太统一.好多问题在网上没有正确的回答.刚换台式机发现 mvn inst ...

  10. Linux并发模型

    Linux并发模型 Linux并发模型 目前可以实现并发程序的方法有Apache模型(Process Per Connection,简称PPC),TPC(Thread PerConnection)模型 ...