西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)其它知識 → 可循環(huán)顯示圖像的Android Gallery組件

可循環(huán)顯示圖像的Android Gallery組件

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:本站整理時間:2010/8/24 0:14:12字體大。A-A+

作者:佚名點擊:2798次評論:0次標簽: Android 循環(huán)

  • 類型:源碼相關(guān)大。23.6M語言:中文 評分:9.1
  • 標簽:
立即下載
5 頁 本例中Main類的完整代碼
本例中Main類的完整代碼如下:


代碼
package net.blogjava.mobile;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
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.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

public class Main extends Activity implements OnItemSelectedListener,
        ViewFactory
{
    private Gallery gallery;
    private ImageSwitcher imageSwitcher;
    private ImageAdapter imageAdapter;

    private int[] resIds = new int[]
    { R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4,
            R.drawable.item5, R.drawable.item6, R.drawable.item7,
            R.drawable.item8, R.drawable.item9, R.drawable.item10,
            R.drawable.item11, R.drawable.item12, R.drawable.item13,
            R.drawable.item14, R.drawable.item15 };

    public class ImageAdapter extends BaseAdapter
    {
        int mGalleryItemBackground;
        private Context mContext;

        public ImageAdapter(Context context)
        {
            mContext = context;
            TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
            mGalleryItemBackground = typedArray.getResourceId(
                    R.styleable.Gallery_android_galleryItemBackground, 0);                       
        }
        // 第1點改進,返回一個很大的值,例如,Integer.MAX_VALUE
        public int getCount()
        {
            return Integer.MAX_VALUE;
        }

        public Object getItem(int position)
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }
 
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ImageView imageView = new ImageView(mContext);
                // 第2點改進,通過取余來循環(huán)取得resIds數(shù)組中的圖像資源ID
            imageView.setImageResource(resIds[position % resIds.length]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));
            imageView.setBackgroundResource(mGalleryItemBackground);
            return imageView;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
    {
        // 選中Gallery中某個圖像時,在ImageSwitcher組件中放大顯示該圖像
        imageSwitcher.setImageResource(resIds[position % resIds.length]);

    }
    @Override
    public void onNothingSelected(AdapterView<?> parent)
    {
    }

    @Override
    // ImageSwitcher組件需要這個方法來創(chuàng)建一個View對象(一般為ImageView對象)
    //  來顯示圖像
    public View makeView()
    {
        ImageView imageView = new ImageView(this);
        imageView.setBackgroundColor(0xFF000000);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return imageView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gallery = (Gallery) findViewById(R.id.gallery);
        imageAdapter = new ImageAdapter(this);
        gallery.setAdapter(imageAdapter);
        gallery.setOnItemSelectedListener(this);
        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
        // 設(shè)置ImageSwitcher組件的工廠對象
        imageSwitcher.setFactory(this);
        // 設(shè)置ImageSwitcher組件顯示圖像的動畫效果
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));

    }

總結(jié)
     在本文介紹了如何實現(xiàn)可循環(huán)顯示的Gallery組件。實際上,這個循環(huán)顯示只是一個偽循環(huán),不過由于getCount方法返回的圖像總數(shù)很大(超過20億),這就意味著已經(jīng)非常接近無限循環(huán)了。實現(xiàn)循環(huán)顯示圖像的關(guān)鍵點有如下兩個:
1.  getCount方法返回一個很大的整數(shù)值(例如,Integer.MAX_VALUE)。
2.  在getView方法中通過取余的方法來循環(huán)獲得圖像的資源ID。

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)