Original article: http://blog.csdn.net/android_tutor/article
Step 1: Prepare image materials.
SetIcon2,Icon3,Icon4,Icon5,Icon6Import five imagesRes/drawableAddIcon.pngThere are 6 images in total.
Step 2: NewAndroidProject, namedGallerydemo.
Step 3:DesignUI, ModifyMain. xmlThe Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Background = "@ drawable/White"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: Id = "@ + ID/mytextview01"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: gravity = "center_vertical | center_horizontal"
/>
<Gallery
Android: Id = "@ + ID/mygallery1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "bottom"
/>
</Linearlayout>
Step 4: Design the main program classGallerydemo. JavaThe Code is as follows:
Package com. Android. test;
Import com. Android. Test. R. drawable;
Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. Gallery;
Import Android. widget. imageview;
Public classGallerydemoExtends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
(Gallery) findviewbyid (R. Id. mygallery1). setadapter (New imageadapter (
This ));
}
Public classImageadapterExtendsBaseadapter{
/* The class member mycontext is the context parent class */
Private context mycontext;
/* Use Res/drawable image as the image source */
Private int [] myimageids = {drawable. Icon, drawable. icon2,
Drawable. icon3, drawable. icon4, drawable. icon5, drawable. icon6 };
/* The constructor has only one parameter, that is, the context to be stored */
Public imageadapter (context c ){
This. mycontext = C;
}
/* Return the total number of all defined images */
Public int getcount (){
Return this. myimageids. length;
}
/* Use the getitem method to obtain the array ID of the image in the current container */
Public object getitem (INT position ){
Return position;
}
Public long getitemid (INT position ){
Return position;
}
/* Obtain the view of the image to be displayed, and input the array ID to read and imaging the image */
Public View getview (INT position, view convertview, viewgroup parent ){
/* Create an imageview object */
Imageview I = new imageview (this. mycontext );
I. setimageresource (this. myimageids [position]);
I. setscaletype (imageview. scaletype. fit_xy );
/* Set the width and height of the imageview object, in dip */
I. setlayoutparams (new gallery. layoutparams (120,120 ));
Return I;
}
/* Getscale is used to return the size of views based on the displacement from the center (0.0f to 1.0f )*/
Public float getscale (Boolean focused, int offset ){
/* Formula: 1/(2 ^ offset )*/
Return math. Max (0, 1.0f/(float) math. Pow (2, math. Abs (offset )));
}
}
}
Step 5: Run it. The effect is as follows: