Android BaseAdapter Gallery 畫廊視圖 (左右拖動圖片列表拖至中間時圖片放大顯示)


畫廊視圖使用Gallery表示,能夠按水平方向顯示內容,並且可以手指直接拖動圖片和移動,一般用
來瀏覽圖片,,被選中的選項位於中間,並且可以響應事件顯示信息.在使用畫廊視圖時,首先在屏幕
上添加Gallery組件,通常使用標記在XML而布局文件中添加.

  

畫廊視圖在4.0后已經過期,但是我們仍然可以使用,不過后面我們將用水平的ListView
自定義組件來實現這個效果,在自定義視圖中講解。

 

常用屬性:
1. android:animationDuration 用於設置列表項切換時的動畫持續時間
2. android:gravity 用於設置對齊方式
3. android:spacing 用於設置列表項之間的間距
4. android:unselectedAlpha 用於設置沒有選中的列表項的透明度

 

()

1、搭建布局

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent" >
 4 
 5     <ImageView
 6         android:id="@+id/img"
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:layout_centerHorizontal="true"
10         android:adjustViewBounds="true"
11         android:scaleType="fitXY"
12         android:layout_margin="30dp"
13         android:src="@drawable/ic_launcher" />
14 
15     <Gallery
16         android:id="@+id/gallery"
17         android:layout_below="@id/img"
18         android:layout_margin="20dp"
19         android:spacing="10dp"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content" />
22 
23 </RelativeLayout>
activity_main.xml

2、MainActivity.java中將所有組件找到,並設置監聽

 1 public class MainActivity extends Activity implements OnItemSelectedListener {
 2 
 3     Gallery gallery;
 4     ImageView img;
 5     int[] imgId = new int[6];
 6     
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         initView();
12         MyBaseAdapter adapter = new MyBaseAdapter();
13         gallery.setAdapter(adapter);
14         gallery.setOnItemSelectedListener(this);
15         
16         
17     }
18 
19     private void initView() {
20         for (int i = 0; i < imgId.length; i++) {
21             try {
22                 imgId[i] = R.drawable.class.getField("img0"+(i+1)).getInt(null);
23             } catch (Exception e) {
24                 e.printStackTrace();
25             } 
26         }
27         
28         gallery = (Gallery) findViewById(R.id.gallery);
29         img = (ImageView) findViewById(R.id.img);
30     }
31     
32     class MyBaseAdapter extends BaseAdapter{
33 
34         @Override
35         public int getCount() {
36             return imgId.length;
37         }
38 
39         @Override
40         public Object getItem(int position) {
41             return null;
42         }
43 
44         @Override
45         public long getItemId(int position) {
46             // TODO Auto-generated method stub
47             return 0;
48         }
49 
50         @Override
51         public View getView(int position, View convertView, ViewGroup parent) {
52             if(convertView == null){
53                 convertView = new ImageView(MainActivity.this);
54             }
55             ImageView iv = (ImageView) convertView;
56             
57             //設置數據
58             iv.setImageResource(imgId[position]);
59             
60             return convertView;
61         }
62         
63     }
64 
65     @Override
66     public void onItemSelected(AdapterView<?> parent, View view, int position,
67             long id) {
68         
69         img.setImageResource(imgId[position]);
70         
71     }
72 
73     @Override
74     public void onNothingSelected(AdapterView<?> parent) {
75         // TODO Auto-generated method stub
76         
77     }
78 
79     
80 }
MainActivity.java

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM