zoukankan      html  css  js  c++  java
  • android 这 Gallery的使用

    原文引用:http://blog.csdn.net/hellogv/article/details/5961525

    使用 Gallery 查看图片:

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:id="@+id/testImageView"
        >  
        
    <Gallery 
      android:id="@+id/gallery"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      ></Gallery>  
    </LinearLayout>
    

    GallerydemoActivity.java

    package com.hkrt;
    
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.AdapterView.OnItemClickListener;
    
    public class GallerydemoActivity extends Activity {
    	private Gallery mGallery;  
    //	/获取手机屏幕分辨率的类   
        private DisplayMetrics dm;  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
              
            mGallery = (Gallery)findViewById(R.id.gallery);  
            try {  
                mGallery.setAdapter(new ImageAdapter(this));  
            } catch (IllegalArgumentException e) {  
                e.printStackTrace();  
            } catch (IllegalAccessException e) {  
                e.printStackTrace();  
            }  
            mGallery.setOnItemClickListener(new OnItemClickListener() {  
                public void onItemClick(AdapterView parent, View v, int position, long id) {
    //                TextView testImageView = (TextView)  findViewById(R.id.testImageView);
    //                testImageView.setText(String.valueOf(position));  
                }  
            });  
        }  
          
        /* 
         * class ImageAdapter is used to control gallery source and operation. 
         */  
        private class ImageAdapter extends BaseAdapter{  
            private Context mContext;  
            private ArrayList<Integer> imgList=new ArrayList<Integer>();  
            private ArrayList<Object> imgSizes=new ArrayList<Object>();  
            public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{  
                mContext = c;  
                  
                //用反射机制来获取资源中的图片ID和尺寸  
                Field[] fields = R.drawable.class.getDeclaredFields();  
                for (Field field : fields)  
                {  
                    if (!"icon".equals(field.getName()))//除了icon之外的图片  
                    {     
                        int index=field.getInt(R.drawable.class);  
                        //保存图片ID  
                        imgList.add(index);  
                        //保存图片大小  
                        int size[]=new int[2];  
                        Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);  
                        dm = new DisplayMetrics();   
                        getWindowManager().getDefaultDisplay().getMetrics(dm);   
                                //获得手机的宽度和高度像素单位为px   
                        size[0]=dm.widthPixels;//bmImg.getWidth();
                        size[1]=dm.heightPixels;//bmImg.getHeight(); 
                        imgSizes.add(size);  
                    }  
                }  
            }  
            @Override  
            public int getCount() {  
                // TODO Auto-generated method stub  
      
                return imgList.size();  
            }  
      
            @Override  
            public Object getItem(int position) {  
                // TODO Auto-generated method stub  
      
                return position;  
            }  
      
            @Override  
            public long getItemId(int position) {  
                // TODO Auto-generated method stub  
      
                return position;  
            }  
      
            @Override  
            public View getView(int position, View convertView, ViewGroup parent) {  
                // TODO Auto-generated method stub  
      
                ImageView i = new ImageView (mContext);  
                //从imgList取得图片ID  
                i.setImageResource(imgList.get(position).intValue());  
                i.setScaleType(ImageView.ScaleType.FIT_XY);  
                //从imgSizes取得图片大小  
                int size[]= new int[2];  
                size=(int[]) imgSizes.get(position);  
                i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));  
                return i;  
            }  
              
        };  
    }
    效果图:


  • 相关阅读:
    编写通用shell脚本启动java项目,适用于多数服务,只需修改服务名即可
    linux操作系统安装jdk
    搭建dubbo+zookeeper+dubboadmin分布式服务框架(windows平台下)
    二.传统服务的拆分策略
    一.把传统服务做成dubbo分布式服务架构的步骤
    Android IOS WebRTC 音视频开发总结(六)-- iOS开发之含泪经验
    Android WebRTC 音视频开发总结(五)-- webrtc开发原型
    Android WebRTC 音视频开发总结(四)-- webrtc传输模块
    Android WebRTC 音视频开发总结(三)-- 信令服务和媒体服务
    Android WebRTC 音视频开发总结(二)-- webrtcdemo介绍
  • 原文地址:https://www.cnblogs.com/java20130726/p/3218350.html
Copyright © 2011-2022 走看看