ImageSwitch + Gallery 图片展示

效果如下:

imageimageimage

 

 

包含一个java程序文件,一个 xml布局文件

java代码如下:

public class Main extends Activity {
 
    private static final String tag = "==CrazyIt.org==";
    int[] imgs = new int[] { R.drawable.x1, R.drawable.x2, R.drawable.x3,
            R.drawable.x5, R.drawable.x4, R.drawable.x1, R.drawable.x2,
            R.drawable.x3, R.drawable.x5, R.drawable.x4 };
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        final Gallery gallery = (Gallery) findViewById(R.id.g1);
        final ImageSwitcher is = (ImageSwitcher) findViewById(R.id.is1);
 
        is.setFactory(new ViewFactory() {
 
            public View makeView() {
                // TODO Auto-generated method stub
 
                ImageView iv = new ImageView(Main.this);
                iv.setBackgroundColor(0xff0000);
                iv.setScaleType(ScaleType.FIT_CENTER);
                iv.setLayoutParams(new ImageSwitcher.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
 
                return iv;
            }
        });
 
        BaseAdapter ba = new BaseAdapter() {
 
            public int getCount() {
                // TODO Auto-generated method stub
                return imgs.length;
            }
 
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }
 
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
 
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                ImageView iv = new ImageView(Main.this);
                iv.setImageResource(imgs[position % imgs.length]);
                iv.setScaleType(ScaleType.FIT_XY);
                iv.setLayoutParams(new Gallery.LayoutParams(75, 100));
                return iv;
 
            }
        };
        
        gallery.setAdapter(ba);
        
        gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
 
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {
                // TODO Auto-generated method stub
                is.setImageResource(imgs[pos%imgs.length]);
                
            }
 
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <ImageSwitcher
        android:id="@+id/is1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3" >
    </ImageSwitcher>
<Gallery
        android:id="@+id/g1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:spacing="20dip" />
</LinearLayout>
        
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

 

xml布局文件:

posted @ 2012-11-16 14:44  高捍得  阅读(495)  评论(0编辑  收藏  举报