Android image dragging Gallery

Source: Internet
Author: User

1. Gallery allows you to drag an image to achieve a dazzling effect, as shown in the following figure. You can drag the image to the left and right:

2. layout code
<?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/Gallery"    android:layout_width="fill_parent"    android:layout_height="wrap_content"></Gallery>
3. Use a class inherited from baseadapter to act as a container

 

Public class imageadapter extends baseadapter {private context mcontext; private integer [] mimageids = {R. drawable. img1, R. drawable. img2, R. drawable. img3, R. drawable. img4, R. drawable. img5, R. drawable. img6, R. drawable. img7, R. drawable. img8}; Public imageadapter (context c) {This. mcontext = C ;}@ override public int getcount () {return mimageids. length; // obtain the number of images} @ override public object getitem (INT position) {return position; // obtain the position of the image in the library} @ override public long getitemid (INT position) {return position; // obtain the position of the image in the library} @ override public view getview (INT position, view convertview, viewgroup parent) {log. I ("tag", Position + ""); imageview = new imageview (mcontext); imageview. setimageresource (mimageids [position]); imageview. setlayoutparams (new gallery. layoutparams (100,100); // sets the image size to imageview. setscaletype (imageview. scaletype. fit_center); // set the display ratio type return imageview ;}}

 

4. Java code used:Public class test_gallery extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); gallery G = (Gallery) This. findviewbyid (R. id. gallery); G. setadapter (New imageadapter (this); G. setbackgroundresource (R. drawable. bg0); G. setonitemclicklistener (New onitemclickliste NER () {@ override public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {toast. maketext (test_gallery.this, "selected" + (arg2 + 1) + "image", toast. length_short ). show ();}});}}
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.