Android Learning---Gallery gallery view

Source: Internet
Author: User

Gallery and Spinner have a common parent class: Abspinner, which shows that gallery and spinner are both a list box. The difference between them is that spinner displays a vertical list selection box, and gallery displays a horizontal list selection box. The role of spinner is for the user to choose, while Gallery allows the user to drag to view the previous, next.

Gallery usage is similar to spinner usage, as long as it provides a content adapter. The GetView method of adapter Returns the list item for the view as a gallery list. If the program needs to monitor changes to the gallery selection, you can add Onitemselectedlistener listening.


XML attributes for Gallery



Here's a slideshow example to familiarize yourself with gallery


(1) Activity_main.xml layout a imageswitcher and gallery imageswitcher for displaying gallery selected pictures

<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/imgswt"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"/>    <gallery         android:id= "@+id/gallery"        android:layout_width= "fill_parent"        android:layout_ height= "Wrap_content"        android:layout_margintop= "25DP"        android:unselectedalpha= "0.7"        android: spacing= "2pt"/></linearlayout>

(2) Mainactivity.java

Package Com.example.gallery;import Android.app.activity;import Android.os.bundle;import android.view.Menu;import Android.view.view;import Android.view.viewgroup;import Android.view.animation.animationutils;import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.baseadapter;import Android.widget.gallery;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.radiogroup.layoutparams;import Android.widget.viewswitcher.viewfactory;public class Mainactivity extends Activity {//define component private Imageswitcher IMGSWT = null;private Gallery Gallery = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Get layout component IMGSWT = (imageswitcher) Findviewbyid ( R.ID.IMGSWT) Gallery = (gallery) Findviewbyid (r.id.gallery);//Picture final int images[] = new INT[]{R.DRAWABLE.NAME01, R.drawable.name02,r.drawable.name03,r.drawable.name04,r.drawable.naMe05,r.drawable.name06,r.drawable.name07,r.drawable.name08,r.drawable.name09,r.drawable.name10, r.drawable.name11,r.drawable.name12};//Set Picture Toggle Effect Imgswt.setinanimation (Animationutils.loadanimation (This, Android . r.anim.fade_in)); Imgswt.setoutanimation (Animationutils.loadanimation (this, Android. r.anim.fade_out));//Set Viewfactory object Imgswt.setfactory (New Viewfactory () {@Overridepublic View Makeview () {ImageView ImageView = new ImageView (mainactivity.this); Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER); Imageview.setlayoutparams (New Imageswitcher.layoutparams (350,350)); return imageView;}); /Create Baseadapter object, responsible for providing gallery display all images baseadapter adapter = new Baseadapter () {@Overridepublic View getView (int position, View Convertview, ViewGroup parent) {//create Imageviewimageview ImageView = new ImageView (mainactivity.this); Imageview.setimageresource (Images[position]); Imageview.setscaletype (ImageView.ScaleType.FIT_XY); Imageview.setlayoutparams (New Gallery.layoutparams (Layoutparams.wrap_content,layoutparaMs. wrap_content)); return ImageView;} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic Object getItem (int position) {return position;} @Overridepublic int GetCount () {return images.length;}};/ /set Adapter Gallery.setadapter (adapter) to Gallery;//Add Event Gallery.setonitemselectedlistener (new Onitemselectedlistener () { @Overridepublic void onitemselected (adapterview<?> parent, View view,int position, long ID) { Imgswt.setimageresource (Images[position]);} @Overridepublic void onnothingselected (adapterview<?> parent) {//TODO auto-generated Method stub}});}}

The results are as follows:


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.