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;
            }
            
        }
  • 相关阅读:
    实现路由的RouterMiddleware中间件
    AI: Jarvis
    使用管道(PipeLine)和批量(Batch)操作
    OSWatcher Black Box
    NET Core HTTP 管道
    开源项目!
    Router的创建者——RouteBuilder
    .NET Core)的ZooKeeper异步客户端
    single-write-database-connection
    Quartz与Spring集成 Job如何自动注入Spring容器托管的对象
  • 原文地址:https://www.cnblogs.com/taobox/p/3357276.html
Copyright © 2011-2022 走看看