This article comes fromHttp://blog.csdn.net/hellogv/, The reference must indicate the source!
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. This article introduces the usage of gallery and uses the reflection mechanism to dynamically read images from resources.
In this article:
Main. xml source code:
View plain
Copy to clipboard
Print
?
- <?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/Gallery" Android: layout_height="Fill_parent" Android: layout_width="Fill_parent"></Gallery>
-
- </Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <gallery Android: Id = "@ + ID/Gallery" Android: layout_height = "fill_parent" Android: layout_width = "fill_parent"> </gallery> <br/> </linearlayout> <br/>
ProgramSource code:
View plain
Copy to clipboard
Print
?
-
- PackageCom. testimageview;
-
-
- ImportJava. Lang. Reflect. field;
-
- ImportJava. util. arraylist;
-
- ImportAndroid. App. activity;
-
- ImportAndroid. content. context;
-
- ImportAndroid. Graphics. Bitmap;
-
- ImportAndroid. Graphics. bitmapfactory;
-
- ImportAndroid. OS. Bundle;
-
- ImportAndroid. View. view;
-
- ImportAndroid. View. viewgroup;
- ImportAndroid. widget. adapterview;
-
- ImportAndroid. widget. baseadapter;
-
- ImportAndroid. widget. Gallery;
-
- ImportAndroid. widget. imageview;
-
- ImportAndroid. widget. adapterview. onitemclicklistener;
-
-
- Public ClassTestimageviewExtendsActivity {
- PrivateGallery mgallery;
-
- @ Override
-
- Public VoidOncreate (bundle savedinstancestate ){
-
- Super. Oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
-
- Mgallery = (Gallery) findviewbyid (R. Id. Gallery );
-
- Try{
-
- Mgallery. setadapter (NewImageadapter (This));
-
- }Catch(Illegalargumentexception e ){
-
- // Todo auto-generated Catch Block
- E. printstacktrace ();
-
- }Catch(Illegalaccessexception e ){
-
- // Todo auto-generated Catch Block
-
- E. printstacktrace ();
-
- }
- Mgallery. setonitemclicklistener (NewOnitemclicklistener (){
-
- Public VoidOnitemclick (adapterview parent, view V,IntPosition,LongID ){
-
- Testimageview.This. Settitle (string. valueof (position ));
-
- }
- });
-
- }
-
-
- /*
-
- * Class imageadapter is used to control gallery source and operation.
-
- */
-
- Private ClassImageadapterExtendsBaseadapter {
- PrivateContext mcontext;
-
- PrivateArraylist <integer> imglist =NewArraylist <integer> ();
-
- PrivateArraylist <Object> imgsizes =NewArraylist <Object> ();
- PublicImageadapter (context c)ThrowsIllegalargumentexception, illegalaccessexception {
-
- Mcontext = C;
-
-
- // Use the reflection mechanism to obtain the image ID and size in the Resource
-
- Field [] fields = R. drawable.Class. Getdeclaredfields ();
- For(Field: fields)
-
- {
-
- If(!"Icon". Equals (field. getname ()))// Images except icons
-
- {
- IntIndex = field. getint (R. drawable.Class);
-
- // Save the image ID
-
- Imglist. Add (INDEX );
-
- // Save the image size
- IntSize [] =New Int[2];
-
- Bitmap bmimg = bitmapfactory. decoderesource (getresources (), index );
-
- Size [0] = Bmimg. getwidth (); Size [1] = Bmimg. getheight ();
- Imgsizes. Add (size );
-
- }
-
- }
-
- }
-
- @ Override
-
- Public IntGetcount (){
- // Todo auto-generated method stub
-
-
- ReturnImglist. Size ();
-
- }
-
-
- @ Override
- PublicObject getitem (IntPosition ){
-
- // Todo auto-generated method stub
-
-
- ReturnPosition;
-
- }
-
- @ Override
-
- Public LongGetitemid (IntPosition ){
-
- // Todo auto-generated method stub
-
-
- ReturnPosition;
- }
-
-
- @ Override
-
- PublicView getview (IntPosition, view convertview, viewgroup parent ){
-
- // Todo auto-generated method stub
-
- Imageview I =NewImageview (mcontext );
-
- // Obtain the image ID from imglist
-
- I. setimageresource (imglist. Get (position). intvalue ());
-
- I. setscaletype (imageview. scaletype. fit_xy );
-
- // Obtain the image size from imgsizes
- IntSize [] =New Int[2];
-
- Size = (Int[]) Imgsizes. Get (position );
-
- I. setlayoutparams (NewGallery. layoutparams (size [0], Size [1]);
- ReturnI;
-
- }
-
-
- };
-
- }