Android development: Direct finger-dragging

Source: Internet
Author: User

I. Basic Knowledge:

To achieve this effect, a container is required to store the images displayed by Gallrey. Here, these images are loaded using a derived class inherited from the BaseAdapter class.

We need to listen to the setOnItemClickListener event to determine which image the user selected currently.

First, you need to store the indexes of all the images to be displayed in an int array, and then set the image resources to be displayed by using the setImageResource method.

The ImageView of each image is displayed on the screen.

 

Ii. Code display:

1. "main. xml"

[Html]

<? Xml version = "1.0" encoding = "UTF-8"?>

<Gallery

Xmlns: android = "http://schemas.android.com/apk/res/android"

Android: id = "@ + id/Gallery01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

/>

<? Xml version = "1.0" encoding = "UTF-8"?>

<Gallery

Xmlns: android = "http://schemas.android.com/apk/res/android"

Android: id = "@ + id/Gallery01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

/>

 

2. "Activity01.java"

[Java] view plaincopyprint? Package com. yarin. android. XX;

 

Import android. app. Activity;

Import android. OS. Bundle;

Import android. view. View;

Import android. widget. AdapterView;

Import android. widget. Gallery;

Import android. widget. Toast;

Import android. widget. AdapterView. OnItemClickListener;

 

Public class Activity01 extends Activity

{

/** Called when the activity is first created .*/

@ Override

Public void onCreate (Bundle savedInstanceState)

{

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

 

// Obtain the Gallery object

Gallery g = (Gallery) findViewById (R. id. Gallery01 );

 

// Add the ImageAdapter to the Gallery object

G. setAdapter (new ImageAdapter (this ));

 

// Set the Gallery background

G. setBackgroundResource (R. drawable. bg0 );

// Set Gallery event listening

G. setOnItemClickListener (new OnItemClickListener (){

Public void onItemClick (AdapterView <?> Parent, View v, int position, long id)

{

Toast. makeText (Activity01.this, "you have selected" + (position + 1) + "image ",

Toast. LENGTH_SHORT). show ();

}

});

}

}

Package com. yarin. android. XX;

Import android. app. Activity;

Import android. OS. Bundle;

Import android. view. View;

Import android. widget. AdapterView;

Import android. widget. Gallery;

Import android. widget. Toast;

Import android. widget. AdapterView. OnItemClickListener;

Public class Activity01 extends Activity

{

/** Called when the activity is first created .*/

@ Override

Public void onCreate (Bundle savedInstanceState)

{

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

// Obtain the Gallery object

Gallery g = (Gallery) findViewById (R. id. Gallery01 );

// Add the ImageAdapter to the Gallery object

G. setAdapter (new ImageAdapter (this ));

// Set the Gallery background

G. setBackgroundResource (R. drawable. bg0 );

// Set Gallery event listening

G. setOnItemClickListener (new OnItemClickListener (){

Public void onItemClick (AdapterView <?> Parent, View v, int position, long id)

{

Toast. makeText (Activity01.this, "you have selected" + (position + 1) + "image ",

Toast. LENGTH_SHORT). show ();

}

});

}

}

 

3. "ImageAdapter. java"

[Java]

Package com. yarin. android. XX;

 

Import android. content. Context;

Import android. view. View;

Import android. view. ViewGroup;

Import android. widget. BaseAdapter;

Import android. widget. Gallery;

Import android. widget. ImageView;

 

Public class ImageAdapter extends BaseAdapter

{

// Define Context

Private Context mContext;

// Define an integer array, that is, the image source.

Private Integer [] mImageIds =

{

R. drawable. img1,

R. drawable. img2,

R. drawable. img3,

R. drawable. img4,

R. drawable. img5,

R. drawable. img6,

R. drawable. img7,

R. drawable. img8,

};

 

// Declare ImageAdapter

Public ImageAdapter (Context c)

{

MContext = c;

}

 

// Obtain the number of images

Public int getCount ()

{

Return mImageIds. length;

}

 

// Obtain the image location in the library

Public Object getItem (int position)

{

Return position;

}

 

// Obtain the image ID

Public long getItemId (int position)

{

Return position;

}

 

Public View getView (int position, View convertView, ViewGroup parent)

{

ImageView imageview = new ImageView (mContext );

 

// Set resources for ImageView

Imageview. setImageResource (mImageIds [position]);

// Set the layout image to 120x120

Imageview. setLayoutParams (new Gallery. LayoutParams (120,120 ));

// Set the display proportion type

Imageview. setScaleType (ImageView. ScaleType. FIT_CENTER );

Return imageview;

}

}

Package com. yarin. android. XX;

Import android. content. Context;

Import android. view. View;

Import android. view. ViewGroup;

Import android. widget. BaseAdapter;

Import android. widget. Gallery;

Import android. widget. ImageView;

Public class ImageAdapter extends BaseAdapter

{

// Define Context

Private Context mContext;

// Define an integer array, that is, the image source.

Private Integer [] mImageIds =

{

R. drawable. img1,

R. drawable. img2,

R. drawable. img3,

R. drawable. img4,

R. drawable. img5,

R. drawable. img6,

R. drawable. img7,

R. drawable. img8,

};

// Declare ImageAdapter

Public ImageAdapter (Context c)

{

MContext = c;

}

// Obtain the number of images

Public int getCount ()

{

Return mImageIds. length;

}

// Obtain the image location in the library

Public Object getItem (int position)

{

Return position;

}

// Obtain the image ID

Public long getItemId (int position)

{

Return position;

}

Public View getView (int position, View convertView, ViewGroup parent)

{

ImageView imageview = new ImageView (mContext );

// Set resources for ImageView

Imageview. setImageResource (mImageIds [position]);

// Set the layout image to 120x120

Imageview. setLayoutParams (new Gallery. LayoutParams (120,120 ));

// Set the display proportion type

Imageview. setScaleType (ImageView. ScaleType. FIT_CENTER );

Return imageview;

}

}

 

 

Iii. effect display:

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.