Android Gallery学习笔记(An-Beer工作室)
- 格式:doc
- 大小:343.00 KB
- 文档页数:7
Android Gallery学习笔记Gallery是Android中的图片库控件。
先看效果,爽一番源码下载一、简介在中心锁定,水平显示列表的项。
二、实例1.布局文件<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schem /apk/res/android"android:layout_width="fill_parent"android:orientation="vertical"android:layout_height="wrap_content"><Gallery xmlns:android="http://schem /apk/res/android" androi d:id="@+id/gallery"android:layout_width="match_parent"android:layout_height="wrap_content"/><LinearLayout xmlns:android="http://schem /apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><Gallery android:id="@+id/gallery1"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:spacing="16dp"/></LinearLayout></LinearLayout>2.属性文件<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="TogglePrefAttrs"><attr name="android:preferenceLayoutChild" /></declare-styleable><!-- These are the attributes that we want to retrieve from the t hem e in view/Gallery1.java --><declare-styleable name="Gallery1"><attr name="android:galleryItemBackground" /></declare-styleable><declare-styleable name="LabelView"><attr name="text" format="string" /><attr name="textColor" format="color" /><attr name="textSize" format="dimension" /></declare-styleable></resources>3.代码/****/package wjq.Widget Demo;import yout;import android.app.Activity;import android.content.Context;import android.content.res.TypedArray;import android.database.Cursor;import android.os.Bundle;import android.provider.Contacts.People;import android.view.ContextMenu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.ContextMenu.ContextMenuInfo;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.SimpleCursorAdapter;import android.widget.SpinnerAdapter;import android.widget.Toast;import android.widget.AdapterView.AdapterContextMenuInfo;/*** @author 记忆的永恒**/public class GalleryDemo extends Activity {private Gallery gallery;private Gallery gallery1;/** (non-Javadoc)** @see android.app.Activity#onCreate(android.os.Bundle)*/@Overrideprotected void onCreate(Bundle savedInstanc eState) {// TODO Auto-generated m ethod stubsuper.onCreate(savedInstanceState);setContentView(yout.gallerypage);gallery =(Gallery) findViewById(R.id.gallery);gallery.setAdapter(new ImageAdapter(this));registerForContextMenu(gallery);Cursor c =getContent Resolver().query(People.CONTENT_URI, null, null, null, null);startManagingCursor(c);SpinnerAdapter adapter =new SimpleCursorAdapter(this,// Use a tem plate that displays a text viewyout.simple_gallery_item,// Give the cursor to the list adatperc,// Map the NAME column in the people database to...new String[] {},// The "text1" view defined in the XML templatenew int[] { android.R.id.text1 });gallery1=(Gallery) findViewById(R.id.gallery1);gallery1.setAdapter(adapter);}@Overridepublic void onCreateContextMenu(ContextMenu m enu, View v,ContextMenuInfo menuInfo) {m enu.add("Action");}@Overridepublic boolean onContextItemSelected(MenuItem item) {AdapterContextMenuInfo info =(AdapterContextMenuInfo) item.getMenuInfo();Toast.m akeText(this, "Longpress: " +info.position, Toast.LENGT H_SHORT) .show();return true;}public class Im ageAdapter extends BaseAdapter {int mGalleryItemBackground;private Context m Context;private Integer[] mImageIds ={ R.drawable.b, R.drawable.c,R.drawable.d, R.drawable.f, R.drawable.g };public Im ageAdapter(Context context) {m Context =context;TypedArray a =obtainStyledAttributes(R.styleable.Gallery1);mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);a.recycle();}@Overridepublic int getCount() {return mImageIds.length;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItem Id(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { Im ageView i =new ImageView(mContext);i.setIm ageResource(mImageIds[position]);i.setScaleType(Im ageView.ScaleType.FIT_XY);i.setLayoutParam s(new youtParam s(300, 400));// The preferred Gallery item backgroundi.setBackgroundResource(mGalleryItemBackground);return i;}}}。