How to Use the Android Gallery Control

Source: Internet
Author: User

The Android mobile operating system has many controls that can help us easily implement some functions. Here we will introduce you in detail how to use the Android Gallery control, hoping to help you gain an in-depth understanding of the use of the control.

Android Gallery Control: An Image Browsing Control

Layout part:

 
 
  1. < Gallery xmlns:android=
    "http://schemas.android.com/apk/res/android"   
  2. android:id="@+id/gallery" 
  3. android:layout_width="fill_parent" 
  4. android:layout_height="fill_parent"/> 

Android Gallery control code:

Code of the main class:

 
 
  1. Gallery g = (Gallery) findViewById (R. id. gallery );
    // Define the Gallery Control
  2. G. setAdapter (new ImageAdapter (this ));
    // Set the image source of the Gallery Control
  3. G. setOnItemClickListener (new OnItemClickListener (){
    // Click to listen to events
  4. Public void onItemClick (AdapterView parent, View v,
    Int position, long id) {// Click Event
  5. Toast. makeText (galleryMain. this, "" + position,
    Toast. LENGTH_SHORT). show (); // Toast
  6. }
  7. });

ImageAdapter class

 
 
  1. Private Context mContext;
  2. // Define Context
  3. Private Integer [] mImageIds = {
  4. // Define an integer array, that is, the image source.
  5. R. drawable. sample_1,
  6. R. drawable. sample_2,
  7. R. drawable. sample_3,
  8. R. drawable. sample_4,
  9. R. drawable. sample_5,
  10. R. drawable. sample_6,
  11. R. drawable. sample_7
  12. };
  13. Public ImageAdapter (Context c ){
  14. // Declare ImageAdapter
  15. MContext = c;
  16. }
  17. Public int getCount () {// obtain the number of images
  18. Return mImageIds. length;
  19. }
  20. Public Object getItem (int position ){
  21. // Obtain the image location in the library
  22. Return position;
  23. }
  24. Public long getItemId (int position ){
  25. // Obtain the image location in the library
  26. Return position;
  27. }
  28. Public View getView (int position, View convertView,
    ViewGroup parent ){
  29. ImageView I = new ImageView (mContext );
  30. I. setImageResource (mImageIds [position]);
  31. // Set resources for ImageView
  32. I. setLayoutParams (new Gallery. LayoutParams (200,200 ));
  33. // Set the layout image to 200x200
  34. I. setScaleType (ImageView. ScaleType. FIT_XY );
  35. // Set the proportion type
  36. Return I;
  37. }

The usage of the Android Gallery control is described here.

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.