Gallery exercise (android)

Source: Internet
Author: User

Gallery is used to display images. You need to specify the source adapter of an image, which is inherited from BaseAdapter. The Code is as follows:

Xml Code

<? Xml version = "1.0" encoding = "UTF-8"?>
<Gallery
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/gallery"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>


</Gallery>

 

Java code

Package zziss. android. gallerytest;

Import android. app. Activity;
Import android. app. AlertDialog;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. Gallery;

Public class GalleryTestActivity extends Activity {
/** Called when the activity is first created .*/
Private ImageAdapter2 ia;
Private Gallery iGallery;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
IGallery = (Gallery) this. findViewById (R. id. gallery );
Try {
Ia = new ImageAdapter2 (this );
} Catch (IllegalArgumentException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IllegalAccessException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
IGallery. setBackgroundResource (R. drawable. aa );
IGallery. setAdapter (ia );

IGallery. setOnItemClickListener (new AdapterView. OnItemClickListener ()
{

@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3 ){
AlertDialog dlg = new AlertDialog. Builder (GalleryTestActivity. this)
. SetTitle ("Gallery test ")
. SetMessage ("click" + Integer. toString (arg2) + "image ")
//. SetIcon (ia. mImageIds [arg2])
. SetPositiveButton ("OK", null)
. Create ();
Dlg. setCanceledOnTouchOutside (false );
Dlg. show ();


}

});
}
}

 

 

ImageAdapter code

Package zziss. android. gallerytest;

Import android. content. Context;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageView;

Public class ImageAdapter extends BaseAdapter {

Private Context context;
Public Integer [] mImageIds =
{
R. drawable. phone,
R. drawable. tetris,
R. drawable. tetromino,
R. drawable. text,
R. drawable. videos
};



Public ImageAdapter (Context context ){

This. context = context;
}

@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return mImageIds. length;
}

@ Override
Public Object getItem (int arg0 ){
// TODO Auto-generated method stub
Return arg0;
}

@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}

@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
ImageView iv = new ImageView (context );
Iv. setImageResource (mImageIds [position]);
Iv. setLayoutParams (new Gallery. LayoutParams (60, 60 ));
Iv. setScaleType (ImageView. ScaleType. FIT_CENTER );
Return iv;
}

}

ImageAdater2 code

This is all the images obtained from drawable and does not need to be encoded to specify image resources.

 

Package zziss. android. gallerytest;
Import java. lang. reflect. Field;
Import java. util. ArrayList;

Import zziss. android. gallerytest. R;

Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageView;

Public class ImageAdapter2 extends BaseAdapter {

Private ArrayList <Integer> imgList;
Private ArrayList <Object> sizeList;
Private Context context;
Public ImageAdapter2 (Context context) throws IllegalArgumentException, IllegalAccessException
{
This. context = context;
ImgList = new ArrayList <Integer> ();
SizeList = new ArrayList <Object> ();
Field [] fields = R. drawable. class. getDeclaredFields ();
For (Field field: fields)
{
If (! ("Icon". equals (field. getClass ())))
{
Int index = field. getInt (R. drawable. class );
ImgList. add (index );

Int size [] = new int [2];
Bitmap bitmap = BitmapFactory. decodeResource (context. getResources (), index );
Size [0] = bitmap. getWidth ();
Size [1] = bitmap. getHeight ();
SizeList. add (size );
Bitmap = null;
}
}
}

@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return imgList. size ();
}

@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position;
}

@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}

@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
ImageView iv = new ImageView (context );
Iv. setImageResource (imgList. get (position). intValue ());
Iv. setScaleType (ImageView. ScaleType. FIT_XY );
Int size [] = new int [2];
Size = (int []) sizeList. get (position );
Iv. setLayoutParams (new Gallery. LayoutParams (size [0], size [1]);
Return iv;
}

}

 

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.