Android Gallery 的簡單使用

來源:互聯網
上載者:User

Android的Gallery控制項是個很不錯的看圖控制項,大大減輕了開發人員對於看圖功能的開發,而且效果也比較美觀。本文介紹Gallery的用法,用反射機制來動態讀取資源中的圖片。

       本文的:

 

 

main.xml源碼:

[xhtml] 

<?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" 
    > 
<Gallery android:id="@+id/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent"></Gallery> 
</LinearLayout> 
 

程式源碼:

[java] 

package com.testImageView; 
 
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.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 testImageView extends Activity { 
    private Gallery mGallery; 
    @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) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (IllegalAccessException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        mGallery.setOnItemClickListener(new OnItemClickListener() { 
            public void onItemClick(AdapterView parent, View v, int position, long id) { 
                testImageView.this.setTitle(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); 
                    size[0]=bmImg.getWidth();size[1]=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; 
        } 
         
    }; 
}
作者:Eric

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.