2010年3月23日 星期二

Android學習筆記 - 畫廊(Gallery)

1. 使用Gallery Widget來達到畫廊的效果。

2. 需建立一個繼承BaseAdapter的物件來放置你要呈現的圖片。

3. MainActivity.java
package org.me.android_gallery;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        ImageAdapter imageAdapter = new ImageAdapter(this);
        //設定圖片來源
        Integer[] mImageIds = {
            R.drawable.android_logo,
            R.drawable.androids,
            R.drawable.cupcake_2009,
            R.drawable.donut_2009,
            R.drawable.eclair_2009
        };
        //設定圖片的位置
        imageAdapter.setmImageIds(mImageIds);
        //圖片高度
        imageAdapter.setHeight(100);
        //圖片寬度
        imageAdapter.setWidth(200);
        gallery.setAdapter(imageAdapter);
        gallery.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "您選的是第"+position+"張圖", Toast.LENGTH_LONG).show();
            }
        });
    }
}

4. ImageAdapter.java
package org.me.android_gallery;
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 c) {
        mContext = c;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        //設定圖片來源
        imageView.setImageResource(mImageIds[position]);
        //設定圖片的寬、高
        imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public Integer[] getmImageIds() {
        return mImageIds;
    }

    public void setmImageIds(Integer[] mImageIds) {
        this.mImageIds = mImageIds;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public int getCount() {
        return mImageIds.length;
    }

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

    public long getItemId(int position) {
        return position;
    }
}


5. main.xml(Layout)
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </Gallery>
</LinearLayout>


4. 執行之後的畫面。


1 則留言:

  1. @@ 請問一下喔
    你這個是要點選
    才會顯示位置吧?
    那能不能拉到哪張圖
    他就自動顯示位置@@?

    回覆刪除