Gallery and imageswitcher are used in combination. This article provides a simple Image Browsing function.
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.
<? 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>
ProgramThe source code 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: 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,
Android. R. anim. fade_in ));
Is. setoutanimation (animationutils. loadanimation (this,
Android. R. anim. fade_out ));
Is. setontouchlistener (New ontouchlistener (){
/*
* You can slide on the imageswitcher control to switch between images.
*/
@ Override
Public Boolean ontouch (view V, motionevent event ){
If (event. getaction () = motionevent. action_down)
{
Downx = (INT) event. getx (); // gets the coordinates of the press.
Return true;
}
Else if (event. getaction () = motionevent. action_up)
{
UPX = (INT) event. getx (); // gets the coordinates of the release.
Int Index = 0;
If (UPX-downx> 100) // drag from left to right, that is, view the previous
{
// If it is first, go to the end
If (gallery. getselecteditemposition () = 0)
Index = gallery. getcount ()-1;
Else
Index = gallery. getselecteditemposition ()-1;
}
Else if (downx-UPX> 100) // drag from the right to the left, that is, the last
{
// If it is the last, go to the first
If (gallery. getselecteditemposition () = (gallery. getcount ()-1 ))
Index = 0;
Else
Index = gallery. getselecteditemposition () + 1;
}
// Change the gallery Image Selection and automatically trigger the setonitemselectedlistener of imageswitcher.
Gallery. setselection (index, true );
Return true;
}
Return false;
}
});
// Set the gallery Control
Gallery = (Gallery) findviewbyid (R. Id. Gallery );
Gallery. setadapter (New imageadapter (this ));
Gallery. setonitemselectedlistener (New onitemselectedlistener (){
@ Override
Public void onitemselected (adapterview <?> Arg0, view arg1,
Int position, long arg3 ){
Is. setimageresource (imglist. Get (position ));
}
@ Override
Public void onnothingselected (adapterview <?> Arg0 ){
// Todo auto-generated method stub
}
});
}
// Set imgaeswitcher
@ Override
Public View makeview (){
Imageview I = new imageview (this );
I. setbackgroundcolor (0xff000000 );
I. setscaletype (imageview. scaletype. center); // center
I. setlayoutparams (New imageswitcher. layoutparams (// adaptive image size
Layoutparams. fill_parent, layoutparams. fill_parent ));
Return I;
}
Public class imageadapter extends baseadapter {
Public imageadapter (context c ){
Mcontext = C;
}
Public int getcount (){
Return imglist. Size ();
}
Public object getitem (INT position ){
Return position;
}
Public long getitemid (INT position ){
Return position;
}
Public View getview (INT position, view convertview, viewgroup parent ){
Imageview I = new imageview (mcontext );
I. setimageresource (imglist. Get (position ));
I. setadjustviewbounds (true );
I. setlayoutparams (new gallery. layoutparams (
Layoutparams. wrap_content, layoutparams. wrap_content ));
Return I;
}
Private context mcontext;
}
}