Android implements the image browser example

  • 2020-06-03 08:14:34
  • OfStack

This paper describes a basic Android image browser code, which is a copy of the original Google implementation, the code to achieve the main implementation process and methods, the specific improvement need to add their own, there are a lot of comments in the code, can help beginners quickly understand the code, using part of the image resources.

The main function codes are 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; // The statement ImageSwitcher Object, image display area 
 Gallery gallery;       // The statement Gallery Object, image list index 
 int imagePosition;      // Tag image array subscript for circular display 
 // Declares an array of image integers 
 private int[] images = {
  R.drawable.image1,R.drawable.image2,
  R.drawable.image3,R.drawable.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);
    // Through the control ID To obtain imageSwitcher The object of 
    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
    // Set a custom image display factory class 
    imageSwitcher.setFactory(new MyViewFactory(this));
    // Through the control ID To obtain gallery The object of 
    gallery = (Gallery) findViewById(R.id.gallery);
    // Set up a custom image adapter 
    gallery.setAdapter(new ImageAdapter(this)); 
    // Implement the selected event listener 
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {        
  @Override
  public void onItemSelected(AdapterView<?> parent, View view,
   int position, long id) {
  // By finding the remainder, the image is displayed in a loop 
  imageSwitcher.setImageResource(images[position%images.length]);  
  }
  @Override
  public void onNothingSelected(AdapterView<?> parent) {
  // TODO Auto-generated method stub  
  }
 });    
  }
  
  // Custom image adapter, inheritance BaseAdapter
  class ImageAdapter extends BaseAdapter{   
 private Context context; // Define context 
 
 // Parameter is the constructor of the context 
 public ImageAdapter(Context context) {
  this.context = context;
 }
   
 // Get the size of the picture 
 @Override
 public int getCount() {  // Set to the maximum number of integers 
  return Integer.MAX_VALUE;
 }

 // Gets the object of the specified image 
 @Override
 public Object getItem(int position) {  
  return null;
 }
 
 // Gets the object of the specified image ID
 @Override
 public long getItemId(int position) {  
  return 0;
 }

 // Display icon list 
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  ImageView iv = new ImageView(context); // create ImageView object 
  iv.setImageResource(images[position%images.length]);  // Set the loop to display images 
  iv.setAdjustViewBounds(true); // The picture automatically adjusts the display 
  // Set the width and height of the image 
  iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  return iv; // return ImageView object 
 }   
  }
  
  // Custom picture shows factory class, inheritance ViewFactory
  class MyViewFactory implements ViewFactory{
 private Context context; // Define context 
 
 // Parameter is the constructor of the context 
 public MyViewFactory(Context context) {
  this.context = context;
 }

 // Display icon area 
   @Override
 public View makeView() {
  ImageView iv = new ImageView(context); // create ImageView object 
  iv.setScaleType(ImageView.ScaleType.FIT_CENTER); // The image is automatically centered 
  // Set the width and height of the image 
  iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  return iv; // return ImageView object 
 }   
  }  
}

This article is only part of its main functional code, the reader can improve it 1 step further. The image viewer can also extend a number of practical Android image manipulation features, these are as a novice android application developer should be done skills.


Related articles: