Android学习笔记 Gallery图库组件的使用

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:gravity="bottom" >
    <ImageSwitcher 
        android:id="@+id/myImageSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp" />
    <Gallery 
        android:id="@+id/myGallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

 

grid_lyout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView 
        android:id="@+id/myImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        />

</LinearLayout>

 

MainActivity.java

public class MainActivity extends Activity {
    private Gallery myGallery=null;
    private SimpleAdapter mySimpleAdapter=null;
    private List<Map<String,Integer>> list=new ArrayList<Map<String,Integer>>();
    private ImageSwitcher myImageSwitcher=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        this.myGallery=(Gallery)super.findViewById(R.id.myGallery);
        this.myGallery.setOnItemClickListener(new OnItemClickListenerImpl());
        this.initAdapter();
        this.myGallery.setAdapter(this.mySimpleAdapter);
        this.myImageSwitcher=(ImageSwitcher)super.findViewById(R.id.myImageSwitcher);
        this.myImageSwitcher.setFactory(new ViewFactoryImpl());
    }
    
    private void initAdapter(){
        Field[] fields=R.drawable.class.getDeclaredFields();//取得全部属性
        for (int i = 0; i < fields.length; i++) {
            if(fields[i].getName().startsWith("png_")){ //以png_开头的文件
                Map<String,Integer> map=new HashMap<String, Integer>();
                try{
                map.put("img", fields[i].getInt(R.drawable.class));
                }catch(Exception e){
                    
                }
                this.list.add(map);
            }
        }
        this.mySimpleAdapter=new SimpleAdapter(
                this,
                this.list,
                R.layout.grid_layout,
                new String[] {"img"},
                new int[]{R.id.myImg} 
        );
    }
    
    private class OnItemClickListenerImpl implements OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int postion,
                long id) {
            // TODO Auto-generated method stub
            //Toast.makeText(MainActivity.this, String.valueOf(postion), Toast.LENGTH_SHORT).show();
            Map<String, Integer> map=(Map<String,Integer>) parent.getAdapter().getItem(postion);
            MainActivity.this.myImageSwitcher.setImageResource(map.get("img"));
        }
        
    }

    private class ViewFactoryImpl implements ViewFactory{

        @Override
        public View makeView() {
            ImageView img=new ImageView(MainActivity.this);
            img.setBackgroundColor(0xFFFFFFFF);
            img.setScaleType(ImageView.ScaleType.CENTER);
            img.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
            return img;
        }
        
    }

 

posted @ 2013-10-08 16:03  寂静之秋  阅读(316)  评论(0编辑  收藏  举报
哈尔滨八零网