Android beginners: Gallery + ImageSwitcher

Source: Internet
Author: User

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,
&

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.