Android Special 5--Learning Gallery Summary

Source: Internet
Author: User

Context
    1. This is a more complex concept, with context, handle, environment variables and so on multiple meanings
    2. Currently understood as: a pointer to the parent, such as button btn = New button (this), here is a context object, this code tells the BTN in which activity
What does a res\values\ folder do?
    • We can use system-defined attributes to decorate a view in a Java code in an XML file, such as android:layout_wid= "Wrap_content".
    • The Android:layout_wid above is a property, and we can define our own properties.
    • The first thing to do now is res\values\ create a new XML file of our own
    • The following android:galleryitembackground is a custom style that we use to modify the gallery background.

The Android:galleryitembackground here is an android with a custom property, so there is no format appendedHow do I use the format of this definition?

How to cycle through pictures

The principle of cyclic display of images

The loop shows something similar to a circular list, and the next node of the last node is the 1th node. Looping through images can also simulate this.

Perhaps the attentive reader will find something in the Imageadapter class that was implemented in the previous section. Right! Is the relationship between the position parameter and the GetCount method in the GetView method. The value of the position parameter is not likely to exceed the value returned by the GetCount method, that is, the range of position parameter values is 0 to GetCount ()-1.

If at this point the gallery component is displayed exactly to the last image, the position parameter value is exactly GetCount ()-1. So how do we get gallery to show the next image again? That means let the position parameter value increase by 1, yes! The return value of the GetCount () method is also increased by 1.

So here's another question: if the value of the position parameter increases infinitely, it means that the Resids array will continue to grow, which consumes the system's resources greatly. Think of this, you need to solve two problems: both position constantly increase, and let the resids array to save the image resource ID is limited, how to do it? The GetCount () method is a good solution, so the GetCount method can return a large number, for example, Integer.max_value. The position parameter value can then be increased as the image of the gallery component moves forward. Now that the Resids array has only 15 elements, if the value of position exceeds the bounds of the array, you want to continue looping through the elements in the array (that is, when the value of position is 15 o'clock, take the No. 0 element of the Resids array, which is 16 o'clock to take the 1th element), The simplest way is to take the remainder, the code is as follows:

Resids[position% resids.length]

The following two improvements have been made to the Imageadapter class in this section:

1. Make the GetCount method return a very large value. It is recommended to return Integer.max_value.

2. Loop through the GetView method to get the image resource ID in the Resids array by taking the remainder.

With the above two improvements, you can make the image list loop through the image as it moves to the right. Of course, this approach is essentially a pseudo-loop, which means that if you move the image to the value returned by the GetCount method, it will be displayed to the last image. But here the GetCount method returns the Integer.max_value, which is more than 2 billion, unless someone really wants to move the image to the No. 2 billion position, otherwise the gallery component looks like a component that loops through the image.

1  PackageCom.example.testgallery;2 3 ImportAndroid. R.integer;4 Importandroid.app.Activity;5 ImportAndroid.content.Context;6 ImportAndroid.content.res.TypedArray;7 ImportAndroid.os.Bundle;8 ImportAndroid.view.Menu;9 ImportAndroid.view.View;Ten ImportAndroid.view.ViewGroup; One ImportAndroid.widget.BaseAdapter; A ImportAndroid.widget.Gallery; - ImportAndroid.widget.ImageView; -  the  Public classMainactivityextendsActivity { -     PrivateGallery Gallery; -     Private int[] Imageids ={r.drawable.item1, r.drawable.item2, - r.drawable.item3, R.drawable.item4, R.DRAWABLE.ITEM5, + R.drawable.item6, R.drawable.item7, R.DRAWABLE.ITEM8, - R.DRAWABLE.ITEM9}; +  A @Override at     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Gallery =(Gallery) Findviewbyid (R.id.galley); -Gallery.setadapter (NewImageadapter ( This)); -     } in  - @Override to      Public BooleanOncreateoptionsmenu (Menu menu) { +         //inflate the menu; This adds items to the action bar if it is present. - getmenuinflater (). Inflate (R.menu.main, menu); the         return true; *     } $     Panax Notoginseng     Private classImageadapterextendsbaseadapter{ -         PrivateContext Mcontext; the         Private intMgalleryitembackground; +          PublicImageadapter (Context context) { AMcontext =context; theTypedArray Typearray =obtainstyledattributes (r.styleable.gallery); +Mgalleryitembackground = Typearray.getresourceid (r.styleable.gallery_android_galleryitembackground, 0); -         } $ @Override $          Public intGetCount () { -             returnInteger.max_value; -         } the @Override -          PublicObject GetItem (intposition) {Wuyi             returnposition; the         } - @Override Wu          Public LongGetitemid (intposition) { -             returnposition; About         } $ @Override -          PublicView GetView (intposition, View arg1, ViewGroup arg2) { -ImageView ImageView =NewImageView (mcontext); -Imageview.setimageresource (imageids[position%imageids.length]); A Imageview.setscaletype (ImageView.ScaleType.FIT_XY); +Imageview.setlayoutparams (NewGallery.layoutparams (310, 210)); the Imageview.setbackgroundresource (mgalleryitembackground); -             returnImageView; $         } the     } the}

Activity_main.xml

1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= ". Mainactivity ">Ten  One<Gallery AAndroid:id= "@+id/galley" -Android:layout_width= "Fill_parent" -android:layout_height= "Wrap_content"/> the  -</RelativeLayout>

Attrs.xml

1 <?xml version= "1.0" encoding= "Utf-8"?>2 <resources>3     < Declare-styleable name= "Gallery" >4         <attr name= "Android:galleryitembackground"/>5     </declare-styleable>6 </resources>

Android Special 5--Learning Gallery Summary

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.