Android 控件之Gallery图片集

作者: admin 分类: 学习文档 发布时间: 2011-11-03 22:25

Gallery是Android中的图片库控件。先看效果

1.png

 

2.png

 

3.png

一、简介

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

二、实例

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

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Protected by WP Anti Spam