編輯:開發入門
Android手機操作系統的應用方式靈活簡單,深受廣大編程愛好者的喜愛;尤其是它開放的源代碼,讓我們能方便地實現自己想要的各項功能。今天就為大家帶來Android圖片瀏覽的相關實現方法。
首先來看圖片瀏覽用到的布局文件 layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageSwitcher android:id="@+id/switcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<Gallery android:id="@+id/gallery"
android:background="#55000000"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp" />
</RelativeLayout>
布局中用到前面提到的兩個控件:ImageSwitcher用於顯示大圖,Gallery用於顯示縮略圖。重點看ImageSwitcher:使用時需要實現ViewSwitcher.ViewFactory接口,該接口裏有一個makeView方法,用來產生顯示圖片的View。ImageSwitcher的工作流程是這樣的——先有一個Factory為它提供一個View,隨後ImageSwitcher才能初始化各項資源。要特別注意:在使用ImageSwitcher之前,一定要先調用setFactory方法,否則調用setImageResource時會拋出空指針異常。
下面是完整的圖片瀏覽代碼:
package com.zx.imageswitcher;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;
public class ImageSwitcherTest extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
private ImageSwitcher mSwitcher;
private Integer[] mThumbIds = {
R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};
private Integer[] mImageIds = {
R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
/* override for ViewSwitcher.ViewFactory#makeView() */
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
/* override for AdapterView.OnItemSelectedListener#onItemSelected() */
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}
/* override for AdapterView.OnItemSelectedListener#onNothingSelected() */
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}
}
從代碼中可以看到,我們還實現了AdapterView.OnItemSelectedListener,因此需要重寫onItemSelected()方法,並在其中調用mSwitcher.setImageResource(mImageIds[position]),這樣就完成了圖片在ImageSwitcher中的切換。
只需一個 U 盤,在電腦上也能體驗安卓系統
想體驗 Android 系統,不一定需要購買新手機或平板電腦。只要準備一個 U 盤,並製作可開機的 Android 系統隨身碟,就能在電腦上嘗試 Android 的操作介
用 Eclipse 與 Android 元件做開發(三):TextView、EditText、Button 與 Toast 實作
動手做一個用到元件的範例應用 現在來改寫現有的應用,讓它變成下圖那樣。這個例子的目的,是讓你熟悉幾個基本元件在真實應用中的用法:TextView、EditText、But
Android 上的 XML 與 JSON 實戰(上):從格式基礎到 Twitter feed 解析
簡介:這個兩 Part 的系列要談的是行動平台上處理資料交換格式的實務——XML 與 JavaScript Object Notation(JSON)。第一篇先把兩種格式
用 Eclipse 與 Android 元件做開發(四):用 ListView 與 ContentProvider 做通訊錄
用 ListView 元件:一個簡單的範例應用 這一節我們要動手做一個範例應用,用 ListView 元件把電話簿裡的所有聯絡人列出來(見圖 9a、9b)。這個例子不只示