惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 生如夏花之灿烂

本人讲课时录制的Android应用开发技术教学视频 jsoup使用样式class抓取数据时空格的处理 我的第一款anroid软件作品--短信精灵1.0 (原创)Android入门教程(三十六)------实现手机联系人的全选 Android入门教程(三十五)------在Android上使用ZXing识别条形码/二维码(转) - 生如夏花之灿烂 Android入门教程(三十四)------之多级树形菜单的实现 (转) Android入门教程(三十三)------之ListView自适应实现表格(转) Android入门教程(三十二)------之SQLite分页表格(转) Android入门教程(三十一)------SQLite分页读取(转) Android入门教程(三十)------之XML解析与生成(转) - 生如夏花之灿烂 Android入门教程(二十九)------之BroadcastReceiver (转) Android入门教程(二十八)------之Service(转) Android入门教程(二十七)------之Style与Theme (转) Android入门教程(二十六)------之ActivityGroup + GridView 实现Tab分页标签(转) Android入门教程(二十五)------之画图(转) Android入门教程(二十四)------之Gallery + ImageSwitcher(转) Android入门教程(二十二)------之TabHost,TabWidget(转) Android入门教程(二十一)------之PopupWindow (转) Android入门教程(二十)之--之AlertDialog(转)
Android入门教程(二十三)------之Gallery(转)
生如夏花之灿烂 · 2011-08-11 · via 博客园 - 生如夏花之灿烂

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

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

       本文的效果图:

main.xml源码:

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

程序源码:

  1. package com.testImageView;  
  2.   
  3. import java.lang.reflect.Field;  
  4. import java.util.ArrayList;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.graphics.Bitmap;  
  9. import android.graphics.BitmapFactory;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.widget.AdapterView;  
  14. import android.widget.BaseAdapter;  
  15. import android.widget.Gallery;  
  16. import android.widget.ImageView;  
  17. import android.widget.AdapterView.OnItemClickListener;  
  18.   
  19. public class testImageView extends Activity {  
  20.     private Gallery mGallery;  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.           
  26.         mGallery = (Gallery)findViewById(R.id.gallery);  
  27.         try {  
  28.             mGallery.setAdapter(new ImageAdapter(this));  
  29.         } catch (IllegalArgumentException e) {  
  30.               
  31.             e.printStackTrace();  
  32.         } catch (IllegalAccessException e) {  
  33.               
  34.             e.printStackTrace();  
  35.         }  
  36.         mGallery.setOnItemClickListener(new OnItemClickListener() {  
  37.             public void onItemClick(AdapterView parent, View v, int position, long id) {  
  38.                 testImageView.this.setTitle(String.valueOf(position));  
  39.             }  
  40.         });  
  41.     }  
  42.       
  43.      
  44.  
  45.   
  46.     private class ImageAdapter extends BaseAdapter{  
  47.         private Context mContext;  
  48.         private ArrayList<Integer> imgList=new ArrayList<Integer>();  
  49.         private ArrayList<Object> imgSizes=new ArrayList<Object>();  
  50.         public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{  
  51.             mContext = c;  
  52.               
  53.               
  54.             Field[] fields = R.drawable.class.getDeclaredFields();  
  55.             for (Field field : fields)  
  56.             {  
  57.                 if (!"icon".equals(field.getName()))  
  58.                 {     
  59.                     int index=field.getInt(R.drawable.class);  
  60.                       
  61.                     imgList.add(index);  
  62.                       
  63.                     int size[]=new int[2];  
  64.                     Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);  
  65.                     size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();  
  66.                     imgSizes.add(size);  
  67.                 }  
  68.             }  
  69.         }  
  70.         @Override  
  71.         public int getCount() {  
  72.               
  73.   
  74.             return imgList.size();  
  75.         }  
  76.   
  77.         @Override  
  78.         public Object getItem(int position) {  
  79.               
  80.   
  81.             return position;  
  82.         }  
  83.   
  84.         @Override  
  85.         public long getItemId(int position) {  
  86.               
  87.   
  88.             return position;  
  89.         }  
  90.   
  91.         @Override  
  92.         public View getView(int position, View convertView, ViewGroup parent) {  
  93.               
  94.   
  95.             ImageView i = new ImageView (mContext);  
  96.               
  97.             i.setImageResource(imgList.get(position).intValue());  
  98.             i.setScaleType(ImageView.ScaleType.FIT_XY);  
  99.               
  100.             int size[]= new int[2];  
  101.             size=(int[]) imgSizes.get(position);  
  102.             i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));  
  103.             return i;  
  104.         }  
  105.           
  106.     };  
  107. }