您的位置:首页 > 移动开发 > Android开发

Android 控件之Gallery图片集

2012-10-18 14:24 218 查看

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








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
<!--Thesearetheattributesthatwewanttoretrievefromthetheme
10
inview/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
*/
004
package

wjq.WidgetDemo;
005
006
import

android.R.layout;
007
import

android.app.Activity;
008
import

android.content.Context;
009
import

android.content.res.TypedArray;
010
import

android.database.Cursor;
011
import

android.os.Bundle;
012
import

android.provider.Contacts.People;
013
import

android.view.ContextMenu;
014
import

android.view.MenuItem;
015
import

android.view.View;
016
import

android.view.ViewGroup;
017
import

android.view.ContextMenu.ContextMenuInfo;
018
import

android.widget.BaseAdapter;
019
import

android.widget.Gallery;
020
import

android.widget.ImageView;
021
import

android.widget.SimpleCursorAdapter;
022
import

android.widget.SpinnerAdapter;
023
import

android.widget.Toast;
024
import

android.widget.AdapterView.AdapterContextMenuInfo;
025
026
/**
027
*@author记忆的永恒
028
*
029
*/
030
public
class
GalleryDemo
extends

Activity{
031
private

Gallerygallery;
032
private

Gallerygallery1;
033
034
/*

035
*(non-Javadoc)
036
*

037
*@seeandroid.app.Activity#onCreate(android.os.Bundle)
038
*/
039
@Override
040
protected

void
onCreate(BundlesavedInstanceState){
041
//TODOAuto-generatedmethodstub
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
Cursorc=getContentResolver().query(People.CONTENT_URI,
null
,
null
,
null
,
null
);
050
startManagingCursor(c);
051
052
SpinnerAdapteradapter=
new
SimpleCursorAdapter(
this
,
053
//Useatemplatethatdisplaysatextview
054
android.R.layout.simple_gallery_item,
055
//Givethecursortothelistadatper
056
c,
057
//MaptheNAMEcolumninthepeopledatabaseto...
058
new

String[]{People.NAME},
059
//The"text1"viewdefinedintheXMLtemplate
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(ContextMenumenu,Viewv,
068
ContextMenuInfomenuInfo){
069
menu.add(
"Action"
);
070
}

071
072
@Override
073
public

boolean
onContextItemSelected(MenuItemitem){
074
AdapterContextMenuInfoinfo=(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

ContextmContext;
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(Contextcontext){
089
mContext=context;
090
091
TypedArraya=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

ObjectgetItem(
int

position){
104
return

position;
105
}

106
107
@Override
108
public

long
getItemId(
int

position){
109
return

position;
110
}

111
112
@Override
113
public

ViewgetView(
int

position,ViewconvertView,ViewGroupparent){
114
ImageViewi=
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
//ThepreferredGalleryitembackground
121
i.setBackgroundResource(mGalleryItemBackground);
122
123
return

i;
124
}

125
126
}

127
128
129
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: