The last time I talked about how to use the Gallery control, this time I will talk about the combination of Gallery and ImageSwitcher. This article implements a simple Image Browsing function. First paste the program running:
In addition to Gallery, you can drag and switch images. I added the setOnTouchListener event implementation to the ImageSwitcher control, so that ImageSwitcher can also switch images in the drag. In this example, we still use the JAVA reflection mechanism to automatically read images from resources.
The source code of main. xml is as follows:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<ImageSwitcher android: id = "@ + id/switcher"
Android: layout_width = "match_parent" android: layout_height = "match_parent"/>
<Gallery android: id = "@ + id/gallery"
Android: background = "#55000000"
Android: layout_width = "match_parent"
Android: layout_alignParentBottom = "true"
Android: layout_alignParentLeft = "true"
Android: gravity = "center_vertical"
Android: spacing = "16dp" android: layout_height = "100dp"/>
</RelativeLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<ImageSwitcher android: id = "@ + id/switcher"
Android: layout_width = "match_parent" android: layout_height = "match_parent"/>
<Gallery android: id = "@ + id/gallery"
Android: background = "#55000000"
Android: layout_width = "match_parent"
Android: layout_alignParentBottom = "true"
Android: layout_alignParentLeft = "true"
Android: gravity = "center_vertical"
Android: spacing = "16dp" android: layout_height = "100dp"/>
</RelativeLayout>
The source code of the program is as follows:
View plaincopy to clipboardprint?
Package com. testImageView;
Import java. lang. reflect. Field;
Import java. util. ArrayList;
Import android. app. Activity;
Import android. content. Context;
Import android. OS. Bundle;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnTouchListener;
Import android. view. ViewGroup;
Import android. view. animation. AnimationUtils;
Import android. widget. AdapterView;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageSwitcher;
Import android. widget. ImageView;
Import android. widget. AdapterView. OnItemSelectedListener;
Import android. widget. Gallery. LayoutParams;
Import android. widget. ViewSwitcher. ViewFactory;
Public class testImageView extends Activity implements ViewFactory {
Private ImageSwitcher is;
Private Gallery gallery;
Private int downX, upX;
Private ArrayList <Integer> imgList = new ArrayList <Integer> (); // image ID
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Use the reflection mechanism to obtain the image ID in the Resource
Field [] fields = R. drawable. class. getDeclaredFields ();
For (Field field: fields)
{
If (! "Icon". equals (field. getName () // image except icon
{
Int index = 0;
Try {
Index = field. getInt (R. drawable. class );
} Catch (IllegalArgumentException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IllegalAccessException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Save the image ID
ImgList. add (index );
}
}
// Set the ImageSwitcher Control
Is = (ImageSwitcher) findViewById (R. id. switcher );
Is. setFactory (this );
Is. setInAnimation (AnimationUtils. loadAnimation (this,
&