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)文章
Android平臺(tái)預(yù)置GMS包后關(guān)機(jī)鬧鐘失效問(wèn)題及解決方法
這篇文章主要介紹了Android平臺(tái)預(yù)置GMS包后,關(guān)機(jī)鬧鐘失效,本文給大家分享問(wèn)題原因及解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Android編程滑動(dòng)效果之倒影效果實(shí)現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了Android編程滑動(dòng)效果之倒影效果實(shí)現(xiàn)方法,基于繼承BaseAdapter自定義Gallery和ImageAdapter實(shí)現(xiàn)倒影的功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02Android app會(huì)crash的原因及解決方法
這篇文章主要介紹了Android app會(huì)crash的原因及解決方法,幫助大家更好的進(jìn)行Android開(kāi)發(fā),感興趣的朋友可以了解下2020-12-12Android?studio實(shí)現(xiàn)動(dòng)態(tài)背景頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)動(dòng)態(tài)背景頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04一文教你如何使用Databinding寫(xiě)一個(gè)關(guān)注功能
這篇文章主要介紹了一文教你如何使用Databinding寫(xiě)一個(gè)關(guān)注功能,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Android系統(tǒng)設(shè)置中的清除數(shù)據(jù)會(huì)清除哪些數(shù)據(jù)?
這篇文章主要介紹了Android系統(tǒng)設(shè)置中的清除數(shù)據(jù)會(huì)清除哪些數(shù)據(jù)?本文對(duì)比了清除前和清除后的數(shù)據(jù)情況,從而得出到底清除了哪些數(shù)據(jù),需要的朋友可以參考下2015-01-01Android build.gradle版本名打包配置的方法
這篇文章主要介紹了Android build.gradle版本名打包配置的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Android 用SQLite實(shí)現(xiàn)事務(wù)的方法
本篇文章小編為大家介紹,Android用SQLite實(shí)現(xiàn)事務(wù)的方法。需要的朋友參考下2013-04-04