Android UI Control Series: Gallery (Gallery view)

Source: Internet
Author: User
Tags set background
Gallery is able to display its content horizontally, typically used to browse the image, the selected option is in the middle, and the information can be displayed in the corresponding event. The following combination of Imageswitcher components to implement a thumbnail to browse the image of the program, the following steps

The first step:

Create a Andorid project "Gallerytest", the project's entry is activity class gallerytest inherit activity and implement Onitemselectedlistener and Viewfactory interface, For creating pictures and views

Package Org.hualang.gallery;import Android.app.activity;import Android.os.bundle;import android.view.View;import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import android.widget.viewswitcher.viewfactory;//inherits activity, implements Onitemselectedlistener and Viewfactory interface public class Gallerytest extends activity implements onitemselectedlistener,viewfactory{/** called when the activity is first creat        Ed. */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.main);  } @Override Public View Makeview () {//TODO auto-generated method stub return        Null                        } @Override public void onitemselected (adapterview<?> arg0, View arg1, int arg2, Long Arg3) {//TODO auto-generated method stub} @Override public void Onnothingsel ected (adapterview<?> arg0){//TODO auto-generated method stub}} 

Step Two:

Add 7 images and thumbnails in the project's res\drawable\ directory

Step three:

in Project res\layout\ directory, create a layout file Main.xml, in which you add a gallery component and a Imageswitcher component, and set the corresponding property

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout  xmlns:android= "http://schemas.android.com/apk/ Res/android "      android:orientation=" vertical "      android:layout_width=" fill_parent "      android:layout_ height= "Fill_parent"      >  <imageswitcher android:id= "@+id/switcher" android:layout_width= "Fill_          Parent "          android:layout_height=" fill_parent "          android:layout_alignparenttop=" true "          android:layout_ Alignparentleft= "true"      />        <gallery android:id= "@+id/gallery"          android:background= "#55000000 "          android:layout_width=" fill_parent "          android:layout_height=" 60DP "          android:layout_ Alignparentbottom= "true"          android:layout_alignparentleft= "true"          android:gravity= "center_vertical"          android:spacing= "16DP"      />  </LinearLayout>

Fourth step: Declare the Imageswitcher instance used in the gallerytest top of the image resource integer array

public class Gallerytest extends activity implements onitemselectedlistener,viewfactory{/** called when the activity I s first created.        *//Declaration imageswitcher private Imageswitcher switcher; Thumbnail image ID array private integer[] thumbids={r.drawable.thumb0, r.drawable . THUMB1, R.DRAWABLE.THUMB2, R.DRAWABLE.THUMB3, R.draw ABLE.THUMB4, R.drawable.thumb5, R.drawable.thumb6, R.        DRAWABLE.THUMB7};                         Image ID Array private integer[] imgids={r.drawable.img0, R.DRAWABLE.IMG1,                        R.drawable.img2, R.DRAWABLE.IMG3, R.DRAWABLE.IMG4,        R.drawable.img5, R.drawable.img6, R.drawable.img7 };

Fifth Step:

In the OnCreate () method of gallerytest, set the window style to untitled, set the current layout view, get the Imageswitcher instance, and set the gradient draw to get the gallery instance

public void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Set window feature untitled        requestwindowfeature (window.feature_no_title);        Setcontentview (r.layout.main);        Imageswitcher Object        switcher= (imageswitcher) Findviewbyid (R.id.switcher) was obtained by Findviewbyid method;        Set up Factory        switcher.setfactory (this) for Imageswitcher;        Sets the animation fade-in effect        switcher.setinanimation (Animationutils.loadanimation (this, Android. r.anim.fade_in));        Sets the animation fade-out effect        switcher.setoutanimation (Animationutils.loadanimation (this, Android. r.anim.fade_out));        Get Gallery Object        Gallery g= (Gallery) Findviewbyid (r.id.gallery) by Findviewbyid method;    }

Sixth step:

Create an inner class Imageadapter, which inherits Baseadapter, sets the adapter instance for gallery

