歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android入門教程之Gallery

Android入門教程之Gallery

日期:2017/3/1 10:35:15   编辑:Linux編程
Android的Gallery控件是個很不錯的看圖控件,大大減輕了開發者對於看圖功能的開發,而且效果也比較美觀。本文介紹Gallery的用法,用反射機制來動態讀取資源中的圖片。

本文的效果圖:

main.xml源碼:

[xhtml]

  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>

程序源碼:

[java]

  1. package com.testImageView;
  2. import java.lang.reflect.Field;
  3. import java.util.ArrayList;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AdapterView;
  12. import android.widget.BaseAdapter;
  13. import android.widget.Gallery;
  14. import android.widget.ImageView;
  15. import android.widget.AdapterView.OnItemClickListener;
  16. public class testImageView extends Activity {
  17. private Gallery mGallery;
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. mGallery = (Gallery)findViewById(R.id.gallery);
  23. try {
  24. mGallery.setAdapter(new ImageAdapter(this));
  25. } catch (IllegalArgumentException e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28. } catch (IllegalAccessException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. mGallery.setOnItemClickListener(new OnItemClickListener() {
  33. public void onItemClick(AdapterView parent, View v, int position, long id) {
  34. testImageView.this.setTitle(String.valueOf(position));
  35. }
  36. });
  37. }
  38. /*
  39. * class ImageAdapter is used to control gallery source and operation.
  40. */
  41. private class ImageAdapter extends BaseAdapter{
  42. private Context mContext;
  43. private ArrayList<Integer> imgList=new ArrayList<Integer>();
  44. private ArrayList<Object> imgSizes=new ArrayList<Object>();
  45. public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{
  46. mContext = c;
  47. //用反射機制來獲取資源中的圖片ID和尺寸
  48. Field[] fields = R.drawable.class.getDeclaredFields();
  49. for (Field field : fields)
  50. {
  51. if (!"icon".equals(field.getName()))//除了icon之外的圖片
  52. {
  53. int index=field.getInt(R.drawable.class);
  54. //保存圖片ID
  55. imgList.add(index);
  56. //保存圖片大小
  57. int size[]=new int[2];
  58. Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);
  59. size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();
  60. imgSizes.add(size);
  61. }
  62. }
  63. }
  64. @Override
  65. public int getCount() {
  66. // TODO Auto-generated method stub
  67. return imgList.size();
  68. }
  69. @Override
  70. public Object getItem(int position) {
  71. // TODO Auto-generated method stub
  72. return position;
  73. }
  74. @Override
  75. public long getItemId(int position) {
  76. // TODO Auto-generated method stub
  77. return position;
  78. }
  79. @Override
  80. public View getView(int position, View convertView, ViewGroup parent) {
  81. // TODO Auto-generated method stub
  82. ImageView i = new ImageView (mContext);
  83. //從imgList取得圖片ID
  84. i.setImageResource(imgList.get(position).intValue());
  85. i.setScaleType(ImageView.ScaleType.FIT_XY);
  86. //從imgSizes取得圖片大小
  87. int size[]= new int[2];
  88. size=(int[]) imgSizes.get(position);
  89. i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));
  90. return i;
  91. }
  92. };
  93. }
Copyright © Linux教程網 All Rights Reserved