Android--gallery Picture Drag Effect

Source: Internet
Author: User

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

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.