Android getting started tutorial (23) -- Gallery)

Source: Internet
Author: User

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 ?
  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Linearlayout Xmlns: Android=Http://schemas.android.com/apk/res/android"
  3. Android: Orientation="Vertical"
  4. Android: layout_width="Fill_parent"
  5. Android: layout_height="Fill_parent"
  6. >
  7. <Gallery Android: ID="@ + ID/Gallery" Android: layout_height="Fill_parent" Android: layout_width="Fill_parent"></Gallery>
  8. </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 ?
  1. PackageCom. testimageview;
  2. ImportJava. Lang. Reflect. field;
  3. ImportJava. util. arraylist;
  4. ImportAndroid. App. activity;
  5. ImportAndroid. content. context;
  6. ImportAndroid. Graphics. Bitmap;
  7. ImportAndroid. Graphics. bitmapfactory;
  8. ImportAndroid. OS. Bundle;
  9. ImportAndroid. View. view;
  10. ImportAndroid. View. viewgroup;
  11. ImportAndroid. widget. adapterview;
  12. ImportAndroid. widget. baseadapter;
  13. ImportAndroid. widget. Gallery;
  14. ImportAndroid. widget. imageview;
  15. ImportAndroid. widget. adapterview. onitemclicklistener;
  16. Public ClassTestimageviewExtendsActivity {
  17. PrivateGallery mgallery;
  18. @ Override
  19. Public VoidOncreate (bundle savedinstancestate ){
  20. Super. Oncreate (savedinstancestate );
  21. Setcontentview (R. layout. Main );
  22. Mgallery = (Gallery) findviewbyid (R. Id. Gallery );
  23. Try{
  24. Mgallery. setadapter (NewImageadapter (This));
  25. }Catch(Illegalargumentexception e ){
  26. // Todo auto-generated Catch Block
  27. E. printstacktrace ();
  28. }Catch(Illegalaccessexception e ){
  29. // Todo auto-generated Catch Block
  30. E. printstacktrace ();
  31. }
  32. Mgallery. setonitemclicklistener (NewOnitemclicklistener (){
  33. Public VoidOnitemclick (adapterview parent, view V,IntPosition,LongID ){
  34. Testimageview.This. Settitle (string. valueof (position ));
  35. }
  36. });
  37. }
  38. /* 
  39. * Class imageadapter is used to control gallery source and operation. 
  40. */
  41. Private ClassImageadapterExtendsBaseadapter {
  42. PrivateContext mcontext;
  43. PrivateArraylist <integer> imglist =NewArraylist <integer> ();
  44. PrivateArraylist <Object> imgsizes =NewArraylist <Object> ();
  45. PublicImageadapter (context c)ThrowsIllegalargumentexception, illegalaccessexception {
  46. Mcontext = C;
  47. // Use the reflection mechanism to obtain the image ID and size in the Resource
  48. Field [] fields = R. drawable.Class. Getdeclaredfields ();
  49. For(Field: fields)
  50. {
  51. If(!"Icon". Equals (field. getname ()))// Images except icons
  52. {
  53. IntIndex = field. getint (R. drawable.Class);
  54. // Save the image ID
  55. Imglist. Add (INDEX );
  56. // Save the image size
  57. IntSize [] =New Int[2];
  58. Bitmap bmimg = bitmapfactory. decoderesource (getresources (), index );
  59. Size [0] = Bmimg. getwidth (); Size [1] = Bmimg. getheight ();
  60. Imgsizes. Add (size );
  61. }
  62. }
  63. }
  64. @ Override
  65. Public IntGetcount (){
  66. // Todo auto-generated method stub
  67. ReturnImglist. Size ();
  68. }
  69. @ Override
  70. PublicObject getitem (IntPosition ){
  71. // Todo auto-generated method stub
  72. ReturnPosition;
  73. }
  74. @ Override
  75. Public LongGetitemid (IntPosition ){
  76. // Todo auto-generated method stub
  77. ReturnPosition;
  78. }
  79. @ Override
  80. PublicView getview (IntPosition, view convertview, viewgroup parent ){
  81. // Todo auto-generated method stub
  82. Imageview I =NewImageview (mcontext );
  83. // Obtain the image ID from imglist
  84. I. setimageresource (imglist. Get (position). intvalue ());
  85. I. setscaletype (imageview. scaletype. fit_xy );
  86. // Obtain the image size from imgsizes
  87. IntSize [] =New Int[2];
  88. Size = (Int[]) Imgsizes. Get (position );
  89. I. setlayoutparams (NewGallery. layoutparams (size [0], Size [1]);
  90. ReturnI;
  91. }
  92. };
  93. }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.