Android deep examples-simple finger drag image, image slide to show application Gallery, BaseAdapter, and ImageView usage

Source: Internet
Author: User

Preface
You can drag images by sliding your fingers on the screen on any mobile phone. In fact, this is very simple in Android. I will explain it to you in detail below.

Ideas
First, we need the Gallery object, which is also known as the Gallery object. Everyone knows about the Gallery. In real life, the Gallery stores the specific paintings painted by painters, what else do I need? You also need a specific image, which is actually an ImageView object. How can I fill the painted image in the gallery? Here we need a filler, that is, BaseAdapter.

Steps

I. layout file writing

1.1: Layout file main. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Gallery
Android: id = "@ + id/gallery"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>
</LinearLayout>

Ii. Code File writing

2. 1: MainActivity. java

Package com. menglin. gallery;

Import android. app. Activity;
Import android. content. Context;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageView;

Public class MainActivity extends Activity
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
// Load the layout file main. xml
SetContentView (R. layout. main );
// Obtain the Gallery object through the findViewById () method
Gallery gallery = (Gallery) findViewById (R. id. gallery );
// Add an ImageAdapter and set it to Gallery
Gallery. setAdapter (new ImageAdapter (this ));
}
 
Public class ImageAdapter extends BaseAdapter
{
Private Context context;
// Use the System icon image as the Image Library Source
Private int [] imageids =
{
Android. R. drawable. btn_minus,
Android. R. drawable. btn_radio,
Android. R. drawable. ic_lock_idle_low_battery,
Android. R. drawable. btn_radio,
Android. R. drawable. btn_dialog
};

// Constructor. This constructor has only one parameter, that is, the Context of the number to be stored.
Public ImageAdapter (Context c)
{
This. context = c;
}

// Obtain the total number of defined Images
Public int getCount ()
{
Return imageids. length;
}

// Obtain the array of images in the current container
Public Object getItem (int position)
{
Return position;
}

// Obtain the array ID of the image in the current container
Public long getItemId (int position)
{
Return position;
}

// Obtain the Image view to be displayed and input an array ID to read the image
Public View getView (int position, View convertView, ViewGroup parent)
{
// Create an ImageView object
ImageView imageview = new ImageView (context );
// Set the image to the ImageView object
Imageview. setImageResource (imageids [position]);
// Reset the image width and height
Imageview. setScaleType (ImageView. ScaleType. FIT_XY );
// Reset the width and height of Layout.
Imageview. setLayoutParams (new Gallery. LayoutParams (120,120 ));
Return imageview;
}

// Return the size of the view by getScale () based on the displacement from the center.

Public float getScale (boolean focused, int offset)
{
Return Math. max (0, 1.0f/(float) Math. pow (2, Math. abs (offset )));
}
}
}

The running effect is as follows:

We drag from right to left with our fingers

  

  

  

  

 

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.