android pullzoomview是github上面的一個第三方開源項目,該項目實現的功能被新浪微博的移動端廣泛使用,其效果就是,當用戶在下拉過程中,頭部的圖片會有一定的拉伸,當用戶松開時候,圖片又收縮復位,下載地址:https://github.com/frank-zhu/pullzoomview
pullzoomview要實現兩類,一類是典型的android listview,另外一類是android 的scroll view。本文先介紹pullzoomview在listview上的實現:pulltozoomlistviewex。
首先需要把pulltozoomlistviewex像listview一樣寫進布局:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<relativelayout 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" tools:context= "com.zzw.testpullzoomview.mainactivity" > <com.ecloud.pulltozoomview.pulltozoomlistviewex xmlns:custom= "http://schemas.android.com/apk/res-auto" android:id= "@+id/listview" android:layout_width= "match_parent" android:layout_height= "match_parent" custom:headerview= "@layout/head_view" custom:zoomview= "@layout/head_zoom_view" /> </relativelayout> |
需要注意的是,需要定義一個headerview:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
custom:headerview= "@layout/head_view" <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/layout_view" android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_gravity= "bottom" android:gravity= "bottom" > <imageview android:id= "@+id/iv_user_head" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_centerinparent= "true" android:background= "@android:color/holo_red_light" android:src= "@drawable/ic_launcher" /> <textview android:id= "@+id/tv_user_name" android:textsize= "sp" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/iv_user_head" android:layout_centerhorizontal= "true" android:text= "新浪微博" android:textcolor= "#ffffff" /> <linearlayout android:id= "@+id/ll_action_button" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:background= "#" android:layout_alignparentbottom= "true" android:padding= "dip" > <textview android:id= "@+id/tv_register" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "注冊" android:layout_weight= "" android:textsize= "sp" android:gravity= "center" android:layout_gravity= "center" android:textcolor= "#ffffff" /> <textview android:id= "@+id/tv_login" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "登錄" android:layout_weight= "" android:textsize= "sp" android:gravity= "center" android:layout_gravity= "center" android:textcolor= "#ffffff" /> </linearlayout> </relativelayout> |
此處的headerview是位于pulltozoomlistviewex頭部的一個子布局,里面定義一些控件將出現在pulltozoomlistviewex的頭部,但此處的headerview并不會縮放,只是可以看到此處的headerview在隨著下拉過程中移位。
而定義的custom:zoomview:
1
2
3
4
5
6
7
8
9
|
custom:zoomview= "@layout/head_zoom_view" <?xml version= "." encoding= "utf-" ?> <imageview xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/imageview" android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_gravity= "center_horizontal" android:scaletype= "centercrop" android:src= "@drawable/a" /> |
head_zoom_view其實就是簡單的放一張圖片。
則是真正的要縮放伸展的view,此處通常會放置一張圖片,在用戶下拉過程中滑動縮放,產生奇妙的視覺效果。
在一定程度上講,zoomview是襯托在headerview底下的。headerview是一個正常顯示的android view布局,而zoomview則是可以產生動態縮放和收縮效果的特殊zoom view。
寫一個完整的例子加以說明。
java代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package com.zzw.testpullzoomview; import com.ecloud.pulltozoomview.pulltozoomlistviewex; import android.app.activity; import android.os.bundle; import android.util.displaymetrics; import android.util.log; import android.view.view; import android.widget.abslistview; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); pulltozoomlistviewex listview = (pulltozoomlistviewex) findviewbyid(r.id.listview); string data[] = new string[]; for ( int i = ; i < data.length; i++) { data[i] = "測試數據" + i; } listview.setadapter( new arrayadapter<string>( this , android.r.layout.simple_list_item_, data)); listview.getpullrootview().setonitemclicklistener( new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { log.d( "position" , "getpullrootview--->position = " + position); } }); listview.setonitemclicklistener( new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { log.d( "position" , "position = " + position); } }); setpulltozoomlistviewexheaderlayoutparams(listview); } // 設置頭部的view的寬高。 private void setpulltozoomlistviewexheaderlayoutparams(pulltozoomlistviewex listview) { displaymetrics localdisplaymetrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(localdisplaymetrics); int mscreenheight = localdisplaymetrics.heightpixels; int mscreenwidth = localdisplaymetrics.widthpixels; abslistview.layoutparams localobject = new abslistview.layoutparams(mscreenwidth, ( int ) (.f * (mscreenwidth / .f))); listview.setheaderlayoutparams(localobject); } } |
以上所述是本文關于新浪微博第三方登錄界面上下拉伸圖片之第三方開源pulltozoomlistviewex(一)的全部敘述,希望大家喜歡,下篇給大家介紹新浪微博第三方登錄界面上下拉伸圖片之第三方開源pulltozoomlistviewex(二),希望大家繼續關注。