Android實現圖片瀏覽器樣本_Android

來源:互聯網
上載者:User

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

主要功能代碼如下:

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對象 }     }  }

本文所述僅為其主要功能代碼部分,讀者可以對其進一步加以完善。由映像查看器還可以擴充出很多實用的Android映像操作功能,這些都是作為一個android應用開發新手應該搞定的技巧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.