展会信息港展会大全

Android入门第十二篇之Gallery
来源:互联网   发布日期:2015-10-03 10:45:21   浏览:1923次  

导读:Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。本文介绍Gallery的用法,用反射机制来动态读取资源中的图片。 ...

Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。本文介绍Gallery的用法,用反射机制来动态读取资源中的图片。

本文的效果图:

main.xml源码:

view plaincopy to clipboardprint?<?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><?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>

程序源码:

view plaincopy to clipboardprint?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;@Overridepublic 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 blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.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);//保存图片IDimgList.add(index);//保存图片大小int size[]=new int[2];Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();imgSizes.add(size);}}}@Ove

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港