Android Road Gallery Gallery

Source: Internet
Author: User

Gallery is an internal element that scrolls horizontally, and is able to position the currently selected child element in its center layout component.

Let's take a look at the execution effect of the sample directly.


1. Xinjiang project hellogallery

2. Put the pictures that need to be displayed in res/drawable folder. Woman01.jpg. Woman02.jpg,woman03.jpg

3.res/layout/activity_main.xml file such as the following:

<framelayout  xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:id= "@+id/framelayout01"   ><imageview android:layout_width= "Match_parent"    android: layout_height= "Match_parent"    android:id= "@+id/imageview01"    android:src= "@drawable/woman01"/>< Gallery android:layout_width= "match_parent"    android:layout_height= "wrap_content" android:id= "@+id/    Gallery01 "    android:spacing=" 5DP "/></framelayout >

We use framelayout to achieve overlay effects and use ImageView to display large images. Gallery to show the gallery, the android:spacing= "5DP" property is used to set the interval between elements.


4. Create a new attrs.xml content in the res/values/folder such as the following:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable   name= "Hellogallery" >         <attr name= "Android:galleryitembackground" >        </attr>   </declare-styleable></ Resources>
5. The code for the main activity,mainactivity is as follows:

<pre name= "code" class= "java" >package com.howlaa.hellogallery;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.view.ViewGroup;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.gallery;import Android.widget.imageview;import Android.widget.toast;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); final ImageView IV = (ImageView) Findviewbyid (R.ID.IMAGEVIEW01);        Gallery g = (Gallery) Findviewbyid (R.ID.GALLERY01);//Set Picture match G.setadapter (new Imageadapter (this)); Set Adapterview click Listener, Gallery is the subclass of Adapterview G.setonitemclicklistener (new Onitemclicklistener () {@Overri De public void Onitemclick (adapterview<?> parent, view view, int pOsition, long ID) {//Display Click is the number of pictures Toast.maketext (Mainactivity.this, "" + position,                    Toast.length_long). Show ();            Setting the ImageView of the background section displays the picture of the current item Iv.setimageresource ((ImageView) view). GetId ()); }    });} }


The code for 6.ImageAdapter is as follows:


Package Com.howlaa.hellogallery;import Android.content.context;import Android.content.res.typedarray;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import android.widget.Gallery; Import Android.widget.imageview;import Android.widget.spinneradapter;public class Imageadapter extends BaseAdapter {/    /item-Modified background int mgalleryitembackground;    Context object private context Mcontext;  Image array Private integer[] Mimageids ={r.drawable.woman01,r.drawable.woman02,r.drawable.woman03};    Construction method Public Imageadapter (Context c) {mcontext = C;    Read Styleable resource TypedArray a = Mcontext.obtainstyledattributes (r.styleable.hellogallery);    Mgalleryitembackground = A.getresourceid (r.styleable.hellogallery_android_galleryitembackground, 0);    A.recycle (); } @Overridepublic int GetCount () {return mimageids.length;} @Overridepublic Object getItem (int position) {return position;} @Overridepublic long Getitemid (int position) {REturn position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {ImageView IV = new ImageView (mcontext); Iv.  Setimageresource (Mimageids[position]);        Set the ID for the generated ImageView, not set the ID is -1 iv.setid (mimageids[position]);        Iv.setlayoutparams (New Gallery.layoutparams (120, 160));        Iv.setscaletype (ImageView.ScaleType.FIT_XY); Iv.setbackgroundresource (Mgalleryitembackground); return IV;}}


Android Road Gallery Gallery

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.