Android仿QQ可拉伸头部控件

前端之家收集整理的这篇文章主要介绍了Android仿QQ可拉伸头部控件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了Android仿QQ可拉伸头部控件的具体实现代码,供大家参考,具体内容如下

该控件大致思路:

1.采用继承listview加入头部view。
2.监听listview滚动。
3.自定义动画回弹。

先看效果吧:

Android仿QQ可拉伸头部控件


Android仿QQ可拉伸头部控件


activity-main.xml布局如下:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. tools:context=".MainActivity" >
  7.  
  8. <com.example.headerlistview.HeaderListView
  9. android:id="@+id/header_lv"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:cacheColorHint="@android:color/transparent"
  13. android:divider="@android:color/darker_gray"
  14. android:dividerHeight="1dip"
  15. android:duplicateParentState="true"
  16. android:scrollbars="none" >
  17. </com.example.headerlistview.HeaderListView>
  18.  
  19. </LinearLayout>

headerview.xml布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6.  
  7. <ImageView
  8. android:id="@+id/header_image"
  9. android:layout_width="match_parent"
  10. android:layout_height="150dip"
  11. android:scaleType="centerCrop"
  12. android:src="@drawable/lifei987"
  13. />
  14.  
  15. </LinearLayout>

list_item布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="80dip"
  5. android:gravity="center_vertical"
  6. android:orientation="horizontal" >
  7. "
  8.  
  9. <ImageView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:src="@drawable/ic_launcher" />
  13.  
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="80dip"
  17. android:layout_marginLeft="10dip"
  18. android:orientation="vertical" >
  19.  
  20. <TextView
  21. android:id="@+id/tv_name"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginTop="10dip"
  25. android:text="lifei" />
  26.  
  27. <TextView
  28. android:id="@+id/tv_describe"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_marginTop="10dip"
  32. android:text="lifeiasdfasdfasfsadfasf" />
  33. </LinearLayout>
  34.  
  35. </LinearLayout>

activity代码

  1. package com.example.headerlistview;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import android.os.Bundle;
  9. import android.app.Activity;
  10. import android.view.LayoutInflater;
  11. import android.view.Menu;
  12. import android.view.View;
  13. import android.widget.BaseAdapter;
  14. import android.widget.ImageView;
  15. import android.widget.ListView;
  16. import android.widget.SimpleAdapter;
  17.  
  18. public class MainActivity extends Activity {
  19.  
  20. private HeaderListView header_lv;
  21.  
  22. private ImageView header_iv;
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. getHeaderView();
  29. initView();
  30. }
  31.  
  32. private void initView() {
  33. // TODO Auto-generated method stub
  34. header_lv=(HeaderListView) findViewById(R.id.header_lv);
  35. header_lv.addHeaderView(getHeaderView());
  36. header_lv.setHeaderView(header_iv);
  37. header_lv.setAdapter(getSimpleAdapter());
  38. }
  39.  
  40. public BaseAdapter getSimpleAdapter(){
  41. List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
  42. for(int i=0;i<15;i++){
  43. Map<String,Object> map=new HashMap<String,Object>();
  44. map.put("name","郑州___"+i);
  45. map.put("describe","asdfasdfasdfasdfasdfsadfsad");
  46. data.add(map);
  47. }
  48.  
  49. SimpleAdapter simpleAdapter=new SimpleAdapter(this,data,R.layout.list_item,new String[]{"name","describe"},new int[]{R.id.tv_name,R.id.tv_describe});
  50. return simpleAdapter;
  51. }
  52.  
  53. public View getHeaderView(){
  54. View view= getLayoutInflater().inflate(R.layout.headerview,null);
  55. header_iv =(ImageView) view.findViewById(R.id.header_image);
  56. return view;
  57. }
  58.  
  59. }

