Android入门第十二篇之Gallery
16lz
2021-01-26
Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。本文介绍Gallery的用法,用反射机制来动态读取资源中的图片。
本文的效果图:
main.xml源码:
view plain copy to clipboard print ?
- <? 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 plain copy to clipboard print ?
- 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 GallerymGallery;
- @Override
- public void onCreate(BundlesavedInstanceState){
- super .onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mGallery=(Gallery)findViewById(R.id.gallery);
- try {
- mGallery.setAdapter(new ImageAdapter( this ));
- }catch (IllegalArgumentExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch (IllegalAccessExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- mGallery.setOnItemClickListener(new OnItemClickListener(){
- public void onItemClick(AdapterViewparent,Viewv, int position, long id){
- testImageView.this .setTitle(String.valueOf(position));
- }
- });
- }
- /*
- *classImageAdapterisusedtocontrolgallerysourceandoperation.
- */
- private class ImageAdapter extends BaseAdapter{
- private ContextmContext;
- private ArrayList<Integer>imgList= new ArrayList<Integer>();
- private ArrayList<Object>imgSizes= new ArrayList<Object>();
- public ImageAdapter(Contextc) throws IllegalArgumentException,IllegalAccessException{
- mContext=c;
- //用反射机制来获取资源中的图片ID和尺寸
- Field[]fields=R.drawable.class .getDeclaredFields();
- for (Fieldfield:fields)
- {
- if (! "icon" .equals(field.getName())) //除了icon之外的图片
- {
- int index=field.getInt(R.drawable. class );
- //保存图片ID
- imgList.add(index);
- //保存图片大小
- int size[]= new int [ 2 ];
- BitmapbmImg=BitmapFactory.decodeResource(getResources(),index);
- size[0 ]=bmImg.getWidth();size[ 1 ]=bmImg.getHeight();
- imgSizes.add(size);
- }
- }
- }
- @Override
- public int getCount(){
- //TODOAuto-generatedmethodstub
- return imgList.size();
- }
- @Override
- public ObjectgetItem( int position){
- //TODOAuto-generatedmethodstub
- return position;
- }
- @Override
- public long getItemId( int position){
- //TODOAuto-generatedmethodstub
- return position;
- }
- @Override
- public ViewgetView( int position,ViewconvertView,ViewGroupparent){
- //TODOAuto-generatedmethodstub
- ImageViewi=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技术积累:图片缓存管理
- Android学习笔记(三)UI
- 更换android的初始化图片
- Android(安卓)开发注意事项
- 【转】【Android】获取手机中已安装apk文件信息(PackageInfo、Re
- Android使用ImageLoader异步加载网络图片(二)结合listview
- Android(安卓)关于嵌套listView时onItemClick事件不响应的解决办
- Android图片放大缩小实现方式(一)
- 如何实现一个图片加载框架