Android-Gallery畫廊相簿   

練習檔 所以圖檔是放在drawable寫死的

layout拉一個Gallery
一個ImageSwitcher

===========================================================GalleryActivity.java

 

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;

public class GalleryActivity extends Activity {
    
    private Gallery gallery;
    private ImageSwitcher imageSwitcher;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //建立畫廊相簿
        gallery = (Gallery) findViewById(R.id.Gallery01);
        
        //建立相簿照片資料
        Integer[] imageIds = {
                R.drawable.sakura01, R.drawable.sakura02, R.drawable.sakura03, R.drawable.sakura04,
                R.drawable.sakura05, R.drawable.sakura06, R.drawable.sakura07, R.drawable.sakura08,
                R.drawable.sakura01, R.drawable.sakura02, R.drawable.sakura03, R.drawable.sakura04,
                R.drawable.sakura05, R.drawable.sakura06, R.drawable.sakura07, R.drawable.sakura08,
        };
        
        //將照片放在畫廊相簿內
        gallery.setAdapter(new ImageAdapter(this, 80, 60, imageIds));

        //設定畫廊相簿的監聽功能
        gallery.setOnItemSelectedListener(new MyOnItemSelectedListener());
        
        // 建立ImageSwitcher
        imageSwitcher = (ImageSwitcher)findViewById(R.id.ImageSwitcher01);
        
        // 在 ImageSwitcher中建立顯示組件
        imageSwitcher.setFactory(new MyViewFactory());
        
        // 設定照片進(in)出(out)的方式
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(GalleryActivity.this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(GalleryActivity.this, android.R.anim.fade_out));
        
        // 設定初始相片
        imageSwitcher.setImageResource(R.drawable.sakura01);
        
    }
    
    // 畫廊相簿的監聽器
    private class MyOnItemSelectedListener implements OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            imageSwitcher.setImageResource((int)gallery.getItemIdAtPosition(position));
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    }
    
    // ImageSwitcher 中的顯示組件
    private class MyViewFactory implements ViewFactory {
        @Override
        public View makeView() {
            ImageView imageView = new ImageView(GalleryActivity.this);            
            return imageView;
        }
    }
}
==================================================================ImageAdapter.java
Created with colorer-take5 library. Type 'java'

package com.test;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter{
    private Context mContext;
    private Integer width;
    private Integer height;
    private Integer[] mImageIds;
    
    public ImageAdapter(Context mContext, Integer width, Integer height, Integer[] mImageIds) {
        this.mContext = mContext;
        this.mImageIds = mImageIds;
        this.width = width;
        this.height = height;
    }
    
    //取得gallery內的照片數量
    public int getCount() {
        return mImageIds.length;
    }        
    //
    public Object getItem(int position) {
        return null;
    }
    
    //取得gallery內的某一張照片的檔案
    public long getItemId(int position) {
        return mImageIds[position];
    }

    //將某一張照片安置在 imageView,且設定顯示方式,在中間,大小���wxh
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mImageIds[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
        return imageView;
    }        


}

, , , ,
創作者介紹

Mazs's Notes

cookiesp 發表在 痞客邦 PIXNET 留言(0) 人氣()