Android Implementation Picture Browser sample _android

Source: Internet
Author: User
Tags event listener

This article described as a basic version of the Android image browser code, is a copy of the original Google implementation, the code to achieve the main implementation process and methods, the specific improvement also need to add their own, the code has many comments, can help beginners quickly understand the code, using a part of the image resources.

The main functional code is as follows:

Package com.android.coding;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.BaseAdapter;
Import Android.widget.Gallery;
Import Android.widget.Gallery.LayoutParams;
Import Android.widget.ViewSwitcher.ViewFactory;
Import Android.widget.ImageSwitcher;
Import Android.widget.ImageView; public class Viewpicturesactivity extends activity {imageswitcher imageswitcher;//Declare Imageswitcher object, picture display area Gallery gal       Lery;      Declares the Gallery object, the Picture list index int imageposition; Tag Image array subscript for loop display//Declaration picture Integer Array Private int[] images = {R.drawable.image1,r.drawable.image2, R.drawable.image3,r.drawa Ble.image4, R.drawable.image5,r.drawable.image6, R.drawable.image7,r.drawable.image8, R.drawable.image9, R.drawable.image10, R.drawable.image11,r.drawable.image12, r.drawable.image13,r.drawable.iMage14, R.drawable.image15,r.drawable.image16, r.drawable.image17};
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Gets Imageswitcher Object imageswitcher = (imageswitcher) Findviewbyid (R.id.switcher) through the ID of the control;
    Set a custom picture to display the factory class Imageswitcher.setfactory (new Myviewfactory (this));
    Gets Gallery Object gallery = (gallery) Findviewbyid (r.id.gallery) through the ID of the control; 
    Set the custom picture adapter Gallery.setadapter (new Imageadapter (this)); Implements the selected event listener Gallery.setonitemselectedlistener (new Onitemselectedlistener () {@Override public void Onite Mselected (adapterview<?> Parent, view view, int position, long ID) {//Loop through the remainder to show the picture imageswitcher.setimageres  
  Ource (Images[position%images.length]); @Override public void onnothingselected (adapterview<?> parent) {//TODO auto-generated method stub}}    
  ); //Custom picture adapter, inheriting Baseadapter class ImageadaPter extends baseadapter{private context;//defines a construction method that contexts//parameters are contexts public imageadapter
 This.context = context;
 ///Get the size of the picture @Override public int GetCount () {//set to the maximum number of integers return integer.max_value;
 ///Get the object of the specified picture @Override public object GetItem (int position) {return null;
 The ID of the object that gets the specified picture @Override public long getitemid (int position) {return 0; ///Display icon list @Override public View getview (int position, View Convertview, ViewGroup parent) {ImageView IV = new IMAG Eview (context);  Create a ImageView object Iv.setimageresource (Images[position%images.length]); Sets the loop to display the picture iv.setadjustviewbounds (true); Picture automatically adjusts display//Set picture width and height iv.setlayoutparams (new Gallery.layoutparams (LAYOUTPARAMS.WRAP_CONTENT,LAYOUTPARAMS.WRAP_
  CONTENT)); return IV; Return ImageView Object}//Custom picture Display factory class, Inherit Viewfactory class Myviewfactory implements viewfactory{private context C Ontext;
  Constructs a method that defines a context//parameter as a context-public Myviewfactory {This.context = context; ///Display icon Area @Override public View Makeview () {ImageView IV = new ImageView (context);//Create ImageView Object Iv.setscale Type (ImageView.ScaleType.FIT_CENTER); The picture automatically centers//sets the width and height of the picture iv.setlayoutparams (New Imageswitcher.layoutparams (Layoutparams.fill_parent,
  Layoutparams.fill_parent)); return IV;
 Return ImageView Object}}}

This article is only the main functional code section, the reader can further improve it. The image Viewer can also expand a number of practical Android image manipulation features, which are a skill that should be handled as a novice Android application developer.

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.