仿QQ空间视差效果,ListView.setHeader( )

根据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( )的更多相关文章
- JS仿QQ空间鼠标停在长图片时候图片自动上下滚动效果
JS仿QQ空间鼠标停在长图片时候图片自动上下滚动效果 今天是2014年第一篇博客是关于类似于我们的qq空间长图片展示效果,因为一张很长的图片不可能全部把他展示出来,所以外层用了一个容器给他一个高度,超 ...
- 仿QQ空间长图效果简易版--母亲节感恩
手机网站 母亲节最火的两件事 1.NBA 杜兰特在获MVP催泪致辞献给母亲:她才是真的MVP. 2.QQ空间长图 ------------------------------------------- ...
- 仿QQ空间和微信朋友圈,高解耦高复用高灵活
先看看效果: 用极少的代码实现了 动态详情 及 二级评论 的 数据获取与处理 和 UI显示与交互,并且高解耦.高复用.高灵活. 动态列表界面MomentListFragment支持 下拉刷新与上拉加载 ...
- 仿QQ空间动态界面分享
先看看效果: 用极少的代码实现了 动态详情 及 二级评论 的 数据获取与处理 和 UI显示与交互,并且高解耦.高复用.高灵活. 动态列表界面MomentListFragment支持 下拉刷新与上拉加载 ...
- Fragment,仿QQ空间
转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9023451 在今天的这篇文章当中,我依然会以实战加理论结合 ...
- iOS传感器集锦、飞机大战、开发调试工具、强制更新、Swift仿QQ空间头部等源码
iOS精选源码 飞机大作战 MUPhotoPreview -简单易用的图片浏览器 LLDebugTool是一款针对开发者和测试者的调试工具,它可以帮... 多个UIScrollView.UITable ...
- Html - 仿QQ空间右下角工具浮动块
仿QQ空间右下角工具浮动块 <style type="text/css"> .cy-tp-area>.cy-tp-fixbtn>.cy-tp-text { ...
- 仿QQ空间根据位置弹出PopupWindow显示更多操作效果
我们打开QQ空间的时候有个箭头按钮点击之后弹出PopupWindow会根据位置的变化显示在箭头的上方还是下方,比普通的PopupWindow弹在屏幕中间显示好看的多. 先看QQ空间效果图: ...
- ScrollView的阻尼回弹效果实现(仿qq空间)
玩过新浪微博,qq空间等手机客户端的童鞋,都应该清楚,在主界面向下滑动时,会有一个阻尼回弹效果,看起来挺不错,接下来我们就来实现一下这种效果,下拉后回弹刷新界面,先看效果图: 这个是编辑器里面的界面效 ...
随机推荐
- c#事件求解
闲来无聊对于clr一书又重新温习了下,但是看到事件这张后还是有很多的困惑,对于事件能力CLR是这样描述,通知其它对象发生特定的事情. 1.其它对象:是指对于事件的关注者 2.特定的事件:对于满足事件交 ...
- ASP.Net页面传值比较
ASP.Net页面传值比较 作为一个ASP.Net程序员,尤其是搞B/S开发的,对于不同页面之间变量值的传递用的非常广泛,而掌握不同方式之间的区别和特点也就很有必要.本文将针对这一知识点做一个简单 ...
- .net实现依赖注入
.net实现依赖注入 1. 问题的提出 开发中,尤其是大型项目的开发中,为了降低模块间.类间的耦合关系,比较提倡基于接口开发,但在实现中也必须面临最终是“谁”提供实体类的问题.Martin Fowle ...
- Sql Server实现多行数据按分组用逗号分隔成一行数据
例如,要将下面的数据 以GROUP_ID进行分组,一组一行,一组中的多个PRODUCT_ID用逗号分隔,select 出来成如下结果: 在Sql Server中,我目前想到的一种方法是写一个函数,如下 ...
- 解决Timer回调方法重复调用的问题
如果Timer的回调方法的执行时间很长,计时器可能在上个回调方法还没有完成的时候再次触发,如果我们只是想让一个回调方法执行完后再执行下一个回调方法,可以这样: private static Timer ...
- [转]ARM/Thumb2PortingHowto
src: https://wiki.edubuntu.org/ARM/Thumb2PortingHowto#ARM_Assembler_Overview When you see some assem ...
- 用django搭建一个简易blog系统(翻译)(四)
12. Create the templates 你需要做三件事来去掉TemplateDoesNotExist错误 第一件,创建下面目录 * netmag/netmag/templates * net ...
- 使用 Aspose.Cells 实现 excel导入
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 开发者所需要知道的iOS7 SDK新特性
iOS 7 春风又绿加州岸,物是人非又一年.WWDC 2013 keynote落下帷幕,新的iOS开发旅程也由此开启.在iOS7界面重大变革的背后,开发者们需要知道的又有哪些呢.同去年一样,我会先简单 ...
- easyui 通用的datagrid中如何带有查询条件分页
html 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...