Android仿QQ可拉伸頭部控件
本文實(shí)例為大家分享了Android仿QQ可拉伸頭部控件的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
該控件大致思路:
1.采用繼承l(wèi)istview加入頭部view。
2.監(jiān)聽listview滾動。
3.自定義動畫回彈。
先看效果吧:
activity-main.xml布局如下:
<LinearLayout 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" android:orientation="horizontal" tools:context=".MainActivity" > <com.example.headerlistview.HeaderListView android:id="@+id/header_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@android:color/transparent" android:divider="@android:color/darker_gray" android:dividerHeight="1dip" android:duplicateParentState="true" android:scrollbars="none" > </com.example.headerlistview.HeaderListView> </LinearLayout>
headerview.xml布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/header_image" android:layout_width="match_parent" android:layout_height="150dip" android:scaleType="centerCrop" android:src="@drawable/lifei987" /> </LinearLayout>
list_item布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="80dip" android:gravity="center_vertical" android:orientation="horizontal" > " <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="80dip" android:layout_marginLeft="10dip" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="lifei" /> <TextView android:id="@+id/tv_describe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="lifeiasdfasdfasfsadfasf" /> </LinearLayout> </LinearLayout>
activity代碼:
package com.example.headerlistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private HeaderListView header_lv;
private ImageView header_iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getHeaderView();
initView();
}
private void initView() {
// TODO Auto-generated method stub
header_lv=(HeaderListView) findViewById(R.id.header_lv);
header_lv.addHeaderView(getHeaderView());
header_lv.setHeaderView(header_iv);
header_lv.setAdapter(getSimpleAdapter());
}
public BaseAdapter getSimpleAdapter(){
List<Map<String, Object>> data=new ArrayList<Map<String,Object>>();
for(int i=0;i<15;i++){
Map<String, Object> map=new HashMap<String, Object>();
map.put("name", "鄭州___"+i);
map.put("describe", "asdfasdfasdfasdfasdfsadfsad");
data.add(map);
}
SimpleAdapter simpleAdapter=new SimpleAdapter(this, data, R.layout.list_item, new String[]{"name","describe"}, new int[]{R.id.tv_name,R.id.tv_describe});
return simpleAdapter;
}
public View getHeaderView(){
View view= getLayoutInflater().inflate(R.layout.headerview, null);
header_iv =(ImageView) view.findViewById(R.id.header_image);
return view;
}
}
自定義控件HeaderListView:
package com.example.headerlistview;
import java.security.spec.ECField;
import android.annotation.SuppressLint;
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.ListView;
public class HeaderListView extends ListView {
private ImageView headerView;
private int headerView_initHeight;//imageview初始高度
public void setHeaderView(ImageView headerView) {
this.headerView = headerView;
}
public HeaderListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public HeaderListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public HeaderListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* listview焦點(diǎn)改變時--獲取iamgeview高度的初始值,該值不能在構(gòu)造方法中獲取
*/
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasWindowFocus);
if(hasWindowFocus){
this.headerView_initHeight=headerView.getHeight();
}
}
@SuppressLint("NewApi") @Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
int scrollY, int scrollRangeX, int scrollRangeY,
int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
// 滑動過頭的時候調(diào)用
boolean bl=resizeHeaderView(deltaY);
return bl?true:super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
}
/**
* 控制imageview高度的增加
* @param deltaY 偏移量
*/
private boolean resizeHeaderView(int deltaY) {
if(Math.abs((double)deltaY)<200){
if(deltaY<0){
headerView.getLayoutParams().height=headerView.getHeight()-deltaY;
//重新繪制
headerView.requestLayout();
}else{
headerView.getLayoutParams().height=headerView.getHeight()-deltaY;
headerView.requestLayout();
}
}
return false;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
//獲取imageview父控件
View parent=(View) headerView.getParent();
//當(dāng)父控件的top值小于零或者高度大于原始高度時觸發(fā)
if(parent.getTop()<0||headerView.getHeight()>headerView_initHeight){
headerView.getLayoutParams().height=headerView.getHeight()+parent.getTop();
parent.layout(parent.getLeft(),0, parent.getRight(), parent.getHeight());
headerView.requestLayout();
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if(ev.getAction()==MotionEvent.ACTION_UP||ev.getAction()==MotionEvent.ACTION_CANCEL){
MyAnimation animation=new MyAnimation(headerView, headerView_initHeight);
animation.setDuration(200);
headerView.startAnimation(animation);
}
return super.onTouchEvent(ev);
}
public class MyAnimation extends Animation{
private ImageView header_iv;
private int currentHeight;
private int targetHeight;
private int poorHeight;
public MyAnimation(ImageView iv,int targetHeight){
this.header_iv=iv;
this.targetHeight=targetHeight;
this.currentHeight=iv.getHeight();
this.poorHeight=this.currentHeight-this.targetHeight;
}
/**
* 動畫執(zhí)行期間執(zhí)行該方法,不斷執(zhí)行
* interpolatedTime:當(dāng)前時間與duration的時間比(時間執(zhí)行百分比)
*/
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
this.header_iv.getLayoutParams().height=(int)(currentHeight-poorHeight*interpolatedTime);
this.header_iv.requestLayout();
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android自定義View實(shí)現(xiàn)地鐵顯示牌效果
欄 目:Android
下一篇:android使用surfaceview+MediaPlayer播放視頻
本文標(biāo)題:Android仿QQ可拉伸頭部控件
本文地址:http://www.jygsgssxh.com/a1/Android/9075.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
- 01-10android實(shí)現(xiàn)簡單計算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


