The first Google summer College Students' blog sharing competition-2010 andriod
I used my milestone to download and install Tencent's "QQ Browser". I felt that the frequently-used Web site column can be used to drag the image effects like the iPhone, so I wanted to follow suit.
This effect can be easily achieved in Android, but some skills are required, including Android. content. Context, Android. widget. baseadapter, and Android. widget. imageview. In activity, content is equivalent to a canvas. in layout, a gallery object is laid out and the image required by Gallery is stored in the baseadapter container.
1. Write your own internal class imageadapter inheritance and baseadapter, override getcount (), getitem (INT arg0), etitemid (INT arg0), getview (INT position, view convertview, viewgroup parent) method.
2. Import your own image file as the image to be displayed. In the Res/drawable folder, select import and import the file, however, before importing, you must note that the file name must contain lowercase letters, numbers, underscores, and decimal places. Otherwise"Res \ drawable-hdpi \ document.png: invalid file name: must contain only [a-z0-9 _.]"Error message. After successful addition, related records will be automatically added to R. Java:
Public Static Final Class Drawable {
Public Static Final Int Document = 0x7f020000 ;
Public Static Final Int Email = 0x7f020001 ;
Public Static Final Int Icon = 0x7f020002 ;
Public Static Final Int Im = 0x7f020003 ;
Public Static Final Int Music = 0x7f020004 ;
Public Static Final Int Vedio = 0x7f020005 ;
}
3. Add an array to the internal class imageadpter and put the added image resources in the array for future calls
Private Int[] Myimageids=
{
R. drawable. email,
R.drawable.doc ument,
R. drawable. Im,
R. drawable. Music,
R. drawable. vedio
};
4. Create an imageview object in the getview method:
// New Object
Imageview iview = New Imageview ( This . Mycontext );
Iview. setimageresource (myimageids [position]);
Iview. setscaletype (imageview. scaletype. fit_xy );
// Set the width and height of the imageview object
Iview. setlayoutparams ( New Gallery. layoutparams ( 128 , 128 ));
Last:
Download source code
Complete internal class imageadapterCode:
1 Public Class Imageadapter Extends Baseadapter {
2
3 Private Context mycontext = Null ;
4
5 /* Save custom images to several groups */
6 Private Int [] Myimageids =
7 {
8 R.drawable.doc ument,
9 R. drawable. Music,
10 R. drawable. email,
11 R. drawable. Im,
12 R. drawable. vedio
13 };
14
15 /**
16 * Constructor
17 * @ Param C
18 */
19 Public Imageadapter (context c ){
20 This . Mycontext = C;
21 }
22 /**
23 * Returns the total number of images of all definitions.
24 */
25 @ Override
26 Public Int Getcount (){
27 // Todo auto-generated method stub
28 Return This . Myimageids. length;
29 }
30
31 @ Override
32 Public Object getitem ( Int Arg0 ){
33 // Todo auto-generated method stub
34 Return Arg0;
35 }
36
37 @ Override
38 Public Long Getitemid ( Int Arg0 ){
39 // Todo auto-generated method stub
40 Return Arg0;
41 }
42
43 /**
44 * Obtain the view of the image to be displayed.
45 * Input an array id value for reading and Imaging
46 */
47 @ Override
48 Public View getview ( Int Position, view convertview, viewgroup parent ){
49 // Todo auto-generated method stub
50 Imageview iview = New Imageview ( This . Mycontext );
51 Iview. setimageresource (myimageids [position]);
52 Iview. setscaletype (imageview. scaletype. fit_xy );
53 Iview. setlayoutparams ( New Gallery. layoutparams ( 128 , 128 ));
54 Return Iview;
55 }
56
57 }
58