<1> Introduction
This is a very dazzling effect. You can drag the image with your fingers to move it. The iPhone once attracted countless Apple fans with this special effect, which can also be achieved on the Android platform.
To achieve this, you must have a container to store the image requested by Gallery. Here, we use a derived class inherited from baseadapter to install these images. We need to listen to the setonitemclicklistener event to determine the image selected currently.
First, we need to store the index of the displayed image in an int array. Then, you can use the setimageresource method to set the image resources to be displayed in the imageview. Finally, the imageview of each image is displayed on the screen.
<2> XML attributes
Attribute name |
Description |
Android: animationduration |
Set the time required for converting the animation when the layout changes (in milliseconds ). Timing only when the animation starts. The value must be an integer, such as 100. |
Android: gravity |
Specifies how to place content on the X and Y axes of an object. Specify one or more constants (separated by "|)
Constant |
Value |
Description |
Top |
0x30 |
Close to the Container top without changing its size |
Bottom |
0x50 |
Close to the bottom of the container without changing its size |
Left |
0x03 |
Close to the left side of the container without changing its size |
Right
|
0x05 |
Close to the right of the container without changing its size |
Center_vertical
|
0x10 |
Center vertically without changing its size |
Fill_vertical
|
0x70 |
Stretch vertically to filled containers |
Center_horizontal
|
0x01 |
Center horizontally without changing its size |
Fill_horizontal
|
0x07 |
Horizontally stretch to fill the container |
Center
|
0x11 |
Center alignment without changing its size |
Fill
|
0x77 |
Stretch horizontally and vertically to fill the container |
Clip_vertical
|
0x80 |
Vertical cutting (when the object edge exceeds the container, cut out the upper and lower edges) |
Clip_horizontal
|
0x08 |
Horizontal cut (when the object edge exceeds the container, cut out the left and right edges beyond the container) |
|
Android: spacing |
(Translator's note: Set the spacing between images) |
Android: unselectedalpha |
Sets the transparency (alpha) of unselected items ). The value must be of the float type, for example, "1.2 ". |
<3> example
Package xiaosi. gallery; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. view; import android. view. viewgroup; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. baseadapter; import android. widget. gallery; import android. widget. imageview; import android. widget. toast; public class galleryactivit Y extends activity {/** called when the activity is first created. */private gallery = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); gallery = (Gallery) findviewbyid (R. id. gallery); // sets the image adapter Gallery. setadapter (New imageadapter (this); Gallery. setspacing (10); // sets the listener Gallery. setonitemclicklistener (New oni Temclicklistener () {public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {toast. maketext (galleryactivity. this, "click the" + arg2 + "image", toast. length_long ). show () ;}}) ;}} class imageadapter extends baseadapter {private context; // image source array private integer [] imageinteger = {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e, R. drawable. f ,}; public imageadapter (context c) {context = C ;}// obtain the number of images public int getcount () {return imageinteger. length ;}// obtain the position of the image in the library public object getitem (INT position) {return position ;}// obtain the image ID public long getitemid (INT position) {return position ;} public View getview (INT position, view convertview, viewgroup parent) {imageview = new imageview (context); // set the imageview resource for imageview. setimageresource (imageinteger [position]); // sets the display ratio type imageview. setscaletype (imageview. scaletype. fit_xy); // set the layout image 120*80 imageview. setlayoutparams (new gallery. layoutparams (120, 80); Return imageview ;}}
Main. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="bottom" android:background="?android:galleryItemBackground"/></LinearLayout>