Simple Application of Android image browser-gallery

Source: Internet
Author: User

This week, I learned gallery again by taking advantage of the rest time, and then I wrote a picture browsing stuff.

First, let's take a look at the interface:

Click the image below to display it.

The Code is as follows:

View code

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <ImageView            android:id="@+id/imageView"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@drawable/star_red"            android:scaleType="matrix"             android:layout_weight="4"            />        <Gallery            android:id="@+id/gallery"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:layout_weight="1"            android:spacing="5dp" >        </Gallery></LinearLayout>
View code

1 package COM. example. griview; 2 3 Import javax. XML. parsers. saxparser; 4 Import javax. XML. parsers. saxparserfactory; 5 6 Import Org. XML. sax. helpers. defaulthandler; 7 Import Org. xmlpull. v1.xmlserializer; 8 9 Import android. OS. bundle; 10 Import android. app. activity; 11 import android. app. activitymanager; 12 Import android. content. context; 13 Import android. view. menu; 14 Import android. view. view; 15 im Port android. view. viewgroup; 16 Import android. view. viewgroup. layoutparams; 17 import android. widget. adapterview; 18 Import android. widget. adapterview. onitemclicklistener; 19 Import android. widget. baseadapter; 20 Import android. widget. gallery; 21 import android. widget. gridview; 22 Import android. widget. imageview; 23 Import android. widget. toast; 24 25 public class mainactivity extends activity {26 27 private gallery Gallery; 28 private imageview; 29 protected void oncreate (bundle savedinstancestate) {30 super. oncreate (savedinstancestate); 31 setcontentview (R. layout. relative); 32 gallery = (Gallery) findviewbyid (R. id. gallery); 33 imageview = (imageview) findviewbyid (R. id. imageview); 34 Gallery. setadapter (New imageadapter (this); 35 36 Gallery. setonitemclicklistener (New onitemclicklis Tener () {37 38 public void onitemclick (adapterview <?> Parent, view, int position, 39 Long ID) {40 imageview. setbackgroundresource (mthumbids [position]); 41 // The Position of the clicked gridview element is 42 toast. maketext (mainactivity. this, mthumbids [position], toast. length_long ). show (); 43} 44 45 46}); 47 48} 49 50 public class imageadapter extends baseadapter {51 52 private context mcontext; 53 // declare imageadapter 54 public imageadapter (context) 55 {56 this. mcontext = context; 57} 58 // obtain the number of images 59 Public int getcount () {60 return mthumbids. length; 61} 62 63 // obtain the position of the image in the database 64 public object getitem (INT position) {65 return mthumbids [position]; 66} 67 68 // obtain the position of the image in the library 69 public long getitemid (INT position) {70 return 0; 71} 72 73 public view getview (INT position, view convertview, viewgroup parent) {74 75 imageview; 76 if (convertview = NULL) 77 {78 imageview = new imageview (mcontext); 79 imageview. setlayoutparams (new gallery. layoutparams (75, 75); 80 imageview. setscaletype (imageview. scaletype. fit_center); 81 imageview. setpadding (8, 8, 8, 8); 82} 83 else {84 imageview = (imageview) convertview; 85} 86 87 imageview. setimageresource (mthumbids [position]); 88 return imageview; 89} 90 91} 92 93 // display picture 94 private integer [] mthumbids = {95 R. drawable. heart, 96 R. drawable. heart_broken, 97 R. drawable. heart_delete, 98 R. drawable. heart_preferences, 99 R. drawable. heart_editing, 100 r. drawable. heart_new, 101 R. drawable. star_red, 102 R. drawable. meishi3, 103 R. drawable. meishi4, 104 R. drawable. meishi5, 105}; 106 public Boolean oncreateoptionsmenu (menu) {107 // inflate the menu; this adds items to the action bar if it is present.108 getmenuinflater (). inflate (R. menu. main, menu); 109 return true; 110} 111 112}

I learned this week and look forward to the next learning progress...

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.