Introduction to Android Gallery Usage Example Analysis _android

Source: Internet
Author: User

The Android Gallery control, described in this article, is a very good looking map control, which can greatly reduce the developer's ability to look at the map, and the effect is very beautiful. The use of gallery in the example of this paper mainly realizes the dynamic reading of images in resources using reflection mechanism.

The example code runs with the following effect diagram:

Main.xml source code is as follows:

<?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" 
  > 
<gallery android:id= "@+id/gallery" android:layout_height= "Fill_parent" Android:layout_width= "Fill_parent" ></Gallery> 
</LinearLayout>

Java program 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.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.AdapterView; 
Import Android.widget.BaseAdapter; 
Import Android.widget.Gallery; 
Import Android.widget.ImageView; 
 
Import Android.widget.AdapterView.OnItemClickListener; 
  public class Testimageview extends activity {private Gallery mgallery; 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
     
    Setcontentview (R.layout.main); 
    MGallery = (Gallery) Findviewbyid (r.id.gallery); 
    try {mgallery.setadapter (new Imageadapter (this)); 
    catch (IllegalArgumentException e) {//TODO auto-generated catch block E.printstacktrace (); catch (illegalaccessexceptIon e) {//TODO auto-generated catch block E.printstacktrace ();  Mgallery.setonitemclicklistener (New Onitemclicklistener () {public void Onitemclick (adapterview parent, View 
      V, int position, long id) {TestImageView.this.setTitle (string.valueof (position)); 
  } 
    }); 
   /* * Class Imageadapter is used to control gallery source and operation. 
    * * Private class Imageadapter extends baseadapter{private context mcontext; 
    Private arraylist<integer> imglist=new arraylist<integer> (); 
    Private arraylist<object> imgsizes=new arraylist<object> (); 
       
      Public Imageadapter (context C) throws IllegalArgumentException, illegalaccessexception{mcontext = c; 
      Use reflection mechanism to get picture IDs and dimensions in resources field[] fields = R.drawable.class.getdeclaredfields (); for (Field field:fields) {if (!) Icon ". Equals (Field.getname ())//except icon image {int index=fIeld.getint (R.drawable.class); 
          Save picture ID Imglist.add (index); 
          Save picture size int size[]=new int[2]; 
          Bitmap Bmimg=bitmapfactory.decoderesource (getresources (), index); 
          Size[0]=bmimg.getwidth (); Size[1]=bmimg.getheight (); 
        Imgsizes.add (size); @Override public int GetCount () {//TODO auto-generated method Stub return IM 
    Glist.size (); @Override public Object getitem (int position) {//TODO auto-generated a stub return PO 
    sition; @Override public long getitemid (int position) {//TODO auto-generated a stub return PO 
    sition; @Override public View getview (int position, View Convertview, ViewGroup parent) {//TODO Auto-genera 
      Ted Method Stub ImageView i = New ImageView (mcontext); 
 Get picture ID I.setimageresource from imglist (imglist.get (position). Intvalue ());     I.setscaletype (ImageView.ScaleType.FIT_XY); 
      From imgsizes get picture size int size[]= new INT[2]; 
      Size= (int[]) imgsizes.get (position); 
      I.setlayoutparams (New Gallery.layoutparams (size[0], size[1)); 
    return i; 
} 
     
  }; }

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.