Learning Objectives:
1. How to establish gallery in Android
2, a preliminary understanding of the principle of Android adapter
3, the implementation of simple control scaling animation
Introduction:
1, gallery is a built-in Android control, it can inherit a number of pictures or even other controls
2, gallery with the scrolling function, this function you can drag the mouse through the simulator or on the phone to drag and drop verification
3, gallery need adapters to transfer data, if you are not familiar with the "adapter design mode", you can understand the adapter as a manufacturer of the computer adapter, as long as the manufacturer of all models of computers can use the adapter, that is, the design of new models of computers, we can also use this adapter, As long as you realize how to accept the power supply on the receiving end, this adapter doesn't care what type of computer is using it, it simply provides power.
4, interface Spinneradapter is a linear simple adapter, you can understand it as a two-eye plug, so there must be three eyes plug (other adapters), in view of the gallery stored a group of pictures are linear, there is no parent-child relationship, only in order, So Android defines spinneradapter for it as an adapter. With the example described in 3, you already have an adapter and a power supply (Gallery), so you don't need to care how to get the power, just care about how to use it.
5, Spinneradapter is an interface that has not been implemented, so in this case we use one of its implementation classes: Baseadapter, and expand.
This example describes how to use the simplest features of gallery, but I hope you can modify or beautify on this basis, and even make it a good result is not difficult:
The picture contains several pictures, and the selected pictures will be enlarged slowly.
XML layout:
Copy Code code 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 Code code as follows:
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Find albums by ID
Gallery Gallery = (Gallery) This.findviewbyid (r.id.gallery);
Initialize a custom picture adapter
Imageadapter ADP = new Imageadapter (this);
Binding adapters
Gallery.setadapter (ADP);
Listening for picture selection events
Gallery.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,
Long Arg3) {
TODO auto-generated Method Stub
Initializes an instance of the scale animation class that is magnified to 1.5 times times from 1 time times, and the center point of magnification is the center point of the picture
scaleanimation animation = new Scaleanimation (1, 1.5f, 1, 1.5f,
animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);
From 1 time times to 1.5 times times it takes 1 seconds.
Animation.setduration (1000);
Start animation
Arg1.startanimation (animation);
}
});
}
Custom Adapters
public class Imageadapter extends baseadapter{
This value is only for passing activity
private context;
Public Imageadapter {
This.context = context;
}
An array of picture IDs, each of which can be called by ImageView to display pictures
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 adapter, that is, what controls are 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);
Gets the ID of the picture for the specified index
int id = imagearray[arg0];
Tell ImageView the ID and it will find the picture
View.setimageresource (ID);
Lay out the ImageView
View.setlayoutparams (New Gallery.layoutparams (120,120));
Set the pull type for the ImageView, where centered, 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 photo album, and realize the interactive animation with the user click, and introduced the principle of the adapter. You can modify on this basis, to achieve your own albums, embedded in any program will be a lot of color.