1. xml file
<?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" >
</Gallery>
2, Imageadapter
public class Imageadapter extends Baseadapter {
Define Context
Private context context;
Define an integer array, which is the picture source
Private integer[] Imageids = {r.drawable.button1, r.drawable.button2,
R.drawable.button3, r.drawable.button31};
Statement Imageadapter
Public Imageadapter (Context c) {
context = C;
}
Get the number of pictures
@Override
public int GetCount () {
return imageids.length;
}
Get the position of the picture in the library
@Override
Public Object getItem (int position) {
return position;
}
Get the position of the picture in the library
@Override
public long getitemid (int position) {
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
ImageView ImageView = new ImageView (context);
Setting up resources for ImageView
Imageview.setimageresource (Imageids[position]);
Set up a layout picture to display 120*120
Imageview.setlayoutparams (New Gallery.layoutparams (120, 120));
Set the display scale type
Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}
3, Galleryactivity
public class Galleryactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_gallery);
Get Gallery Object
Gallery g = (Gallery) Findviewbyid (R.ID.GALLERY01);
G.setadapter (New Imageadapter (this));
Set Gallery Background
G.setbackgroundresource (R.DRAWABLE.BUTTON31);
Set Gallery Background color
G.setbackgroundcolor (Color.gray);
G.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> parent, View V,
int position, long ID) {
Toast.maketext (Galleryactivity.this,
"You have selected" + (position + 1) + "number picture", Toast.length_short)
. Show ();
}
});
}
}
Android--gallery Picture Drag Effect