An example analysis of gallery usage of Android control _android

Source: Internet
Author: User

This example describes the gallery usage of the Android control. Share to everyone for your reference. Specifically as follows:
The gallery component is used primarily for landscape display of image lists, although it is routinely done. The gallery component can only display the specified image in a limited way. That is, if you specify 10 images for the gallery component, the Gallery component will not continue to display when it is displayed to the 10th sheet. It doesn't matter most of the time, but in some cases we want the image to be displayed to the last one and then the 1th one to start showing, which is the loop display. To implement this style of gallery components, you need to make improvements to the gallery adapter objects.

The following gallery simulates looping through the image, displaying an enlarged image below (using the Imageswitcher component) when you click an image in one of the gallery components.

Directory structure

Main.xml Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" vertical " 
 android:layout_width=" fill_parent "
 android:layout_" height= "Fill_parent" >
 <!--android:unselectedalpha: Sets the transparency (Alpha) of unselected entries. The value must be a float type, such as: "1.2". -->
 <gallery android:layout_width= "fill_parent"
  android:layout_height= "Wrap_content" 
  android: spacing= "10dip" 
  android:unselectedalpha= "1.2"
  android:id= "@+id/gallery"
  android:layout_margintop = "30DP"/>
 <imageswitcher android:id= "@+id/imageswitcher"
  android:layout_width= "Fill_parent"
  android:layout_height= "wrap_content"
  android:layout_margintop= "30DP"/>
</linearlayout >

Galleryactivity class:

Package com.ljq.ga;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.animation.AnimationUtils;
Import Android.widget.AdapterView;
Import Android.widget.Gallery;
Import Android.widget.ImageSwitcher;
Import Android.widget.ImageView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.LinearLayout.LayoutParams;
Import Android.widget.ViewSwitcher.ViewFactory;
 public class Galleryactivity extends activity implements Viewfactory {private Gallery Gallery = null;
 Private Imageswitcher Imageswitcher=null; Int[] imageids={r.drawable.p1,r.drawable.p2,r.drawable.p3, R.DRAWABLE.P4,R.DRAWABLE.P5,R.DRAWABLE.P6, R.drawabl
 E.P7,R.DRAWABLE.P8};
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main); Imageswitcher= (Imageswitcher) Findviewbyid (R.ID.IMAGESWItcher);
  Set the factory object for the Imageswitcher component Imageswitcher.setfactory (this); Sets the animation effect of the Imageswitcher component display Image imageswitcher.setinanimation (this, Android.  
  r.anim.fade_in)); Imageswitcher.setoutanimation (This, Android. Animationutils.loadanimation.
  R.anim.fade_out));
  Gallery = (gallery) Findviewbyid (r.id.gallery);
  Imageviewadapter adapter=new imageviewadapter (Galleryactivity.this, imageids);
  Gallery.setadapter (adapter); Gallery.setonitemselectedlistener (New Onitemselectedlistener () {public void onitemselected (adapterview<?> Parent, view view, int position, long ID) {//When an image in Gallery is selected, the image is magnified in the Imageswitcher component Imageswitcher.setimageresou
   Rce (Imageids[position%imageids.length]);
  public void onnothingselected (Adapterview<?> arg0) {}}); Gallery.setonitemclicklistener (New Onitemclicklistener () {public void Onitemclick (adapterview<?> parent, View view, int position, long id) {LOG.I ("Ljq", "parent=" +paRent.getclass (). GetName ()); Gallery log.i ("Ljq", "view=" +view.getclass (). GetName ()); ImageView log.i ("Ljq", "position=" + position);
    1 log.i ("LJQ", "id=" + ID);//1 Gallery gl= (Gallery) parent;
   ImageView iv= (imageview) view;
 }
  }); The//Imageswitcher component requires this method to create a View object (typically a ImageView object)//To display the image public View Makeview () {ImageView ImageView = new Ima
  Geview (this);
  Imageview.setbackgroundcolor (0xff000000);
  Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER);
  Imageview.setlayoutparams (New Imageswitcher.layoutparams (Layoutparams.fill_parent, LayoutParams.FILL_PARENT));
 return ImageView;

 }
}

Imageviewadapter Custom Adapter:

Package com.ljq.ga;
Import Android.content.Context;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
 public class Imageviewadapter extends baseadapter{private int[] imageids=null;
 Private context Context=null;
  Public Imageviewadapter (context context, int[] imageids) {this.context=context;
 This.imageids=imageids;
 //To return the total number of images, note that this total cannot be greater than the actual number of the image (can be less than the actual number of images), or you will throw a cross boundary exception.
  public int GetCount () {//optimize a//return imageids.length;
 return integer.max_value;
 Public Object getitem (int position) {return imageids[position];
 public long getitemid (int position) {return position; The//scaletype usage//center/center is centered by the original size of the picture, and when the picture is longer/wider than the view's length/width, the center portion of the image is displayed//center_crop/centercrop Enlarge the size of the picture in proportion to the center so that the length (width) of the picture is equal to or greater than the view's long (wide)//center_inside/centerinside to center the contents of the picture completely, by scaling or the original size to make the picture long/ Long/wide//fit_center/fitcenter with width equal to or less than view to enlarge/shrink the picture proportionally to the width of view, centered display//fit_End/fitend the picture proportionally to the width of the view, shown in the lower part of the view//fit_start/fitstart the picture is scaled/reduced to the width of the view, displayed in the upper part of the View//fit_xy/fitxy
  Enlarge/Shrink picture to view size display//matrix/matrix use matrices to draw public View GetView (int position, View Convertview, ViewGroup parent) {
  ImageView IV = new ImageView (context);
  Optimization two, through the acquisition of Imageids array of the image resource ID, the remainder can be greatly less waste of resources iv.setimageresource (Imageids[position%imageids.length]);
  Iv.setscaletype (ImageView.ScaleType.CENTER_INSIDE);
 Iv.setlayoutparams (New Linearlayout.layoutparams (77,77));//reduce the image to the original 60% return IV;

 }
}

Run results

I hope this article will help you with your Android program.

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.