Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 開發入門 >> Android圖片浏覽源碼解讀

Android圖片浏覽源碼解讀

編輯:開發入門

 android手機操作系統的應用方式靈活,簡單,深受廣大編程愛好者的喜愛。尤其是它的開源代碼,使得我們能夠方便的得到自己想要的功能需求。今天我們就為大家帶來了有關android圖片浏覽的相關方法。

  首先是android圖片浏覽中layout XML:

  1.< ?XML version="1.0" encoding="utf-8"?>

  2.< RelativeLayout XMLns:android="http://schemas.Android.com/apk/res/android"

  3.android:layout_width="fill_parent"

  4.android:layout_height="fill_parent">

  5.< ImageSwitcher android:id="@+id/switcher"

  6.android:layout_width="fill_parent"

  7.android:layout_height="fill_parent"

  8.android:layout_alignParentTop="true"

  9.android:layout_alignParentLeft="true"

  10./>

  11.< Gallery android:id="@+id/gallery"

  12.android:background="#55000000"

  13.android:layout_width="fill_parent"

  14.android:layout_height="60dp"

  15.android:layout_alignParentBottom="true"

  16.android:layout_alignParentLeft="true"

  17.android:gravity="center_vertical"

  18.android:spacing="16dp"

  19./>

  20.< /RelativeLayout>

  layout裡面用到了前面所說的兩個控件,ImageSwitcher用啦顯示全圖,Gallery用來顯示縮略圖。著重看看ImageSwitcher,ImageSwitcher1中需要實現ViewSwitcher.VIEwFactory這個接口,這個接口裡有個方法makeVIEw,這樣就產生了用來顯示圖片的vIEw. ImageSwitcher調用過程是這樣的,首先要有一個Factory為它提供一個VIEw,然後ImageSwitcher就可以初始化各種資源了。注意在使用一個ImageSwitcher之前,一定要調用setFactory方法,要不setImageResource這個方法會報空指針異常。

  下面是android圖片浏覽代碼:

  1.package com.zx.imageswitcher;

  2.import android.app.Activity;

  3.import android.content.Context;

  4.import android.os.Bundle;

  5.import android.view.VIEw;

  6.import android.view.VIEwGroup;

  7.import android.vIEw.animation.AnimationUtils;

  8.import android.widget.AdapterVIEw;

  9.import android.widget.BaseAdapter;

  10.import android.widget.Gallery;

  11.import android.widget.ImageSwitcher;

  12.import android.widget.ImageVIEw;

  13.import android.widget.VIEwSwitcher;

  14.import android.widget.Gallery.LayoutParams;

  15.public class ImageSwitcherTest extends Activity implements

  16.AdapterView.OnItemSelectedListener, ViewSwitcher.VIEwFactory{

  17.private ImageSwitcher mSwitcher;

  18.private Integer[] mThumbIds = {

  19.R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,

  20.R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,

  21.R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,

  22.R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};

  23.private Integer[] mImageIds = {

  24.R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,

  25.R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,

  26.R.drawable.sample_6, R.drawable.sample_7};

  27./** Called when the activity is first created. */

  28.@Override

  29.public void onCreate(Bundle savedInstanceState) {

  30.super.onCreate(savedInstanceState);

  31.setContentVIEw(R.layout.main);

  32.mSwitcher = (ImageSwitcher) findVIEwById(R.id.switcher);

  33.mSwitcher.setFactory(this);

  34.mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,

  35.android.R.anim.fade_in));

  36.mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,

  37.android.R.anim.fade_out));

  38.Gallery g = (Gallery) findVIEwById(R.id.gallery);

  39.g.setAdapter(new ImageAdapter(this));

  40.g.setOnItemSelectedListener(this);

  41.}

  42./*

  43.* override for ViewSwitcher.ViewFactory#makeVIEw()

  44.*/

  45.public View makeVIEw() {

  46.ImageView i = new ImageVIEw(this);

  47.i.setBackgroundColor(0xFF000000);

  48.i.setScaleType(ImageVIEw.ScaleType.FIT_CENTER);

  49.i.setLayoutParams(new ImageSwitcher.LayoutParams

  (LayoutParams.FILL_PARENT,

  50.LayoutParams.FILL_PARENT));

  51.return i;

  52.}

  53./*

  54.* override for

  55.* AdapterVIEw.OnItemSelectedListener#onItemSelected()

  56.*/

  57.public void onItemSelected(AdapterVIEw parent,

  VIEw v, int position, long id) {

  58.mSwitcher.setImageResource(mImageIds[position]);

  59.}

  60./*

  61.* override for AdapterVIEw.OnItemSelectedListener

  #onNothingSelected()

  62.*/

  63.public void onNothingSelected(AdapterVIEw< ?> arg0) {

  64.// TODO Auto-generated method stub

  65.}

  66.public class ImageAdapter extends BaseAdapter {

  67.public ImageAdapter(Context c) {

  68.mContext = c;

  69.}

  70.public int getCount() {

  71.return mThumbIds.length;

  72.}

  73.public Object getItem(int position) {

  74.return position;

  75.}

  76.public long getItemId(int position) {

  77.return position;

  78.}

  79.public View getView(int position, View convertVIEw,

  VIEwGroup parent) {

  80.ImageView i = new ImageVIEw(mContext);

  81.i.setImageResource(mThumbIds[position]);

  82.i.setAdjustVIEwBounds(true);

  83.i.setLayoutParams(new Gallery.LayoutParams(

  84.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

  85.i.setBackgroundResource(R.drawable.picture_frame);

  86.return i;

  87.}

  88.private Context mContext;

  89.}

  90.}

  從android圖片浏覽的代碼中看到還實現了AdapterVIEw.OnItemSelectedListener,這樣就需要重寫onItemSelected()方法,然後在該方法中:mSwitcher.setImageResource(mImageIds[position]);這樣就實現了圖片在ImageSwitcher中的切換。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved