The gallery control of Android is a Good Graph Display Control, which greatly reduces developers' development of the Image view function and provides a more elegant effect.
/Chapter04_ui_gallery01/src/COM/Amaker/test/mainactivity. Java
Code
Package Com. Amaker. test;
Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. Gallery;
Import Android. widget. imageview;
Public Class Mainactivity Extends Activity {
/** Called when the activity is first created. */
@ Override
// Called at creation
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Gallery g = (Gallery) findviewbyid (R. Id. gallery01 );
G. setadapter ( New Myadapter ( This ));
}
Class Myadapter Extends Baseadapter {
Context context;
Myadapter (context ){
This . Context = Context;
}
Private Integer [] IMGs = {
R. drawable. gallery_photo_1,
R. drawable. gallery_photo_2,
R. drawable. gallery_photo_3,
R. drawable. gallery_photo_4,
R. drawable. gallery_photo_5,
R. drawable. gallery_photo_6,
R. drawable. gallery_photo_7,
R. drawable. gallery_photo_8,
R. drawable. gallery_photo_1,
R. drawable. gallery_photo_2,
R. drawable. gallery_photo_3,
R. drawable. gallery_photo_4,
R. drawable. gallery_photo_5,
R. drawable. gallery_photo_6,
R. drawable. gallery_photo_7,
R. drawable. gallery_photo_8
};
Public Int Getcount (){
Return IMGs. length;
}
Public Object getitem ( Int ITEM ){
Return Item;
}
Public Long Getitemid ( Int ID ){
Return ID;
}
Public View getview ( Int Position, view convertview, viewgroup parent ){
Imageview = New Imageview (context );
Imageview. setimageresource (IMGs [position]);
Imageview. setscaletype (imageview. scaletype. fit_xy );
Imageview. setlayoutparams ( New Gallery. layoutparams ( 136 , 88 ));
Return Imageview;
}
}
}
/Chapter04_ui_gallery01/RES/layout/Main. xml
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: Orientation = "Vertical"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"
>
< Gallery
Android: ID = "@ + ID/gallery01"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content" >
</ Gallery >
</ Linearlayout >