Android PullZoomView:PullToZoomListViewEx(1)

51 篇文章 0 订阅

Android PullZoomView:PullToZoomListViewEx(1)

Android PullZoomView是github上面的一个第三方开源项目,该项目实现的功能被新浪微博的移动端广泛使用,其效果就是,当用户在下拉过程中,头部的图片会有一定的拉伸,当用户松开时候,图片又收缩复位,其效果如动态图所示:


PullZoomView要实现两类,一类是典型的Android ListView,另外一类是Android 的scroll view。本文先介绍PullZoomView在ListView上的实现:PullToZoomListViewEx。
首先需要把PullToZoomListViewEx像ListView一样写进布局:

[html]  view plain copy
  1. <RelativeLayout 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.     tools:context="zhangphil.listview.MainActivity" >  
  6.   
  7.     <com.ecloud.pulltozoomview.PullToZoomListViewEx  
  8.         xmlns:custom="http://schemas.android.com/apk/res-auto"  
  9.         android:id="@+id/listview"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         custom:headerView="@layout/head_view"  
  13.         custom:zoomView="@layout/head_zoom_view" />  
  14.   
  15. </RelativeLayout>  


需要注意的是,需要定义一个headerView:
custom:headerView="@layout/head_view"

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/layout_view"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:layout_gravity="bottom"  
  6.     android:gravity="bottom">  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/iv_user_head"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_centerInParent="true"  
  13.         android:src="@drawable/ic_launcher" />  
  14.   
  15.     <TextView  
  16.         android:id="@+id/tv_user_name"  
  17.         android:textSize="12sp"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_below="@id/iv_user_head"  
  21.         android:layout_centerHorizontal="true"  
  22.         android:text="http://blog.csdn.net/zhangphil"  
  23.         android:textColor="#ffffff" />  
  24.   
  25.     <LinearLayout  
  26.         android:id="@+id/ll_action_button"  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:background="#66000000"  
  30.         android:layout_alignParentBottom="true"  
  31.         android:padding="10dip">  
  32.   
  33.         <TextView  
  34.             android:id="@+id/tv_register"  
  35.             android:layout_width="match_parent"  
  36.             android:layout_height="wrap_content"  
  37.             android:text="注册"  
  38.             android:layout_weight="1"  
  39.             android:textSize="12sp"  
  40.             android:gravity="center"  
  41.             android:layout_gravity="center"  
  42.             android:textColor="#ffffff" />  
  43.   
  44.         <TextView  
  45.             android:id="@+id/tv_login"  
  46.             android:layout_width="match_parent"  
  47.             android:layout_height="wrap_content"  
  48.             android:text="登录"  
  49.             android:layout_weight="1"  
  50.             android:textSize="12sp"  
  51.             android:gravity="center"  
  52.             android:layout_gravity="center"  
  53.             android:textColor="#ffffff" />  
  54.     </LinearLayout>  
  55. </RelativeLayout>  


此处的headerView是位于PullToZoomListViewEx头部的一个子布局,里面定义一些控件将出现在PullToZoomListViewEx的头部,但此处的headerView并不会缩放,只是可以看到此处的headerView在随着下拉过程中移位。
而定义的custom:zoomView:
custom:zoomView="@layout/head_zoom_view"

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ImageView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/imageView"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:layout_gravity="center_horizontal"  
  7.     android:scaleType="centerCrop"  
  8.     android:src="@drawable/image" />  
head_zoom_view其实就是简单的放一张图片。


则是真正的要缩放伸展的View,此处通常会放置一张图片,在用户下拉过程中滑动缩放,产生奇妙的视觉效果。
在一定程度上讲,zoomView是衬托在headerView底下的。headerView是一个正常显示的Android View布局,而zoomView则是可以产生动态缩放和收缩效果的特殊zoom View。
写一个完整的例子加以说明。

Java代码:

[java]  view plain copy
  1. package zhangphil.listview;  
  2.   
  3. import com.ecloud.pulltozoomview.PullToZoomListViewEx;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.util.DisplayMetrics;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10. import android.widget.AbsListView;  
  11. import android.widget.AdapterView;  
  12. import android.widget.ArrayAdapter;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.           
  21.         PullToZoomListViewEx listView = (PullToZoomListViewEx) findViewById(R.id.listview);  
  22.   
  23.         String[] data=new String[100];  
  24.         for(int i=0;i<data.length;i++){  
  25.             data[i]="测试数据 @ Zhang Phil "+i;  
  26.         }  
  27.           
  28.         listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));  
  29.         listView.getPullRootView().setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  30.             @Override  
  31.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  32.                 Log.d("position""position = " + position);  
  33.             }  
  34.         });  
  35.   
  36.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  37.             @Override  
  38.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  39.                 Log.d("position""position = " + position);  
  40.             }  
  41.         });  
  42.   
  43.         setPullToZoomListViewExHeaderLayoutParams(listView);  
  44.     }  
  45.       
  46.     //设置头部的View的宽高。  
  47.     private void    setPullToZoomListViewExHeaderLayoutParams(PullToZoomListViewEx listView){  
  48.                     
  49.         DisplayMetrics localDisplayMetrics = new DisplayMetrics();  
  50.         getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);  
  51.         int mScreenHeight = localDisplayMetrics.heightPixels;  
  52.         int mScreenWidth = localDisplayMetrics.widthPixels;  
  53.         AbsListView.LayoutParams localObject = new AbsListView.LayoutParams(mScreenWidth, (int) (9.0f * (mScreenWidth / 16.0F)));  
  54.         listView.setHeaderLayoutParams(localObject);  
  55.     }  
  56. }  


运行的结果就是前文中的动态效果。


静态图1,此为初始状态:




静态图2,此为下拉伸展时候的状态:




头部的素材图片image.jpg原图:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值