Android 控件之Gallery图片集

 

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

Android 控件之Gallery图片集

Android 控件之Gallery图片集

Android 控件之Gallery图片集

 

 widgetdemo.rar

 一、简介

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

二、实例

1.布局文件

01<?xml version="1.0" encoding="utf-8"?>
02<LinearLayout
03  xmlns:android="http://schemas.android.com/apk/res/android"
04  android:layout_width="fill_parent"
05   android:orientation="vertical"
06  android:layout_height="wrap_content">
07   
08  <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"
09 android:layout_width="match_parent"
10 android:layout_height="wrap_content"
11/>
12  
13<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
14    android:orientation="vertical"
15    android:layout_width="match_parent"
16    android:layout_height="wrap_content">
17  
18    <Gallery android:id="@+id/gallery1"
19        android:layout_width="match_parent"
20        android:layout_height="wrap_content"
21        android:gravity="center_vertical"
22        android:spacing="16dp"
23    />
24  
25</LinearLayout>
26  
27  
28</LinearLayout>

 

 2.属性文件

01<?xml version="1.0" encoding="utf-8"?>
02  
03<resources>
04  
05    <declare-styleable name="TogglePrefAttrs">
06        <attr name="android:preferenceLayoutChild" />
07    </declare-styleable>
08     
09    <!-- These are the attributes that we want to retrieve from the theme
10         in view/Gallery1.java -->
11    <declare-styleable name="Gallery1">
12        <attr name="android:galleryItemBackground" />
13    </declare-styleable>
14     
15     <declare-styleable name="LabelView">
16        <attr name="text" format="string" />
17        <attr name="textColor" format="color" />
18        <attr name="textSize" format="dimension" />
19    </declare-styleable>
20</resources>

 

3.代码

001/**
002 *
003 */
004package wjq.WidgetDemo;
005  
006import android.R.layout;
007import android.app.Activity;
008import android.content.Context;
009import android.content.res.TypedArray;
010import android.database.Cursor;
011import android.os.Bundle;
012import android.provider.Contacts.People;
013import android.view.ContextMenu;
014import android.view.MenuItem;
015import android.view.View;
016import android.view.ViewGroup;
017import android.view.ContextMenu.ContextMenuInfo;
018import android.widget.BaseAdapter;
019import android.widget.Gallery;
020import android.widget.ImageView;
021import android.widget.SimpleCursorAdapter;
022import android.widget.SpinnerAdapter;
023import android.widget.Toast;
024import android.widget.AdapterView.AdapterContextMenuInfo;
025  
026/**
027 * @author 记忆的永恒
028 *
029 */
030public class GalleryDemo extends Activity {
031 private Gallery gallery;
032 private Gallery gallery1;
033  
034 /*
035  * (non-Javadoc)
036  *
037  * @see android.app.Activity#onCreate(android.os.Bundle)
038  */
039 @Override
040 protected void onCreate(Bundle savedInstanceState) {
041  // TODO Auto-generated method stub
042  super.onCreate(savedInstanceState);
043  setContentView(R.layout.gallerypage);
044  gallery = (Gallery) findViewById(R.id.gallery);
045  gallery.setAdapter(new ImageAdapter(this));
046    
047  registerForContextMenu(gallery);
048    
049   Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
050         startManagingCursor(c);
051          
052         SpinnerAdapter adapter = new SimpleCursorAdapter(this,
053         // Use a template that displays a text view
054                 android.R.layout.simple_gallery_item,
055                 // Give the cursor to the list adatper
056                 c,
057                 // Map the NAME column in the people database to...
058                 new String[] {People.NAME},
059                 // The "text1" view defined in the XML template
060                 new int[] { android.R.id.text1 });
061  
062         gallery1= (Gallery) findViewById(R.id.gallery1);
063         gallery1.setAdapter(adapter);
064 }
065  
066 @Override
067 public void onCreateContextMenu(ContextMenu menu, View v,
068   ContextMenuInfo menuInfo) {
069  menu.add("Action");
070 }
071  
072 @Override
073 public boolean onContextItemSelected(MenuItem item) {
074  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
075    .getMenuInfo();
076  Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT)
077    .show();
078  return true;
079 }
080   
081 public class ImageAdapter extends BaseAdapter {
082  int mGalleryItemBackground;
083  private Context mContext;
084  
085  private Integer[] mImageIds = { R.drawable.b, R.drawable.c,
086    R.drawable.d, R.drawable.f, R.drawable.g };
087  
088  public ImageAdapter(Context context) {
089   mContext = context;
090  
091   TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
092   mGalleryItemBackground = a.getResourceId(
093     R.styleable.Gallery1_android_galleryItemBackground, 0);
094   a.recycle();
095  }
096  
097  @Override
098  public int getCount() {
099   return mImageIds.length;
100  }
101  
102  @Override
103  public Object getItem(int position) {
104   return position;
105  }
106  
107  @Override
108  public long getItemId(int position) {
109   return position;
110  }
111  
112  @Override
113  public View getView(int position, View convertView, ViewGroup parent) {
114   ImageView i = new ImageView(mContext);
115  
116   i.setImageResource(mImageIds[position]);
117   i.setScaleType(ImageView.ScaleType.FIT_XY);
118   i.setLayoutParams(new Gallery.LayoutParams(300, 400));
119  
120   // The preferred Gallery item background
121   i.setBackgroundResource(mGalleryItemBackground);
122  
123   return i;
124  }
125  
126 }
127  
128  
129}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值