Android基础TOP6_3:Gally和ImageSwitcher实现画廊

结构:

 

Activity:

 

 1 <LinearLayout 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     android:orientation="vertical"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.top6_3.MainActivity" >
11    <Gallery 
12     android:id="@+id/gly"
13     android:layout_height="60dp"
14     android:layout_width="fill_parent"
15     android:animationDuration="1000"
16     android:gravity="center_vertical"
17     android:spacing="20dp"
18     android:unselectedAlpha="0.5"
19     android:background="#55000000"
20      />
21 <ImageSwitcher 
22     android:paddingTop="10dp"
23     android:id="@+id/is"
24     android:layout_width="wrap_content"
25     android:layout_height="match_parent"></ImageSwitcher>
26 
27 </LinearLayout>

JAVA:

MainActivity类:
 1 package com.example.top6_3;
 2 
 3 
 4 
 5 
 6 import android.app.Activity;
 7 import android.os.Bundle;
 8 import android.view.Menu;
 9 import android.view.MenuItem;
10 import android.view.View;
11 import android.view.animation.Animation;
12 import android.view.animation.AnimationUtils;
13 import android.widget.Gallery.LayoutParams;
14 import android.widget.AdapterView;
15 import android.widget.Gallery;
16 import android.widget.ImageSwitcher;
17 import android.widget.ImageView;
18 import android.widget.ViewSwitcher.ViewFactory;
19 
20 
21 public class MainActivity extends Activity {
22     private Gallery gal;
23     private ImageView iv;
24     private ImageAdapter adapter; 
25     private ImageSwitcher is;
26     private static final int[]  imgslds={
27         R.drawable.bjl,
28         R.drawable.dfhgc,
29         R.drawable.fg,
30         R.drawable.gj
31 };
32     @Override
33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35         setContentView(R.layout.activity_main);
36          is=(ImageSwitcher) findViewById(R.id.is);
37          is.setFactory(new ViewFactory() {
38             
39             @Override
40             public View makeView() {
41                 // TODO Auto-generated method stub
42                 ImageView i=new ImageView(getApplicationContext());
43                 //i.setBackgroundColor(0Xff000000);
44                 i.setScaleType(ImageView.ScaleType.FIT_CENTER);
45                 i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
46                         LayoutParams.MATCH_PARENT));
47                 return i;
48             }
49         });
50          is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
51       is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
52       gal=(Gallery) findViewById(R.id.gly);
53       adapter=new ImageAdapter(this);
54       gal.setAdapter(adapter);
55        gal.setOnItemSelectedListener(gaILidstener);
56        }
57            private Gallery.OnItemSelectedListener gaILidstener=new Gallery.OnItemSelectedListener() {
58 
59            @Override
60            public void onItemSelected(AdapterView<?> parent, View view,
61                    int position, long id) {
62                // TODO Auto-generated method stub
63                is.setImageResource(imgslds[position]);
64            }
65 
66            @Override
67            public void onNothingSelected(AdapterView<?> parent) {
68                // TODO Auto-generated method stub
69                
70            }
71     
72            };
73 }

ImageAdapter类:

 1 package com.example.top6_3;
 2 
 3 import android.R.integer;
 4 import android.content.Context;
 5 import android.view.Gravity;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.BaseAdapter;
 9 import android.widget.Gallery;
10 import android.widget.ImageView;
11 
12 public class ImageAdapter extends BaseAdapter {
13     private Context context;
14     
15     private static final int[]  imgslds={
16         R.drawable.bjl,
17         R.drawable.dfhgc,
18         R.drawable.fg,
19         R.drawable.gj
20 };
21     public ImageAdapter(Context c){
22         context=c;
23         
24     }
25     public int getCount() {
26         // TODO Auto-generated method stub
27         //
28         return imgslds.length;
29         
30     }
31 
32     @Override
33     public Object getItem(int position) {
34         // TODO Auto-generated method stub
35         return position;
36     }
37 
38     @Override
39     public long getItemId(int position) {
40         // TODO Auto-generated method stub
41         return position;
42     }
43 
44     @Override
45     public View getView(int position, View convertView, ViewGroup parent) {
46         // TODO Auto-generated method stub
47         ImageView iv=new ImageView(context);
48         iv.setImageResource(imgslds[position]);
49         iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
50         iv.setLayoutParams(new Gallery.LayoutParams(120,80));
51         //iv.setBackgroundResource(R.drawable.bjl);
52         return iv; 
53         
54     }
55 
56 }

效果图:

 

posted @ 2017-04-12 13:30  AndrioidCSY  阅读(282)  评论(0编辑  收藏  举报