Android 控件之Gallery图片集

jerry Android 2015年11月23日 收藏

Gallery是Android中的图片库控件。先看效果,爽一番

一、简介

在中心锁定,水平显示列表的项。

二、实例

1.布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:orientation="vertical"
  6. android:layout_height="wrap_content">
  7. <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. />
  11.  
  12. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  13. android:orientation="vertical"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content">
  16. <Gallery android:id="@+id/gallery1"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:gravity="center_vertical"
  20. android:spacing="16dp"
  21. />
  22. </LinearLayout>
  23.  
  24. </LinearLayout>

2.属性文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="TogglePrefAttrs">
  4. <attr name="android:preferenceLayoutChild" />
  5. </declare-styleable>
  6. <!-- These are the attributes that we want to retrieve from the theme
  7. in view/Gallery1.java -->
  8. <declare-styleable name="Gallery1">
  9. <attr name="android:galleryItemBackground" />
  10. </declare-styleable>
  11. <declare-styleable name="LabelView">
  12. <attr name="text" format="string" />
  13. <attr name="textColor" format="color" />
  14. <attr name="textSize" format="dimension" />
  15. </declare-styleable>
  16. </resources>

3.代码

  1. /**
  2. *
  3. */
  4. package wjq.WidgetDemo;
  5.  
  6. import android.R.layout;
  7. import android.app.Activity;
  8. import android.content.Context;
  9. import android.content.res.TypedArray;
  10. import android.database.Cursor;
  11. import android.os.Bundle;
  12. import android.provider.Contacts.People;
  13. import android.view.ContextMenu;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.view.ContextMenu.ContextMenuInfo;
  18. import android.widget.BaseAdapter;
  19. import android.widget.Gallery;
  20. import android.widget.ImageView;
  21. import android.widget.SimpleCursorAdapter;
  22. import android.widget.SpinnerAdapter;
  23. import android.widget.Toast;
  24. import android.widget.AdapterView.AdapterContextMenuInfo;
  25.  
  26. public class GalleryDemo extends Activity {
  27. private Gallery gallery;
  28. private Gallery gallery1;
  29. /*
  30. * (non-Javadoc)
  31. *
  32. * @see android.app.Activity#onCreate(android.os.Bundle)
  33. */
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. // TODO Auto-generated method stub
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.gallerypage);
  39. gallery = (Gallery) findViewById(R.id.gallery);
  40. gallery.setAdapter(new ImageAdapter(this));
  41. registerForContextMenu(gallery);
  42. Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
  43. startManagingCursor(c);
  44. SpinnerAdapter adapter = new SimpleCursorAdapter(this,
  45. // Use a template that displays a text view
  46. android.R.layout.simple_gallery_item,
  47. // Give the cursor to the list adatper
  48. c,
  49. // Map the NAME column in the people database to...
  50. new String[] {People.NAME},
  51. // The "text1" view defined in the XML template
  52. new int[] { android.R.id.text1 });
  53. gallery1= (Gallery) findViewById(R.id.gallery1);
  54. gallery1.setAdapter(adapter);
  55. }
  56.  
  57. @Override
  58. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  59. menu.add("Action");
  60. }
  61.  
  62. @Override
  63. public boolean onContextItemSelected(MenuItem item) {
  64. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  65. Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
  66. return true;
  67. }
  68. public class ImageAdapter extends BaseAdapter {
  69. int mGalleryItemBackground;
  70. private Context mContext;
  71. private Integer[] mImageIds = { R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.f, R.drawable.g };
  72. public ImageAdapter(Context context) {
  73. mContext = context;
  74. TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
  75. mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
  76. a.recycle();
  77. }
  78.  
  79. @Override
  80. public int getCount() {
  81. return mImageIds.length;
  82. }
  83.  
  84. @Override
  85. public Object getItem(int position) {
  86. return position;
  87. }
  88.  
  89. @Override
  90. public long getItemId(int position) {
  91. return position;
  92. }
  93.  
  94. @Override
  95. public View getView(int position, View convertView, ViewGroup parent) {
  96. ImageView i = new ImageView(mContext);
  97. i.setImageResource(mImageIds[position]);
  98. i.setScaleType(ImageView.ScaleType.FIT_XY);
  99. i.setLayoutParams(new Gallery.LayoutParams(300, 400));
  100. // The preferred Gallery item background
  101. i.setBackgroundResource(mGalleryItemBackground);
  102. return i;
  103. }
  104. }
  105.  
  106. }