Android-Advanced UI Control _gallery picture Toggle

Source: Internet
Author: User

Code

Activity01

 Packagecom.lxt008;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.Gallery;ImportAndroid.widget.Toast;ImportAndroid.widget.AdapterView.OnItemClickListener; Public classActivity01extendsactivity{/**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); //Get Gallery ObjectGallery g =(Gallery) Findviewbyid (R.ID.GALLERY01); //add Imageadapter to Gallery objectG.setadapter (NewImageadapter ( This)); //setting the background of a galleryG.setbackgroundresource (R.DRAWABLE.BG0); //Setting up gallery for event monitoringG.setonitemclicklistener (NewOnitemclicklistener () { Public voidOnitemclick (adapterview<?> parent, View V,intPositionLongID) {toast.maketext (Activity01. This, "You have selected" + (position+1) + "number Picture", Toast.length_short). Show ();    }        }); }}

Imageadapter

 Packagecom.lxt008;ImportAndroid.content.Context;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.Gallery;ImportAndroid.widget.ImageView; Public classImageadapterextendsbaseadapter{//Define Context    PrivateContext Mcontext; //defining an integer array that is a picture source    Privateinteger[] Mimageids ={r.drawable.img1, R.drawable.img2, R.DRAWABLE.IMG3,            R.DRAWABLE.IMG4, R.drawable.img5, R.drawable.img6, R.drawable.img7,    R.drawable.img8,}; //Statement Imageadapter     PublicImageadapter (Context c) {Mcontext=C; }    //get the number of pictures     Public intGetCount () {returnmimageids.length; }    //get the position of the picture in the library     PublicObject GetItem (intposition) {        returnposition; }    //Get Image ID     Public LongGetitemid (intposition) {        returnposition; }     PublicView GetView (intposition, View Convertview, ViewGroup parent) {ImageView ImageView=NewImageView (Mcontext); //setting up resources for ImageViewImageview.setimageresource (mimageids[position]); //Set layout picture 120x120 displayImageview.setlayoutparams (NewGallery.layoutparams (120, 120)); //Set the display scale typeImageview.setscaletype (ImageView.ScaleType.FIT_CENTER); returnImageView; }}

Layout file

<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android"   android:id = "@+id/gallery01"   android:layout_width= "Fill_parent"  android:layout_height= " Wrap_content "/>

Android-Advanced UI Control _gallery picture Toggle

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.