Introduction to Android Gallery+imageswitcher Usage Example Analysis _android

Source: Internet
Author: User
Tags reflection

Following the previous article on how to use the gallery control, this article describes the combination of gallery and imageswitcher use. The example code described in this article will implement a simple browsing image function.

First post the program to run the screenshot as follows:

In addition to the gallery can be dragged to toggle the picture, I imageswitcher control to add the Setontouchlistener event implementation, so that imageswitcher can also be in the drag to toggle the picture. This example still uses the Java reflection mechanism to automatically read the pictures in the resource.

Main.xml source code is as follows:

<?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 Java program's source code is as follows:

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 (Bund Le Savedinstancestate) {//TODO auto-generated methodStub super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
   Use the reflection mechanism to get the picture ID in the resource field[] fields = R.drawable.class.getdeclaredfields (); for (Field field:fields) {if (!)
  Icon ". Equals (Field.getname ()))//except icon image {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 Picture ID Imglist.add (index);
 }//Set Imageswitcher control is = (Imageswitcher) Findviewbyid (R.id.switcher);
 Is.setfactory (this); Is.setinanimation (This, Android. Animationutils.loadanimation.
 r.anim.fade_in)); Is.setoutanimation (This, Android. Animationutils.loadanimation.
 R.anim.fade_out)); Is.setontouchlistener (New Ontouchlistener () {/* * * sliding on imageswitcher control to toggle Picture/@Override Public boolean Ontouch ( View V, motionevent event) {if (EveNt.getaction () ==motionevent.action_down) {downx= (int) event.getx ();//Get the coordinate return true when pressed;
   else if (event.getaction () ==motionevent.action_up) {upx= (int) event.getx ();//get the coordinate int index=0 when released; if (upx-downx>100)//From left to right, that is, look at the previous one {//if first, then go to the tail if (gallery.getselecteditemposition () ==0) index=gallery.get
   Count ()-1;
   else Index=gallery.getselecteditemposition ()-1; else if (downx-upx>100)//From right to left, that is, look at the next {//If the last one, then go to the first if (gallery.getselecteditemposition () = = (Gallery.getcou
   NT ()-1)) index=0;
   else index=gallery.getselecteditemposition () +1;
   ///Change gallery picture selected, automatically triggers Imageswitcher setonitemselectedlistener gallery.setselection (index, true);
  return true;
  return false;
 
 }
  
 });
 Set the gallery Control Gallery = (gallery) Findviewbyid (r.id.gallery);
 Gallery.setadapter (This) (new Imageadapter); 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 picture size
 Layoutparams.fill_parent, layoutparams.fill_parent));
 return i;
 The 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;

 }
}
Related Article

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.