Android control series album GalleryAdapter adapter entry control zoom animation entry

Source: Internet
Author: User

Objective:

1. Master how to build Gallery in Android
2. A preliminary understanding of the principles of Android adapters
3. implement simple control scaling Animation

Introduction:

1. Gallery is a built-in control of Android. It can inherit images and even other controls.
2. Gallery comes with the scroll playback image function. You can drag and drop the mouse on the simulator or on your mobile phone to verify the function.
3. Gallery requires an adapter to transmit data. If you are not familiar with the "adapter design mode", you can understand the adapter as a computer adapter of a vendor, as long as all models of the vendor's computer can use this adapter, that is, when designing a new model computer, we can also use this adapter, as long as the receiver can achieve how to accept the power, this adapter doesn't care which type of computer is using it. It only needs to provide power.
4. The interface SpinnerAdapter is a linear simple Adapter. You can think of it as a two-eye plug, so there must be a three-eye plug (Other adapters ), given that Gallery stores a set of images linearly, there is no parent-child relationship between them and there is only a sequence of order. Therefore, Android defines a SpinnerAdapter as an adapter for it. Combined with the example described in 3, you already have an adapter and a power supply (Gallery). Therefore, you don't need to worry about how to get the power, but you just need to care about how to use the power supply.
5. The SpinnerAdapter is an interface that is not implemented. Therefore, in this example, we use its implementation class: BaseAdapter and expand it.

This example describes how to use the simplest features of Gallery, but it is not difficult to modify or beautify Gallery on this basis, or even convert it into 3D:

The figure contains several images. The selected image will be gradually enlarged.
XML layout:Copy codeThe Code is as follows: <? 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 = "200px"
>
</Gallery>
</LinearLayout>

Background code:Copy codeThe Code is as follows: @ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Find the album by ID
Gallery gallery = (Gallery) this. findViewById (R. id. gallery );
// Initialize the custom image Adapter
ImageAdapter adp = new ImageAdapter (this );
// Bind the adapter
Gallery. setAdapter (adp );
// Listen to the selected image event
Gallery. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3 ){
// TODO Auto-generated method stub
// Initialize the "zoom Animation" class instance, which means to zoom in from 1 to 1.5 times, and the center to zoom in is the center of the image
ScaleAnimation animation = new ScaleAnimation (1, 1.5f, 1, 1.5f,
Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
// It takes 1 second from 1 to 1.5 times
Animation. setDuration (1000 );
// Start executing the animation
Arg1.startAnimation (animation );
}
});
}
// Custom Adapter
Public class ImageAdapter extends BaseAdapter {
// This value is only used to pass the Activity
Private Context context;
Public ImageAdapter (Context context ){
This. context = context;
}
// Array of image IDs. Each ID can be called by ImageView to display images.
Private int [] imageArray = {
R. drawable. p01,
R. drawable. p03,
R. drawable. p04,
R. drawable. p05,
R. drawable. p07,
R. drawable. p09,
R. drawable. p21,
R. drawable. p23,
R. drawable. p33
};
@ Override
Public int getCount (){
// TODO Auto-generated method stub
// Returns the total number of arrays.
Return imageArray. length;
}
@ Override
Public Object getItem (int arg0 ){
// TODO Auto-generated method stub
Return arg0;
}
@ Override
Public long getItemId (int arg0 ){
// TODO Auto-generated method stub
Return arg0;
}
@ Override
Public View getView (int arg0, View arg1, ViewGroup arg2 ){
// TODO Auto-generated method stub
// Implement the core of the Adapter, that is, the control that is returned Based on the given data.
// You need to return the ImageView, because you want to implement the album
ImageView view = new ImageView (this. context );
// Obtain the image ID of the specified index
Int id = imageArray [arg0];
// Tell the ImageView ID to find the image.
View. setImageResource (id );
// Layout the ImageView
View. setLayoutParams (new Gallery. LayoutParams (120,120 ));
// Set the Image view's pull type, which is centered here. You can try different types
View. setScaleType (ImageView. ScaleType. FIT_CENTER );
Return view;
}
}

Summary:
This article describes how to use Gallery to create a simple album, How to Implement Interactive Animation with user clicks, and how the adapter works. On this basis, you can modify and implement your own album, which will increase in color when embedded in any program.

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.