zoukankan      html  css  js  c++  java
  • 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;
            }
            
        }
  • 相关阅读:
    GSS3 SPOJ 1716. Can you answer these queries III gss1的变形
    GSS1 spoj 1043 Can you answer these queries I 最大子段和
    Codeforces Round #197 (Div. 2) C,D两题
    sgu 185 最短路建网络流
    CF 208E
    QTREE2 spoj 913. Query on a tree II 经典的倍增思想
    BZOJ 1146: [CTSC2008]网络管理Network 树链剖分+线段树+平衡树
    ubuntu安装vim
    历史背景更新模型
    码本模型
  • 原文地址:https://www.cnblogs.com/taobox/p/3357276.html
Copyright © 2011-2022 走看看