`
xindrace
  • 浏览: 95720 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android入门第十二篇之Gallery

 
阅读更多

 本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

       Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。本文介绍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"  
    >  
<Gallery android:id="@+id/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent"></Gallery>  
</LinearLayout>  

 

程序源码:

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;  
        }  
          
    };  
}  

 

 

分享到:
评论

相关推荐

    Android入门第十二篇之Gallery.doc

    在"Android入门第十二篇之Gallery"这篇文档中,我们将探讨如何使用Gallery控件,并通过反射机制动态加载资源中的图片。 首先,我们来看一下`main.xml`布局文件。这个XML文件定义了一个垂直布局的LinearLayout,其中...

    Android入门到精通源代码.

    第12章 Android的搜索引擎和Gtalk开发 12.1 搜索引擎在手机中的应用 12.1.1 本地搜索 12.1.2 Web搜索 12.2 Android搜索引擎API简介 12.3 应用实例详解:过滤式搜索 引擎程序 12.3.1 实例分析 12.3.2 实例实现 12.4 ...

    Google.Android开发入门与实战

    第12章 Android综合案例一——RSS阅读器实例 12.1 RSS介绍 12.1.1 RSS基础 12.1.2 RSS的历史 12.1.3 RSS语法介绍 12.2 SAX介绍 12.2.1 SAX基础 12.2.2 使用SAX的作用 12.2.3 怎样使用SAX 12.3 RSS阅读器设计 12.3.1 ...

    android开发资料大全

    android入门与提高必看指南 Android入门逆引手册 Android开发指南中文版、创意设计 【Android系统原理与开发要点详解】/底层 应用 框架 Android核心分析28篇,强烈推荐android初学者,android进阶者看看这个系列...

    Google Android开发入门与实战的代码

    第12章 Android综合案例一——RSS阅读器实例 205 12.1 RSS介绍 205 12.1.1 RSS基础 205 12.1.2 RSS的历史 205 12.1.3 RSS语法介绍 206 12.2 SAX介绍 207 12.2.1 SAX基础 207 12.2.2 使用SAX的...

    老罗android视频开发源码和ppt经典

    一、Android入门介绍 视频教程 1.1 android系统介绍 1.2 android系统框架的介绍 1.3 如何搭建android开发环境 1.4 android生命周期的介绍 1.5 android使用全局变量传递数据 1.6 android使用剪切板传递数据 1.7 意图...

    Android学习新手笔记

    标题《Android学习新手笔记》所涉及的知识点: 1. Android背景知识 ...这份笔记可以作为学习Android开发的入门教材,它覆盖了从基础到实战应用的许多知识点,为未来深入学习Android打下坚实的基础。

    Android游戏与应用开发最佳学习路线图收集.pdf

    - **Hello World**:编写第一个Android应用,理解项目结构。 - **四大组件**:深入理解Activity、Intent、Service、BroadcastReceiver和ContentProvider,以及它们在应用中的作用。 11. **UI设计**: - **UI组件...

Global site tag (gtag.js) - Google Analytics