Home Java javaTutorial Android image browser example

Android image browser example

Jan 17, 2017 pm 02:47 PM

This article describes a basic Android image browser code, which is imitated from the original version of Google. The main implementation processes and methods are implemented in the code. Specific improvements need to be added by yourself. There are many comments in the code, which can be To help novices quickly understand the code, some image resources are used.

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; //声明ImageSwitcher对象,图片显示区域
 Gallery gallery;       //声明Gallery对象,图片列表索引
 int imagePosition;      //标记图片数组下标,用于循环显示
 //声明图片整型数组
 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);
    //通过控件的ID获得imageSwitcher的对象
    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
    //设置自定义的图片显示工厂类
    imageSwitcher.setFactory(new MyViewFactory(this));
    //通过控件的ID获得gallery的对象
    gallery = (Gallery) findViewById(R.id.gallery);
    //设置自定义的图片适配器
    gallery.setAdapter(new ImageAdapter(this)); 
    //实现被选中的事件监听器
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {        
  @Override
  public void onItemSelected(AdapterView<?> parent, View view,
   int position, long id) {
  //通过求余数,循环显示图片
  imageSwitcher.setImageResource(images[position%images.length]);  
  }
  @Override
  public void onNothingSelected(AdapterView<?> parent) {
  // TODO Auto-generated method stub  
  }
 });    
  }
   
  //自定义图片适配器,继承BaseAdapter
  class ImageAdapter extends BaseAdapter{   
 private Context context; //定义上下文
  
 //参数为上下文的构造方法
 public ImageAdapter(Context context) {
  this.context = context;
 }
    
 //得到图片的大小
 @Override
 public int getCount() {  //设置为整型的最大数
  return Integer.MAX_VALUE;
 }
 
 //得到指定图片的对象
 @Override
 public Object getItem(int position) {  
  return null;
 }
  
 //得到指定图片的对象的ID
 @Override
 public long getItemId(int position) {  
  return 0;
 }
 
 //显示图标列表
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  ImageView iv = new ImageView(context); //创建ImageView对象
  iv.setImageResource(images[position%images.length]);  //设置循环显示图片
  iv.setAdjustViewBounds(true); //图片自动调整显示
  //设置图片的宽和高
  iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  return iv; //返回ImageView对象
 }   
  }
   
  //自定义图片显示工厂类,继承ViewFactory
  class MyViewFactory implements ViewFactory{
 private Context context; //定义上下文
  
 //参数为上下文的构造方法
 public MyViewFactory(Context context) {
  this.context = context;
 }
 
 //显示图标区域
   @Override
 public View makeView() {
  ImageView iv = new ImageView(context); //创建ImageView对象
  iv.setScaleType(ImageView.ScaleType.FIT_CENTER); //图片自动居中显示
  //设置图片的宽和高
  iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  return iv; //返回ImageView对象
 }   
  }  
}
Copy after login

The description in this article is only the main function code part, and readers can further improve it. The image viewer can also be extended to many practical Android image operation functions. These are skills that a novice Android application developer should master.

For more related articles on Android image browser examples, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

How to Share Data Between Steps in Cucumber How to Share Data Between Steps in Cucumber Mar 07, 2025 pm 05:55 PM

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive

How can I implement functional programming techniques in Java? How can I implement functional programming techniques in Java? Mar 11, 2025 pm 05:51 PM

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

See all articles