欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)圖片瀏覽器示例

 更新時(shí)間:2014年07月30日 09:53:31   投稿:shichen2014  
這篇文章主要介紹了Android實(shí)現(xiàn)圖片瀏覽器示例,需要的朋友可以參考下

本文所述為一個(gè)基礎(chǔ)的Android圖片瀏覽器代碼,是仿寫(xiě)Google原版實(shí)現(xiàn)的,代碼中實(shí)現(xiàn)了主要的實(shí)現(xiàn)過(guò)程和方法,具體的完善還需要自己添加,代碼中有很多注釋,可幫助新手們快速理解代碼,使用了部分圖像資源。

主要功能代碼如下:

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對(duì)象,圖片顯示區(qū)域
 Gallery gallery;       //聲明Gallery對(duì)象,圖片列表索引
 int imagePosition;      //標(biāo)記圖片數(shù)組下標(biāo),用于循環(huán)顯示
 //聲明圖片整型數(shù)組
 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);
    //通過(guò)控件的ID獲得imageSwitcher的對(duì)象
    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
    //設(shè)置自定義的圖片顯示工廠類(lèi)
    imageSwitcher.setFactory(new MyViewFactory(this));
    //通過(guò)控件的ID獲得gallery的對(duì)象
    gallery = (Gallery) findViewById(R.id.gallery);
    //設(shè)置自定義的圖片適配器
    gallery.setAdapter(new ImageAdapter(this)); 
    //實(shí)現(xiàn)被選中的事件監(jiān)聽(tīng)器
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {        
  @Override
  public void onItemSelected(AdapterView<?> parent, View view,
   int position, long id) {
  //通過(guò)求余數(shù),循環(huán)顯示圖片
  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; //定義上下文
 
 //參數(shù)為上下文的構(gòu)造方法
 public ImageAdapter(Context context) {
  this.context = context;
 }
   
 //得到圖片的大小
 @Override
 public int getCount() {  //設(shè)置為整型的最大數(shù)
  return Integer.MAX_VALUE;
 }

 //得到指定圖片的對(duì)象
 @Override
 public Object getItem(int position) {  
  return null;
 }
 
 //得到指定圖片的對(duì)象的ID
 @Override
 public long getItemId(int position) {  
  return 0;
 }

 //顯示圖標(biāo)列表
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  ImageView iv = new ImageView(context); //創(chuàng)建ImageView對(duì)象
  iv.setImageResource(images[position%images.length]);  //設(shè)置循環(huán)顯示圖片
  iv.setAdjustViewBounds(true); //圖片自動(dòng)調(diào)整顯示
  //設(shè)置圖片的寬和高
  iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  return iv; //返回ImageView對(duì)象
 }   
  }
  
  //自定義圖片顯示工廠類(lèi),繼承ViewFactory
  class MyViewFactory implements ViewFactory{
 private Context context; //定義上下文
 
 //參數(shù)為上下文的構(gòu)造方法
 public MyViewFactory(Context context) {
  this.context = context;
 }

 //顯示圖標(biāo)區(qū)域
   @Override
 public View makeView() {
  ImageView iv = new ImageView(context); //創(chuàng)建ImageView對(duì)象
  iv.setScaleType(ImageView.ScaleType.FIT_CENTER); //圖片自動(dòng)居中顯示
  //設(shè)置圖片的寬和高
  iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  return iv; //返回ImageView對(duì)象
 }   
  }  
}

本文所述僅為其主要功能代碼部分,讀者可以對(duì)其進(jìn)一步加以完善。由圖像查看器還可以擴(kuò)展出很多實(shí)用的Android圖像操作功能,這些都是作為一個(gè)android應(yīng)用開(kāi)發(fā)新手應(yīng)該搞定的技巧。

相關(guān)文章

最新評(píng)論