Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

Android圖片浏覽源碼解讀

編輯:開發入門

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中的切換。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved