博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

android view之imageswitcher

Posted on 2012-08-25 21:07  J_turn  阅读(261)  评论(0)    收藏  举报
 1 public class imageswitcher extends Activity implements
 2         AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
 3 
 4     @Override
 5     public void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7 
 8         setContentView(R.layout.main);
 9 
10         mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
11         mSwitcher.setFactory(this);
12         mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
13                 android.R.anim.fade_in));
14         mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
15                 android.R.anim.fade_out));
16 
17         Gallery g = (Gallery) findViewById(R.id.gallery);
18         g.setAdapter(new ImageAdapter(this));
19         g.setOnItemSelectedListener(this);
20     }
21     
22     public void onItemSelected(AdapterView parent, View v, 
23                                 int position, long id) {
24         mSwitcher.setImageResource(mImageIds[position]);
25     }
26 
27     public void onNothingSelected(AdapterView parent) {
28     }
29     
30     public View makeView() {
31         ImageView i = new ImageView(this);
32         i.setScaleType(ImageView.ScaleType.FIT_CENTER);
33         i.setLayoutParams(new ImageSwitcher.LayoutParams(
34                 LayoutParams.FILL_PARENT,
35                 LayoutParams.FILL_PARENT));
36         return i;
37     }
38 
39     private ImageSwitcher mSwitcher;
40 
41     public class ImageAdapter extends BaseAdapter {
42         public ImageAdapter(Context c) {
43             mContext = c;
44         }
45 
46         public int getCount() {
47             return mImageIds.length;
48         }
49 
50         public Object getItem(int position) {
51             return position;
52         }
53 
54         public long getItemId(int position) {
55             return position;
56         }
57 
58         public View getView(int position, View convertView, 
59                             ViewGroup parent) {
60             ImageView i = new ImageView(mContext);
61 
62             i.setImageResource(mImageIds[position]);
63             i.setAdjustViewBounds(true);
64             i.setLayoutParams(new Gallery.LayoutParams(
65                     LayoutParams.WRAP_CONTENT, 
66                     LayoutParams.WRAP_CONTENT));
67             return i;
68         }
69 
70         private Context mContext;
71     }
72 
73     private Integer[] mImageIds = {
74             R.drawable.photo1, R.drawable.photo2, 
75             R.drawable.photo3, R.drawable.photo4, 
76             R.drawable.photo5, R.drawable.photo6,
77             R.drawable.photo7, R.drawable.photo8};
78 }