Has it ever been attracted by the iPhone's sliding fingers on the screen to slide and drag images? In Android, this is not difficult. You only need to use Gallery. It is usually used in the design of album, image type selector.
Before starting, let's take a look at what is context and the baseadapter in the namespace of Android. widget. In the activity, context is like a canvas, waiting for processing or overwriting at any time. Do you still remember that I used intent and context applications when I introduced inter-activity transfer? Intent is a class in the namespace of Android. content, and the context is the same. This example layout a gallery object in layout, and then store the image required by gallery through the baseadapter container. Put the image you want to display in the drawable folder and compile the project so that it can be used in the code.
The structure of Main. axml is very simple. Just put a gallery:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <gallery Android: Id = "@ + ID/Gallery" Android: layout_height = "fill_parent" Android: layout_width = "fill_parent"> </gallery> <br/> </linearlayout> <br/>
Another focus of this example is how to set the width and height of the gallery image and the size of the Image Layout. Here we create an imageadapter class to inherit the baseadapter container to store the image, you can use the setscaletype method of imageview to change the image display, and then use the layoutparameters attribute class to change the width and height of layout.
Create a new attrs under the values directory. XML file, which is a self-made layout element usage, defines the <declare-styleable> label in it, the purpose is to customize the layout background style, and through android. content. res. the features of the typedarray class allow identical layout elements to be repeatedly used for each image.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <declare-styleable name = "Gallery"> <br/> <ATTR name = "Android: galleryitembackground "/> <br/> </declare-styleable> <br/> </resources>
The structure of the imageadapter class is as follows:
Public class imageadapter: baseadapter <br/>{< br/> private context CTX; <br/> int mgalleryitembackground; <br/> private int [] images = <br/>{< br/> resource. drawable. btn1, <br/> resource. drawable. btn1_ B, <br/> resource. drawable. btn2, <br/> resource. drawable. btn2_ B, <br/> resource. drawable. btn3, <br/> resource. drawable. btn3_ B, <br/> resource. drawable. btn4, <br/> resource. drawable. btn4_ B <br/>}; <br/> Public imageadapter (context CTX) <br/>{< br/> try <br/>{< br/> This. CTX = CTX; <br/> android. content. res. typedarray A = CTX. obtainstyledattributes (resource. styleable. gallery); <br/> mgalleryitembackground =. getresourceid (resource. styleable. gallery_android_galleryitembackground, android. graphics. color. azure); <br/>. recycle (); <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (CTX, ex ); <br/>}< br/> Public override int count <br/>{< br/> Get <br/>{< br/> return images. length; <br/>}< br/> Public override Java. lang. object getitem (INT position) <br/>{< br/> return position; <br/>}< br/> Public override long getitemid (INT position) <br/>{< br/> return position; <br/>}< br/> Public override view getview (INT position, view convertview, viewgroup parent) <br/>{< br/> try <br/> {<br/> imageview v = new imageview (this. CTX); <br/> v. setimageresource (this. images [position]); <br/> v. setscaletype (imageview. scaletype. fitxy); <br/> v. layoutparameters = new gallery. layoutparams (100, 50); <br/> v. setbackgroundresource (mgalleryitembackground); <br/> return V; <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this. CTX, ex); <br/> return NULL; <br/>}< br/>
Activity1.cs code:
[Activity (Label = "monodroidtest", mainlauncher = true)] <br/> public class activity1: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> try <br/> {<br/> gallery mgallery = findviewbyid <gallery> (resource. id. gallery); <br/> mgallery. adapter = new imageadapter (this); <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this, ex); <br/>}< br/>
Run the program as follows:
The next question is, can we read the images on our SD card instead of directly using the images as resources in the package? This is certainly possible. Next we will rewrite the imageadapter. From the example below, we can also learn how to read the SD card file (remember using Java. IO ;):
Public class imageadapter: baseadapter <br/>{< br/> private context CTX; <br/> int mgalleryitembackground; <br/> file [] files; <br/> int width, height; <br/> Public imageadapter (context CTX) <br/>{< br/> try <br/>{< br/> This. CTX = CTX; <br/> displaymetrics dm = new displaymetrics (); <br/> (activity) This. CTX ). windowmanager. defaultdisplay. getmetrics (DM); <br/> width = DM. widthpixels; <br/> Height = DM. heightpixels; <br/> If (Android. OS. environment. externalstoragestate. equals (Android. OS. environment. mediamounted) <br/>{< br/> File Path = android. OS. environment. externalstoragedirectory; <br/> file = new file (path. path + "/pictures/camera"); <br/> If (file. exists () <br/> files = file. listfiles (); <br/> else <br/> throw new system. exception ("this path does not exist in the SD card"); <br/>}< br/> else if (Android. OS. environment. externalstoragestate. equals (Android. OS. environment. mediaremoved) <br/> throw new system. exception ("No SD card"); <br/> android. content. res. typedarray A = CTX. obtainstyledattributes (resource. styleable. gallery); <br/> mgalleryitembackground =. getresourceid (resource. styleable. gallery_android_galleryitembackground, android. graphics. color. azure); <br/>. recycle (); <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (CTX, ex ); <br/>}< br/> Public override int count <br/>{< br/> Get <br/>{< br/> If (Files = NULL) <br/> return 0; <br/> return this. files. length; <br/>}< br/> Public override Java. lang. object getitem (INT position) <br/>{< br/> return position; <br/>}< br/> Public override long getitemid (INT position) <br/>{< br/> return position; <br/>}< br/> Public override view getview (INT position, view convertview, viewgroup parent) <br/>{< br/> try <br/> {<br/> imageview v = new imageview (this. CTX); <br/> bitmap BMP = bitmapfactory. decodefile (files [position]. path); <br/> v. setimagebitmap (BMP); <br/> v. setscaletype (imageview. scaletype. fitxy); <br/> v. layoutparameters = new gallery. layoutparams (width, height); <br/> v. setbackgroundresource (mgalleryitembackground); <br/> return V; <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this. CTX, ex); <br/> return NULL; <br/>}< br/>
Then we will add a small feature, that is, when you click an image, a small prompt will pop up, prompting you which image you are clicking. The toast object is used here. In fact, we have already used the toast object when we introduced the layout in the fourth article, but we have not specifically introduced it. Toast is an exclusive prompt small object for Android, it is easy to use but widely used. Basically, toast is a short small information. The information to be told is displayed in a view floating on the top layer. After toast is displayed, it will automatically disappear after several seconds, the most common application is to adjust the volume. When you click the volume adjustment button, you will see the volume that jumps out to indicate the toast object. After the adjustment, it will disappear.
Gallery mgallery = findviewbyid <gallery> (resource. id. gallery); <br/> mgallery. adapter = new imageadapter (this); <br/> mgallery. itemclick + = (sender, e) =>< br/>{< br/> toast. maketext (this, String. format ("the image {0} You clicked", E. position), toastlength. short ). show (); <br/>}; <br/>
Effect after running:
After the toast is displayed, it will disappear within a certain period of time. The third parameter of the toast. maketext method is a toastlength enumeration, with long or short as the separator.
Of course, you can also use the override toast object method to customize the toast display layout to display the toast object in a different way than the built-in system. For example, to display an image in toast, the method is as follows:
Gallery mgallery = findviewbyid <gallery> (resource. id. gallery); <br/> mgallery. adapter = new imageadapter (this); <br/> mgallery. itemclick + = (sender, e) =>< br/>{< br/> toast T = new toast (this ); <br/> imageview IMG = new imageview (this); <br/> IMG. setimageresource (Android. resource. drawable. icmenucamera); <br/> T. view = IMG; <br/> T. show (); <br/> // toast. maketext (this, String. format ("the image {0} You clicked", E. position), toastlength. short ). show (); <br/>}; <br/>
Finally, let's talk about bitmapfactory (located in Android. graphics namespace), which is an object provided by the android API. You can convert an image file to a bitmap object. The decodefile () used in the example () this method can convert image files in the mobile phone system into bitmap objects. In addition, bitmapfactory also provides other methods. For example, decoderesource can convert pre-stored image files to bitmap objects. The decodestream method can convert inputstream to bitmap objects.