Continue with the AndroidUI component explanation today (write a blog to consolidate and continue to learn knowledge-you must first sharpen your skills to do things well !)
The following is the source code of the main Activity, which contains a description of the knowledge points. As described in the previous blog, I will explain the simple knowledge points in the form of code comments:
Package com. gc. imageswitcherdemo;/** functions and usage of ImageSwitcher ** 1. ImageSwitcher inherits ViewSwitcher, so it has the same features as ViewSwitcher *: you can use the animation effect when switching the View component. ImageSwitcher inherits ViewSwitcher and overwrites * ViewSwitcher's showNext () and showPrevious () methods. Therefore, ImageSwitcher is easier to use. * 2. Use ImageSwitcher in the following two steps. * (1) provide a ViewFactory for ImageSwitcher. The View component generated by this ViewFactory must be * ImageView * (2) when you need to switch an image, you only need to call the setImageDrawable (Drawable drawable) of ImageSwitcher) * The setImageResource (int resid) and setImageURI (Uri uri) Methods change the image. * 3. ImageSwitcher and ImageView functions are similar. They can be used to display images, the difference is that ImageSwitcher * is more effective. It can specify the animation effect during image switching. * // ***** @ 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. widget. 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}; private ImageSwitcher switcher; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // creates a List object. MapList
> ListItems = new ArrayList
> (); For (int I = 0; I
ListItem = new HashMap
(); ListItem. put ("image", imageIds [I]); listItems. add (listItem);} // obtain the ImageSwitcherswitcher = (ImageSwitcher) findViewById (R. id. switcher); // set the animation effect switcher for image witcher. setFactory (new ViewFactory () {@ Overridepublic View makeView () {// create an 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); // set Adaptergrid for the GridView. setAdapter (simpleAdapter); // Add the grid of the listener selected by the list item. setOnItemSelectedListener (new OnItemSelectedListener () {@ Overridepublic void onItemSelected (AdapterView
Parent, View view, int position, long id) {// TODO Auto-generated method stub // display the currently selected image switcher. setImageResource (imageIds [position]);} @ Overridepublic void onNothingSelected (AdapterView
Parent) {// TODO Auto-generated method stub}); // Add the listener grid. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
Parent, View view, int position, long id) {// display the currently selected image switcher. setImageResource (imageIds [position]) ;}});}
Next let's take a look.
activity_main.xml
Effect:
Reprinted please indicate the source: http://blog.csdn.net/android_jiangjun/article/details/25594669