Today continue to explain the Androidui components (blog only to consolidate and continue to learn knowledge----工欲善其事, its prerequisite! )
The following is the main activity of the source, with knowledge points in the explanation, in the previous blog has been explained, simple knowledge points I will be in the form of code comments, OK, no nonsense:
Package com.gc.imageswitcherdemo;/* * Image switcher (Imageswitcher) features and Usage * * 1, Imageswitcher inherits Viewswitcher, So it has the same characteristics as Viewswitcher *: You can use animation effects when switching view components. Imageswitcher inherits Viewswitcher and writes the Shownext (), Showprevious () method of the Viewswitcher, so imageswitcher is easier to use. * 2, use Imageswitcher as long as the following two steps can be. * (1) to provide a viewfactory for Imageswitcher, the viewfactory generated view component must be * ImageView * (2) when the picture needs to be toggled, Just call Imageswitcher's setimagedrawable (drawable drawable) * Setimageresource (int resid) and Setimageuri (Uri Uri) method to change the picture * 3, Imageswitcher and ImageView are a bit similar, they can be used to display pictures, the difference is that the effect of imageswitcher * is more dazzling, it can specify the animation effect when the picture is toggled. *//**** @author: Android General */import Java.util.arraylist;import Java.util.hashmap;import Java.util.list;import Java.util.map;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.view.viewgroup.layoutparams;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.adapterview.onitemselectedlistener;import Android. Widgets. Gridview;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.SimpleAdapter; Import Android.widget.viewswitcher.viewfactory;public class Mainactivity extends Activity {int[] imageids=new int[]{ R.drawable.addresslist1,r.drawable.conference2,r.drawable.daiban3,r.drawable.email4,r.drawable.file5, R.DRAWABLE.INFROM6,R.DRAWABLE.ORGANIZATION7,R.DRAWABLE.PUBLICFILE8,R.DRAWABLE.REQUEST9,R.DRAWABLE.SIGN10, R.DRAWABLE.TASK11,R.DRAWABLE.WORK12};p rivate imageswitcher switcher; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Create a list object, The elements of the list object are maplist<map<string,object>> listitems=new arraylist<map<string,object>> (); int i=0;i<imageids.length;i++) {map<string,object> listitem=new hashmap<string, Object> (); Listitem.put ("image", imageids[i]); Listitems.add (ListItem);} Gets the imageswitcherswitcher= (imageswitcher) Findviewbyid (R.ID.SW) that displays the picture.Itcher);//Set the animation effect of the picture toggle for Imageswitcher switcher.setfactory (new Viewfactory () {@Overridepublic View Makeview () {// Create a ImageView object ImageView imageview=new imageView (mainactivity.this); Imageview.setscaletype (ImageView.ScaleType.FIT _center); Imageview.setlayoutparams (New Imageswitcher.layoutparams (LAYOUTPARAMS.WRAP_CONTENT,LAYOUTPARAMS.WRAP_ (CONTENT)); return imageView;}); /Create a Simpleadaptersimpleadapter simpleadapter=new simpleadapter (this,listitems,r.layout.cell,new String[]{"image" },new int[]{r.id.image1}); GridView grid= (GridView) Findviewbyid (R.ID.GRID01);//For GridView set Adaptergrid.setadapter (Simpleadapter);// Add list item selected listener Grid.setonitemselectedlistener (new Onitemselectedlistener () {@Overridepublic void onitemselected ( Adapterview<?> parent, View view,int position, long ID) {//TODO auto-generated method stub//display the currently selected picture Switcher.set Imageresource (Imageids[position]);} @Overridepublic void onnothingselected (adapterview<?> parent) {//TODO auto-generated Method stub}});// Add a list item clicked Listener Grid.setOnitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View View,int position, long ID) {//Displays the currently selected picture Switcher.setimageresource (Imageids[position]);}});}}Here's a look.
Activity_main.xml
<linearlayout 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:orien tation= "vertical" android:gravity= "center_horizontal" > <!--define a GridView component and <gridview Android:id= "@+id/grid01" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:horizontalspacing= "PT" android:verticalspacing= "2pt" android:numcolumns= "4" Android:gra vity= "Center"/> <!--define a Imageswitcher component, Inanimation and outanimation are animated when switching pictures, the system provided by default is an animation---< ; Imageswitcher android:id= "@+id/switcher" android:layout_width= "300DP" android:layout_height= "300DP" Android:layout_gravity= "Center_horizontal" android:inanimation= "@android: Anim/slide_in_left" android:outAn Imation= "@android: Anim/slide_out_right"/></LinearLayout>
Effect:
Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/25594669