My android advanced tour --> two image browsers with preview and slide are available for Android gallery and gridview, and imageswitcher.

Source: Internet
Author: User

I. Introduction

A. Functions and usage of the gridview (network view)





B. Functions and usage of imageswitcher




C. Functions and usage of Gallery (GALLERY view)



2. Use an instance to learn Gallery, gridview, and imageswitcher

Step 1: Create a project named mygridviewandgallery.



3. Use gridview and imageswitcher to implement them first.

Step 1: Design the UI

A./layout/gridview. xml

<?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"    android:gravity="center"    >   <GridView      android:id="@+id/grid01"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:horizontalSpacing="2pt"    android:verticalSpacing="2pt"    android:numColumns="4"    android:gravity="center"    />    <ImageSwitcher     android:id="@+id/switcher"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_gravity="center_horizontal"    /></LinearLayout>

B. UI/layout/cell. xml of each cell

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent">   <ImageView  android:id="@+id/image1" android:layout_width="50dp"    android:layout_height="50dp"    android:layout_gravity="center_horizontal"/></LinearLayout>

Step 2: mygridviewactivity. Java

Package CN. roco. gridview; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. viewgroup. layoutparams; import android. view. animation. animationutils; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. adapt Erview. onitemselectedlistener; import android. widget. gridview; import android. widget. imageswitcher; import android. widget. imageview; import android. widget. simpleadapter; import android. widget. viewswitcher. viewfactory; public class mygridviewactivity extends activity {int [] imageids = new int [] {R. drawable. beautiful_001, R. drawable. beautiful_002, R. drawable. beautiful_003, R. drawable. beautiful_004, R. drawa Ble. beautiful_005, R. drawable. beautiful_006, R. drawable. beautiful_007, R. drawable. beautiful_008, R. drawable. beautiful_009, R. drawable. beautiful_010, R. drawable. beautiful_011, R. drawable. beautiful_012, R. drawable. beautiful_013};/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. gridview ); List <Map <string, Object> listitems = new arraylist <Map <string, Object> (); For (INT I = 0; I <imageids. length; I ++) {Map <string, Object> listitem = new hashmap <string, Object> (); listitem. put ("image", imageids [I]); listitems. add (listitem);} // obtain the imageswitcherfinal imageswitcher switcher = (imageswitcher) findviewbyid (R. id. switcher); // sets the animation effect Switcher for image switching. setinanimation (animationutils. loadanimatio N (this, android. r. anim. fade_in); switcher. setoutanimation (animationutils. loadanimation (this, android. r. anim. fade_out); // set the image Switcher for the imageswitch. setfactory (New viewfactory () {@ overridepublic view makeview () {imageview = new imageview (mygridviewactivity. this); imageview. setbackgroundcolor (0xff0000); imageview. setscaletype (imageview. scaletype. fit_center); imageview. setlayoutparams (New im Ageswitcher. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); Return imageview ;}}); // create a simpleadapter // use/layout/cell. XML serves as the interface layout simpleadapter = new simpleadapter (this, listitems, R. layout. cell, new string [] {"image"}, new int [] {R. id. image1}); gridview = (gridview) findviewbyid (R. id. grid01); // set simpleadaptergridview for the gridview. setadapter (simpleadapter );// Process the selected event gridview. setonitemselectedlistener (New onitemselectedlistener () {@ overridepublic void onitemselected (adapterview <?> Parent, view, int position, long ID) {// display the currently selected image switcher. setimageresource (imageids [position % imageids. length]) ;}@ overridepublic void onnothingselected (adapterview <?> Parent) {}}); // process the clicked event gridview. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {// display the clicked image switcher. setimageresource (imageids [position % imageids. Length]) ;}}}

Iv. Image browsers using gallery and imageswitch

Step 1: Design the UI/layout/gallery. xml

