Android Hundred Day Program: GridView Implementation Album effect

Source: Internet
Author: User

This chapter uses the GridView control to make an album effect.

The picture works as follows:


In response to the Click event, the prompt is the current chapter of the picture, from left to right, from top to bottom.


Click on the first picture, showing 1.

Steps:

A new project, and then copy the picture resources to the res/drawable/folder, if not drawable This folder does not matter, you can create this folder directly, and then refresh in the Eclipse project, will be displayed in the project

Two then in the layout of the interface XML file Activity_main.xml (of course, you can also like to name), enter the following code:

<?xml version= "1.0" encoding= "Utf-8"? ><gridview xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/gridview "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:columnwidth=" 90DP "    android:numcolumns=" Auto_fit "    android:verticalspacing=" 10DP "    android:horizontalspacing=" 10DP "    android:stretchmode=" columnWidth "    android:gravity=" center "/ >
Nothing special, is to set the line width, and then automatically row number numcolumns= "Auto_fit", this seems to be Android automatic arrangement.

Three activity_main.xml corresponding Java file Mainactivity.java input logic code, mainly modify OnCreate This function can be, the other default can be, no tube:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); GridView GridView = (GridView) Findviewbyid (R.id.gridview); Gridview.setadapter (new Imageadapter (this)); Gridview.setonitemclicklistener (New Onitemclicklistener () {public void Onitemclick (adapterview<?> parent, View V,int position, long id) {Toast.maketext (Mainactivity.this, "" + (position+1), Toast.length_short). Show ();}});

The main is set up to listen to the Itemclicklistener of the GridView object is OK. Toast can be understood as a lightweight prompt dialog box, which is a small dialog box showing 1, which shows that about 1 seconds will automatically disappear.

Tips:

Don't know if the object belongs to those header files? For example, what kind of file library does Onitemclicklistener belong to?

This is sometimes a headache problem, especially when using VC to write C + +, need to do their own to check, headache is, found this function, or class, incredibly no hint need include what header file, but to Java, it seems that is not a thing, a shortcut to get it: Ctrl + Shift+o, then it will automatically include all the required libraries.


After four, create a Java class, put it in a directory of Mainactivity.java, and then add the code:

Package Su.gridview.hellogridview;import Android.content.context;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.gridview;import Android.widget.imageview;public class Imageadapter extends Baseadapter {private Context mcontext;public imageadapter ( Context c) {mcontext = C;} public int GetCount () {return mthumbids.length;} Public Object getItem (int position) {return null;} public long getitemid (int position) {return 0;} Create a new ImageView for each item referenced by the Adapterpublic view GetView (int position, view Convertview, VIEWG Roup parent) {ImageView imageview;if (Convertview = = null) {//If it ' s not recycled, initialize some//Attributesimagevie w = new ImageView (mcontext); Imageview.setlayoutparams (new Gridview.layoutparams); Imageview.setscaletype ( ImageView.ScaleType.CENTER_CROP); imageview.setpadding (8, 8, 8, 8);} else {ImageView = (ImageView) Convertview;} Imageview.setimageresource (Mthumbids[position]); return ImageView;} References to our imagesprivate integer[] Mthumbids = {r.drawable.x0, r.drawable.x1,r.drawable.x2, r.drawable.x3, R.dr Awable.x4, r.drawable.x5,r.drawable.x10, r.drawable.x9, r.drawable.x8, r.drawable.x7,r.drawable.x6, R.drawable.x5, R.drawable.x11, r.drawable.x20,r.drawable.x13, r.drawable.x14, r.drawable.x15, r.drawable.x16,r.drawable.x17, r.drawable.x18, r.drawable.x19, r.drawable.x20};}
In this case, the function is useless because the Baseadapter class must be replicated.

The two functions, GetCount and GetView, are mainly used. According to his style implementation can be, the specific low-level work is automatically called by the adapter class, call Getcount,adapter know how many resource objects need to display, call GetView can display the picture resources, The incoming convertview is to speed up the reuse of the previously used view.

This is my understanding, more detailed principle still need to have a short reference to the source code bar.

Reference: http://developer.android.com/guide/topics/ui/layout/gridview.html

The second chapter completes, feels the Android to write the program is really is too convenient, no wonder the Android big line its way, compares a DirectX These API usage, is simply the fool type. In particular reminds me of the use of direct2d, Microsoft really silly, what a year, incredibly also out of direct2d so difficult to use the API. Oh, don't say I black Microsoft, recently heard VC incredibly to support Android, feel Microsoft is low noble head, support the competitor's things, but also want to catch up with the pace of Android, not sacrificing hue ah. The strategy needs to be low-headed.


Android Hundred Day Program: GridView Implementation Album effect

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.