Android: Use gallery and Imageswitch to make a picture browser that lets you swipe left and right

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>In order to make the picture browser left and right infinite loop sliding we want to customize Gallery's adapter</p></p><p><p>If you want to customize adapter first understand these methods</p></p><p><p></p></p><pre code_snippet_id="1588231" snippet_file_name="blog_20160225_1_7537669" name="code" class="java"><pre code_snippet_id="1588231" snippet_file_name="blog_20160225_1_7537669" name="code" class="java">@Overridepublic int GetCount () {//TODO auto-generated method Stubreturn 0;} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn null;} @Overridepublic long getitemid (int position) {//TODO auto-generated method Stubreturn 0;} @Overridepublic View GetView (int position, view convertview, viewgroup parent) {//TODO auto-generated method Stubreturn N ull;}</pre></pre><br>Where the GetCount method is to return the number of data sources<p><p></p></p><p><p>The GetItem method returns an Object object that returns the data ID position in the current container.</p></p><p><p>Getitemid returns the data ID in the current container</p></p><p><p>GetView get the view currently being displayed</p></p><p><p><strong>If we want to achieve the left and right loop slide first we want to return the number of data sources to the maximum and then the ID of all data to the original data source of the number of the last set gallery the initial position in the 0-maximum value of the middle can</strong></p></p><p><p><strong>That's how the adapter Changed.</strong></p></p><p><p><strong></strong></p></p><pre code_snippet_id="1588231" snippet_file_name="blog_20160225_2_2270772" name="code" class="java">Package Com.example.imageswitcher;import Android.content.context;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.gallery;import Android.widget.imageview;import Android.widget.imageview.scaletype;public class Myadapter extends BaseAdapter{ private int id_image[];p rivate context contex;public myadapter (context contex,int Id_image[]) {this.contex=contex; this.id_image=id_image;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn integer.max_value;} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn id_image[position%id_ image.length];} @Overridepublic long getitemid (int position) {//TODO auto-generated method Stubreturn position%id_image.length;} @Overridepublic View GetView (int position, view convertview, viewgroup parent) {//TODO auto-generated method Stubimagevie W imageview=new imageView (contex); imageview.setbackgroundresource (id_image[position%id_image.length]); ImageView. setlayoutparams (new gallery.layoutparams); imageview.setscaletype (scaletype.fit_xy); return imageView;}} </pre><br>Mainactivity<p><p></p></p><p><p></p></p><pre code_snippet_id="1588231" snippet_file_name="blog_20160225_3_1016253" name="code" class="java">Package Com.example.imageswitcher;import Android.os.bundle;import Android.app.activity;import Android.view.layoutinflater.factory;import Android.view.menu;import Android.view.view;import android.view.Window; Import Android.view.animation.animationutils;import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.gallery;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.imageview.scaletype;import Android.widget.viewswitcher.viewfactory;public class Mainactivity extends Activity implements Onitemselectedlistener , viewfactory{private imageswitcher imageswitcher;private Gallery gallery;private int id_image[] = {R.drawable.beauty1, r.drawable.beauty2,r.drawable.beauty3, r.drawable.beauty4, r.drawable.beauty5,r.drawable.beauty6, r.drawable.beauty7, r.drawable.beauty8,r.drawable.beauty9};p rivate myadapter myadapter; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); requestwindowfeature (window.feature_no_title); setcontentview (r.layout.activity_main); gallery = (gallery) Findviewbyid (r.id.id_gallery); imageswitcher = (imageswitcher) Findviewbyid (r.id.id_imageswitcher); myAdapter=new Myadapter (this, id_image); imageswitcher.setfactory (the); gallery.setonitemselectedlistener (this);// Sets the fade effect imageswitcher.setinanimation (animationutils.loadanimation (this, android. r.anim.fade_in)); imageswitcher.setoutanimation (animationutils.loadanimation (this, android. r.anim.fade_out)); Gallery.setadapter (myadapter);//do not forget to set Gallery initial position for the middle can gallery.setselection (id_ image.length*100);} @Overridepublic void onitemselected (adapterview<?> parent, view view, int Position,long id) {//TODO auto-generated Method Stubimageswitcher.setbackgroundresource (id_image[position%id_image.length]);} @Overridepublic void onnothingselected (adapterview<?> parent) {//TODO auto-generated method stub}@ Overridepublic View Makeview () {//TODO auto-generated method Stubimageview IMage=new ImageView (this); image.setscaletype (scaletype.fit_center); return image;}} </pre><br><br><p><p></p></p><p><p>Android: Use gallery and Imageswitch to make a picture browser that lets you swipe left and right</p></p></span>

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.