安卓Gallery配合ImageSwitcher不显示图片

Gallary装的是缩略图(thumb),ImageSwitcher装的是大图。

 

 

不显示图片的一个可能原因是gallery没设置代理器,另一个原因是没使用相对布局。

 

 

GalleryActivity.java:

 1 public class GalleryActivity extends Activity implements OnItemSelectedListener, ViewFactory {
 2     /** Called when the activity is first created. */
 3     private ImageSwitcher is;
 4     private Gallery gallery;
 5     @Override
 6     public void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         requestWindowFeature(Window.FEATURE_NO_TITLE);
 9         setContentView(R.layout.main);
10         
11         is = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
12         is.setFactory(this);
13         gallery = (Gallery)findViewById(R.id.gallery1);
14         gallery.setAdapter(new ImageAdapter(this));
15         gallery.setOnItemSelectedListener(this);
16     }
17 
18 
19     @Override
20     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
21             long arg3) {
22         // TODO Auto-generated method stub
23         is.setImageResource(mImageIds[arg2]);
24         
25     }
26 
27     @Override
28     public View makeView() {
29         // TODO Auto-generated method stub
30         ImageView iv = new ImageView(this);
31         iv.setBackgroundColor(0xFF000000);
32         iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
33         iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
34         
35         return iv;
36     }
37     @Override
38     public void onNothingSelected(AdapterView<?> arg0) {
39         // TODO Auto-generated method stub
40         
41     }
42     class ImageAdapter extends BaseAdapter {
43         private Context ctx;
44         public ImageAdapter(Context ctx) {
45             this.ctx = ctx;
46         }
47         @Override
48         public int getCount() {
49             // TODO Auto-generated method stub
50             return mThumbIds.length-1;
51         }
52 
53         @Override
54         public Object getItem(int position) {
55             // TODO Auto-generated method stub
56             return mThumbIds[position];
57         }
58 
59         @Override
60         public long getItemId(int position) {
61             // TODO Auto-generated method stub
62             return position;
63         }
64 
65         @Override
66         public View getView(int position, View convertView, ViewGroup parent) {
67             // TODO Auto-generated method stub
68             ImageView iv = new ImageView(ctx);
69             iv.setBackgroundColor(0x000000);
70             iv.setImageResource(mThumbIds[position]);
71             iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
72             iv.setBackgroundResource(R.drawable.picture_frame);
73             return iv;
74         }
75         
76         
77     }
78     private int[] mThumbIds = {
79             R.drawable.sample_thumb_0,
80             R.drawable.sample_thumb_1,
81             R.drawable.sample_thumb_2,
82             R.drawable.sample_thumb_3,
83             R.drawable.sample_thumb_4,
84             R.drawable.sample_thumb_5,
85             R.drawable.sample_thumb_6,
86             R.drawable.sample_thumb_7,
87     };
88     private int[] mImageIds = {
89             R.drawable.sample_0,
90             R.drawable.sample_1,
91             R.drawable.sample_2,
92             R.drawable.sample_3,
93             R.drawable.sample_4,
94             R.drawable.sample_5,
95             R.drawable.sample_6
96     };
97     
98     
99 }
View Code

 

main.xml(layout):

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3        xmlns:android="http://schemas.android.com/apk/res/android"
 4        android:layout_width="fill_parent"
 5        android:layout_height="fill_parent">
 6        
 7        <ImageSwitcher 
 8        android:id="@+id/imageSwitcher1"
 9               android:layout_width="fill_parent" 
10               android:layout_height="fill_parent"
11               android:layout_alignParentTop="true"
12               android:layout_alignParentLeft="true" />
13               
14        <Gallery android:id="@+id/gallery1" 
15        android:background="#55000000"
16               android:layout_width="fill_parent" 
17               android:layout_height="60dp"
18               android:layout_alignParentBottom="true"
19               android:layout_alignParentLeft="true"
20               android:gravity="center_vertical" 
21               android:spacing="16dp" />
22 </RelativeLayout>
View Code

 

posted @ 2013-11-20 23:04  -六月飞雪-  阅读(443)  评论(0编辑  收藏  举报