自定义控件HeaderListView:

  1. package com.example.headerlistview;
  2.  
  3. import java.security.spec.ECField;
  4.  
  5. import android.annotation.SuppressLint;
  6. import android.content.Context;
  7. import android.util.AttributeSet;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.view.animation.Animation;
  11. import android.view.animation.Transformation;
  12. import android.widget.ImageView;
  13. import android.widget.ListView;
  14.  
  15. public class HeaderListView extends ListView {
  16.  
  17. private ImageView headerView;
  18.  
  19. private int headerView_initHeight;//imageview初始高度
  20.  
  21. public void setHeaderView(ImageView headerView) {
  22. this.headerView = headerView;
  23. }
  24.  
  25. public HeaderListView(Context context) {
  26. super(context);
  27. // TODO Auto-generated constructor stub
  28. }
  29.  
  30. public HeaderListView(Context context,AttributeSet attrs,int defStyle) {
  31. super(context,attrs,defStyle);
  32. // TODO Auto-generated constructor stub
  33. }
  34.  
  35. public HeaderListView(Context context,AttributeSet attrs) {
  36. super(context,attrs);
  37. // TODO Auto-generated constructor stub
  38. }
  39.  
  40. /**
  41. * listview焦点改变时--获取iamgeview高度的初始值,该值不能在构造方法获取
  42. */
  43. @Override
  44. public void onWindowFocusChanged(boolean hasWindowFocus) {
  45. // TODO Auto-generated method stub
  46. super.onWindowFocusChanged(hasWindowFocus);
  47. if(hasWindowFocus){
  48. this.headerView_initHeight=headerView.getHeight();
  49. }
  50. }
  51.  
  52. @SuppressLint("NewApi") @Override
  53. protected boolean overScrollBy(int deltaX,int deltaY,int scrollX,int scrollY,int scrollRangeX,int scrollRangeY,int maxOverScrollX,int maxOverScrollY,boolean isTouchEvent) {
  54. // 滑动过头的时候调用
  55. boolean bl=resizeHeaderView(deltaY);
  56.  
  57. return bl?true:super.overScrollBy(deltaX,deltaY,scrollX,scrollY,scrollRangeX,scrollRangeY,maxOverScrollX,maxOverScrollY,isTouchEvent);
  58. }
  59.  
  60. /**
  61. * 控制imageview高度的增加
  62. * @param deltaY 偏移量
  63. */
  64. private boolean resizeHeaderView(int deltaY) {
  65. if(Math.abs((double)deltaY)<200){
  66. if(deltaY<0){
  67. headerView.getLayoutParams().height=headerView.getHeight()-deltaY;
  68. //重新绘制
  69. headerView.requestLayout();
  70. }else{
  71. headerView.getLayoutParams().height=headerView.getHeight()-deltaY;
  72. headerView.requestLayout();
  73. }
  74. }
  75.  
  76. return false;
  77. }
  78.  
  79. @Override
  80. protected void onScrollChanged(int l,int t,int oldl,int oldt) {
  81. // TODO Auto-generated method stub
  82. super.onScrollChanged(l,t,oldl,oldt);
  83. //获取imageview父控件
  84. View parent=(View) headerView.getParent();
  85. //当父控件的top值小于零或者高度大于原始高度时触发
  86. if(parent.getTop()<0||headerView.getHeight()>headerView_initHeight){
  87. headerView.getLayoutParams().height=headerView.getHeight()+parent.getTop();
  88. parent.layout(parent.getLeft(),parent.getRight(),parent.getHeight());
  89. headerView.requestLayout();
  90. }
  91. }
  92.  
  93. @Override
  94. public boolean onTouchEvent(MotionEvent ev) {
  95. // TODO Auto-generated method stub
  96. if(ev.getAction()==MotionEvent.ACTION_UP||ev.getAction()==MotionEvent.ACTION_CANCEL){
  97. MyAnimation animation=new MyAnimation(headerView,headerView_initHeight);
  98. animation.setDuration(200);
  99. headerView.startAnimation(animation);
  100. }
  101. return super.onTouchEvent(ev);
  102. }
  103.  
  104. public class MyAnimation extends Animation{
  105.  
  106. private ImageView header_iv;
  107. private int currentHeight;
  108. private int targetHeight;
  109. private int poorHeight;
  110.  
  111. public MyAnimation(ImageView iv,int targetHeight){
  112. this.header_iv=iv;
  113. this.targetHeight=targetHeight;
  114. this.currentHeight=iv.getHeight();
  115. this.poorHeight=this.currentHeight-this.targetHeight;
  116. }
  117.  
  118. /**
  119. * 动画执行期间执行该方法,不断执行
  120. * interpolatedTime:当前时间与duration的时间比(时间执行百分比)
  121. */
  122. @Override
  123. protected void applyTransformation(float interpolatedTime,Transformation t) {
  124. // TODO Auto-generated method stub
  125. super.applyTransformation(interpolatedTime,t);
  126. this.header_iv.getLayoutParams().height=(int)(currentHeight-poorHeight*interpolatedTime);
  127. this.header_iv.requestLayout();
  128. }
  129. }
  130.  
  131. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的Android相关文章