public class Imageadapter extends Baseadapter {//constructor method public Imageadapter (Context c) {                Mcontext = C;                }//Get quantity public int GetCount () {return thumbids.length;                 }//Gets the current option public Object getItem (int position) {return position; }//Get current Option ID public long getitemid (int position) {RE                Turn position;                        }//Get View Object public view GetView (int position, view Convertview, ViewGroup parent) {                        Instantiate the ImageView object ImageView i = new ImageView (mcontext);                        Set thumbnail image resource I.setimageresource (thumbids[position]);                        Sets the boundary alignment i.setadjustviewbounds (true);          Setting Layout parameters              I.setlayoutparams (New Gallery.layoutparams (layoutparams.wrap_content                        , layoutparams.wrap_content));                        Set Background resource I.setbackgroundresource (r.drawable.picturefrom);                return i;        } private Context Mcontext; }

Seventh Step:

Implement the Onitemselected () method to change the picture

@Override public          void onitemselected (adapterview<?> adapter, View v, int position,                          long id) {                  Switcher.setimageresource (Imgids[position]);          }

Eighth Step:

Implement the Makeview () method to set the layout format for ImageView

@Override public          View Makeview () {                  //TODO auto-generated Method Stub                  //Create ImageView                  ImageView i=new ImageView (this);                  Set Background color                  i.setbackgroundcolor (0xff000000);                  Set Precision type                  i.setscaletype (ImageView.ScaleType.FIT_CENTER);                  Set Layout parameters                  I.setlayoutparams (new Imageswitcher.layoutparams (                                  Layoutparams.fill_parent,layoutparams.fill _parent));                  return i;          }

Nineth Step:

Add adapter to Gallery and add Onitemselectedlistener listener

G.setadapter (New Imageadapter (this));                  G.setonitemselectedlistener (this);

At this point, all, end, run the results as follows

Full Source code:

Package Org.hualang.gallery;<br><br>import Android.app.activity;<br>import Android.content.context;<br>import Android.os.bundle;<br>import Android.view.view;<br>import Android.view.viewgroup;<br>import Android.view.window;<br>import Android.view.animation.animationutils;<br>import Android.widget.adapterview;<br>import Android.widget.baseadapter;<br>import Android.widget.gallery;<br>import Android.widget.ImageSwitcher ; <br>import Android.widget.imageview;<br>import Android.widget.AdapterView.OnItemSelectedListener; <br>import Android.widget.gallery.layoutparams;<br>import android.widget.ViewSwitcher.ViewFactory;                <br><br>public class Gallerytest extends Activity implements Onitemselectedlistener,<br> Viewfactory {<br><br> Private Imageswitcher mswitcher;<br><br> private Integer[] MTh     Umbids = {r.drawable.thumb0,<br>                   R.DRAWABLE.THUMB1, r.drawable.thumb2,<br> r.drawable.thumb3, r.drawable.th Umb4,<br> R.drawable.thumb5, r.drawable.thumb6,<br> r.drawable.t                        Humb7};<br><br> Private integer[] Mimageids = {r.drawable.img0, r.drawable.img1,<br> R.drawable.img2, R.DRAWABLE.IMG3, r.drawable.img4,<br> r.drawable.img5, R.drawab Le.img6, R.drawable.img7};<br><br> @Override <br> public void onCreate (Bundle Savedinstan cestate) {<br> super.oncreate (savedinstancestate);<br><br> REQUESTWINDOWF Eature (Window.feature_no_title);<br> Setcontentview (r.layout.main);<br> MSwitch                 ER = (imageswitcher) Findviewbyid (r.id.switcher);<br> mswitcher.setfactory (this);<br> Mswitcher.setinanImation (Animationutils.loadanimation (this,<br> Android).                                r.anim.fade_in));<br> mswitcher.setoutanimation (Animationutils.loadanimation (this,<br> Android. r.anim.fade_out));<br><br> Gallery g = (Gallery) Findviewbyid (r.id.gallery); &LT;BR&GT;&LT;BR&G                T                G.setadapter (New Imageadapter (this)); G.setonitemselectedlistener (This);<br><br>}<br><br> public class Imageadapter Exten                        DS baseadapter {<br> public imageadapter (Context c) {<br>                        Mcontext = c;<br>}<br> public int getcount () {<br> Return mthumbids.length;<br>}<br> public Object getItem (int Position) {<br> return position;<br>}<br> public long getitemid (int position) {<br> Return position;<br>}<br> public View getView (int positio N, View Convertview, viewgroup parent) {<br> ImageView i = new ImageView (mCon                Text);<br><br> I.setimageresource (mthumbids[position]); <br> I.setadjustviewbounds (true);<br> i.setlayou Tparams (New Gallery.layoutparams (<br> layoutparams.wrap_content, Layoutparams .                Wrap_content));  <br> I.setbackgroundresource (r.drawable.picturefrom);<br> return i;<br>}<br> Private Context mcontext;<br>}<br ><br> @Override <br> public void onitemselected (adapterview<?> adapter, View V, int position,<br> long ID) {<br> Mswitcher.setimageresource (Mimageids[po Sition]);<br>}<br> <br> @Override <br> public void Onnothingse                        lected (adapterview<?> arg0) {<br><br>}<br><br> @Override <br>                Public View Makeview () {<br> ImageView i = new ImageView (this);<br>                I.setbackgroundcolor (0xff000000); <br> I.setscaletype (ImageView.ScaleType.FIT_CENTER);<br> i.setlayoutparams (New I Mageswitcher.layoutparams (<br> layoutparams.fill_parent, Layoutparams . fill_parent));<br> return i;<br>}<br>}
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout   xmlns:android= "http://schemas.android.com/apk /res/android "   android:layout_width=" fill_parent "   android:layout_height=" fill_parent ">  < Imageswitcher android:id= "@+id/switcher"   android:layout_width= "fill_parent"   android:layout_height= "fill _parent "   android:layout_alignparenttop=" true "   android:layout_alignparentleft=" true "   />  < Gallery android:id= "@+id/gallery"   android:background= "#55000000"   android:layout_width= "Fill_parent"   android:layout_height= "60DP"   android:layout_alignparentbottom= "true"   android:layout_ Alignparentleft= "true"   android:gravity= "center_vertical"   android:spacing= "16DP"   />  < /relativelayout>

These are the Android UI Control series: Gallery (Gallery view), more about topic.alibabacloud.com (www.php.cn)!

  • 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.