Android Development for gallery effect

Source: Internet
Author: User

Gallery Slide Show multiple pictures

Layout

1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Vertical">6 7 8 9<GalleryTenAndroid:id="@+id/gallery"   Oneandroid:spacing="5px"   AAndroid:unselectedalpha="0.5"   -Android:layout_width="match_parent"   -android:layout_height="wrap_content"/> the  -     -</LinearLayout>


Activity

1 Package tea_xqx;2 3 import COM.EXAMPLE.TEA.R;4 5 import android.app.Activity;6 import Android.content.res.TypedArray;7 import Android.os.Bundle;8 import Android.view.View;9 import Android.view.ViewGroup;Ten import Android.widget.AdapterView; One import Android.widget.AdapterView.OnItemClickListener; A import Android.widget.BaseAdapter; - import android.widget.Gallery; - import Android.widget.ImageView; the import Android.widget.TextView; - import Android.widget.Toast; -  -  Public classMenuteatools extends activity{ +     //set up gallery pictures -     Private int[] ImageId =New int[] {R.drawable.menu_tea_lvcha,r.drawable.menu_tea_hongcha,r.drawable.menu_tea_baicha,r.drawable.menu_tea_  Heicha,r.drawable.menu_tea_huangcha,r.drawable.menu_tea_wulongcha};  + @Override A     protected voidonCreate (Bundle savedinstancestate) { at         //TODO auto-generated Method Stub - super.oncreate (savedinstancestate); - Setcontentview (r.layout.menu_tools); -          -Gallery Gallery =(Gallery) Findviewbyid (r.id.gallery); -Baseadapter adapter =NewBaseadapter () { in @Override -                  PublicView GetView (intposition, View Convertview, ViewGroup parent) {   to ImageView ImageView;  +                     if(Convertview = =NULL) {   -ImageView =NewImageView (Menuteatools. This);  the Imageview.setscaletype (ImageView.ScaleType.FIT_XY);  *Imageview.setlayoutparams (NewGallery.layoutparams ( -, -));  $TypedArray TypedArray =obtainstyledattributes (r.styleable.gallery); Panax Notoginseng Imageview.setbackgroundresource (Typedarray.getresourceid ( - R.styleable.gallery_android_galleryitembackground, the                                 0));  +Imageview.setpadding (5,0,5,0);  A                     }  the                     Else  +                     {   -ImageView =(ImageView) Convertview;  $                     }   $ Imageview.setimageresource (imageid[position]);  -                     returnImageView;  -                 }   the        - @OverrideWuyi                  Public LongGetitemid (intposition) {   the                     returnposition;  -                 }   Wu        - @Override About                  PublicObject GetItem (intposition) {   $                     returnposition;  -                 }   -        - @Override A                  Public intGetCount () { +                     returnimageid.length;  the                 }   -             };  $ Gallery.setadapter (adapter);  theGallery.setselection (Imageid.length/2); The ID of the picture that is displayed by default the             //Click events for gallery Pictures theGallery.setonitemclicklistener (NewOnitemclicklistener () { the @Override -                  Public voidOnitemclick (adapterview<?>Parent, view view, in                         intPositionLongID) { the                  theToast.maketext (Menuteatools. This,   About                             "Section"+ string.valueof (position+1) +"Picture is selected",   the toast.length_short). Show ();  the                 }   the             });  +         }    -  the     Bayi  the}


Finally add a piece of code to the Res/values/string.xml

1 <declare-styleable name="Gallery">  2         <attr name= " Android:galleryitembackground " />  3     </declare-styleable>

This completes the effect of a gallery

Android Development for gallery effect

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.