Android learning-electronic album

Source: Internet
Author: User

The function is cool and Android cannot be vague. You can use the gallery class to move images with your fingers on your iPhone.

Today I made a small e-album:

Suppose you have created a new project.

First, store the prepared images in the drawable folder and create a new interface:

Public interface imageresource {<br/> // Save image resources with an integer array <br/> integer [] dimageid = {<br/> r. drawable. sample_0, <br/> r. drawable. sample_1, <br/> r. drawable. sample_2, <br/> r. drawable. sample_3, <br/> r. drawable. sample_4, <br/> r. drawable. sample_5, <br/> r. drawable. sample_6, <br/> r. drawable. sample_7, <br/>}; <br/>}< br/>

 

 

This interface contains an int-type array that saves the image under drawable, so that it can be modified later.

 

To use gallery to implement this function, you must first have a container to store the image that gallery wants to display. I use an imageadapter inherited from baseadapter. This electronic album consists of two parts: the previous part is the list of images that users can drag, And the next part is the larger display of the images selected above. Figure:

 

The imageview in charge of the previous part is imageall, And the imageone is in charge of the next part. The Code is as follows:

 

Import android. app. activity; <br/> Import android. content. context; <br/> Import android. content. res. typedarray; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. baseadapter; <br/> Import android. widget. imageview; <br/> Import android. widget. gallery; </P> <p> public class hellogallery extends activity {<br/> // This imageview is used to display a single image <br/> private imageview imageone; <br/> // This imageview is used to display all images <br/> private imageview imageall; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> imageone = (imageview) findviewbyid (R. id. imageview); </P> <p> // creates a gallery class, this is the key to achieving the drag effect <br/> // gallery is a view used to horizontally scroll the Display object <br/> gallery = (Gallery) findviewbyid (R. id. gallery); </P> <p> // imageadapter is the object to be displayed by gallery. <br/> Gallery. setadapter (New imageadapter (this )); </P> <p >}< br/> // implement the imageresource interface to obtain image resources created by myself. <br/> class imageadapter extends baseadapter implements imageresource {<br/>/ /background resources of images in each gallery <br/> private int galleryitembackground; <br/> private context; </P> <p> Public imageadapter (context) {<br/> This. context = context; <br/> // The implemented function is the background frame of each image in the upper part. <br/> // the corresponding XML file is ATTR. XML <br/> typedarray A = obtainstyledattributes (R. styleable. gallery1); <br/> galleryitembackground =. getresourceid (<br/> r. styleable. gallery1_android_galleryitembackground, 0); <br/>. recycle (); <br/>}</P> <p> Public int getcount () {<br/> return dimageid. length; <br/>}</P> <p> Public object getitem (INT position) {<br/> return position; <br/>}</P> <p> // This method is used to obtain the subscript of the image displayed in front of the user. <br/> Public long getitemid (INT position) {<br/> // set the image indexed to imageone to display imageone. setimageresource (imageadapter. dimageid [position]); <br/> imageone. setscaletype (imageview. scaletype. fit_center); <br/> return position; <br/>}</P> <p> // The imageview returned by this method is the image that achieves the drag effect. <br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> imageall = new imageview (context); <br/> // set image resources <br/> imageall. setimageresource (dimageid [position]); <br/> // set the imageall view to 120 × 120 <br/> imageall. setlayoutparams (new gallery. layoutparams (120,120); <br/> // sets the ratio of the image to the view. fit_xy indicates that the image is filled with X and Y axes. <br/> imageall. setscaletype (imageview. scaletype. fit_xy); <br/> // set the background resource of each item in imageall <br/> imageall. setbackgroundresource (galleryitembackground); <br/> return imageall; <br/>}< br/>

 

The layout file is as follows:

Mail. xml:

<? 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/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> <gallery Android: Id = "@ + ID/Gallery" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> <br/> </gallery> </P> <p> <imageview Android: id = "@ + ID/imageview" <br/> Android: layout_height = "fill_parent" <br/> Android: layout_width = "fill_parent"> <br/> </imageview> <br/> </linearlayout> <br/>

The ATTR. xml mentioned above is as follows:

 

ATTR. xml:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <declare-styleable name = "gallery1"> <br/> <ATTR name = "Android: galleryitembackground "/> <br/> </declare-styleable> <br/> <declare-styleable name =" labelview "> <br/> <ATTR name =" text "Format = "string"/> <br/> <ATTR name = "textcolor" format = "color"/> <br/> <ATTR name = "textsize" format = "dimension" /> <br/> </declare-styleable> <br/> </resources> <br/>

 

Summary:

A simple Android version of e-album is coming out. If you have any mistakes, please point them out. I am very willing to share my knowledge about Android with you. I am posting my email. Thank you!

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.