<?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"    android:gravity="center"    >    <ImageSwitcher     android:id="@+id/gallerySwitcher"    android:layout_width="320dp"    android:layout_height="320dp"    />        <Gallery android:id="@+id/gallery"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_marginTop="25dp"    android:unselectedAlpha="0.6"    android:spacing="3dp"     /></LinearLayout>

Step 2: mygralleryactivity. Java

Package CN. roco. gridview; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. viewgroup; import android. view. viewgroup. layoutparams; 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. IMA Geswitcher; import android. widget. imageview; import android. widget. viewswitcher. viewfactory; public class mygralleryactivity extends activity {int [] imageids = new int [] {R. drawable. beautiful_001, R. drawable. beautiful_002, R. drawable. beautiful_003, R. drawable. beautiful_004, R. drawable. beautiful_005, R. drawable. beautiful_006, R. drawable. beautiful_007, R. drawable. beautiful_008, R. drawable. beautiful_009, R. Drawable. beautiful_010, R. drawable. beautiful_011, R. drawable. beautiful_012, R. drawable. beautiful_013};/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. gallery); Final gallery = (Gallery) findviewbyid (R. id. gallery); Final imageswitcher switcher = (imageswitcher) findviewbyid (R. Id. galleryswitcher); // sets the image Switcher for imageswitch. setfactory (New viewfactory () {@ overridepublic view makeview () {imageview = new imageview (mygralleryactivity. this); imageview. setbackgroundcolor (0xff0000); imageview. setscaletype (imageview. scaletype. fit_center); imageview. setlayoutparams (New imageswitcher. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); Return imageview ;} }); // Set the animation effect Switcher for image switching. setinanimation (animationutils. loadanimation (this, android. r. anim. fade_in); switcher. setoutanimation (animationutils. loadanimation (this, android. r. anim. fade_out); // create a baseadapter object that provides the baseadapter adapter = new baseadapter () {@ overridepublic view getview (INT position, view convertview, viewgroup parent) {imageview = new imageview (mygralleryacti Invalid. this); imageview. setimageresource (imageids [position % imageids. length]); imageview. setscaletype (imageview. scaletype. fit_xy); imageview. setlayoutparams (new gallery. layoutparams (75,100); Return imageview ;}@ overridepublic long getitemid (INT position) {return position ;}@ overridepublic object getitem (INT position) {return position ;} @ overridepublic int getcount () {return imageids. length ;}}; Gallery. set Adapter (adapter); // process the selected event gallery. setonitemselectedlistener (New onitemselectedlistener () {@ overridepublic void onitemselected (adapterview <?> Parent, view, int position, long ID) {// display the currently selected image switcher. setimageresource (imageids [position % imageids. length]) ;}@ overridepublic void onnothingselected (adapterview <?> Parent ){}});}}

5. Use tabhost to display two image browsers

Myintenttab. Java

Package CN. roco. gridview; import android. app. tabactivity; import android. content. intent; import android. OS. bundle; import android. view. window; import android. view. windowmanager; import android. widget. tabhost; public class myintenttab extends tabactivity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); // request full screen getwindow (). requestfeature (window. feature_no_title); // the request does not contain the title bar tabhost = gettabhost (); // use intent to add it to the first tab page tabhost. addtab (tabhost. newtabspec ("grallery "). setindicator ("grallery "). setcontent (new intent (this, mygralleryactivity. class); // use intent to add to the second tab page tabhost. addtab (tabhost. newtabspec ("gridview "). setindicator ("gridview "). setcontent (new intent (this, mygridviewactivity. class )));}}

6. androidmanifest. xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.roco.gridview"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".MyIntentTab"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity><activity android:name=".MyGralleryActivity"/><activity android:name=".MyGridViewActivity"/>    </application></manifest>

7. Deploy the application to the simulator to view the Running Effect


A. Slide picture Browser




B. Picture browser with image Preview





========================================================== ========================================================== ============================

Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng

========================================================== ========================================================== ============================

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.