Android¥¢¥×¥ê¤ò³«È¯¤¹¤ë¤¿¤á¤Î´ðÁäò¤Þ¤È¤á¤Æ¤¢¤ê¤Þ¤¹¡£

Step1

¥×¥í¥¸¥§¥¯¥È¤ÎºîÀ®

¼¡¤Î¥×¥í¥¸¥§¥¯¥È¤òºîÀ®¤·¤Æ¤¯¤À¤µ¤¤¡£
¹àÌÜ̾ÀßÄêÃÍ
¥×¥í¥¸¥§¥¯¥È̾image_gallery
¥Ó¥ë¥É¥¿¡¼¥²¥Ã¥ÈAndroid 2.2
¥¢¥×¥ê¥±¡¼¥·¥ç¥ó̾¥¤¥á¡¼¥¸¥®¥ã¥é¥ê¡¼
¥Ñ¥Ã¥±¡¼¥¸Ì¾jp.co.example.practice
¥¢¥¯¥Æ¥£¥Ó¥Æ¥£¤ÎºîÀ®MainActivity
ºÇ¾®SDK¥Ð¡¼¥¸¥ç¥ó8

²èÁüÁǺà

»öÁ°¤Ë¥À¥¦¥ó¥í¡¼¥É¤·¤Æ¤ª¤¤¤¿²èÁü¤ò¡¢res/drawable-nodpi¥Õ¥©¥ë¥À¡¼¤Ë¥³¥Ô¡¼¤·¤Æ¤¯¤À¤µ¤¤¡£
¢¨drawable-nodpi¥Õ¥©¥ë¥À¡¼¤¬Â¸ºß¤·¤Ê¤¤¾ì¹ç¡§ res¥Õ¥©¥ë¥À¡¼¤ò±¦¥¯¥ê¥Ã¥¯¤·¡¢¿·µ¬¢ª¥Õ¥©¥ë¥À¡¼¤è¤êºîÀ®

»öÁ°½àÈ÷¤Ï´°Î»¤Ç¤¹¡£º£²ó¤Ï¡¢¼¡¤Î¤è¤¦¤Ê¥¢¥×¥ê¤òÀ©ºî¤·¤Þ¤¹¡£

Step2

res/layout/main.xml¤ò¡¢¼¡¤Î¤è¤¦¤Ë½ñ¤­´¹¤¨¤Æ¤¯¤À¤µ¤¤¡£
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center">
  <ImageView
    android:id="@+id/image_view"
    android:layout_width="280dip"
    android:layout_height="280dip"
    android:layout_marginBottom="20dip"
    android:scaleType="centerCrop"
    />
  <Gallery
    android:id="@+id/gallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00aa0000"
    />
</LinearLayout>

Step3

¥µ¥à¥Í¥¤¥ë²èÁü¤ò¥®¥ã¥é¥ê¡¼¤ËÇÛÃÖ¤¹¤ëºÝ¤Î¥ì¥¤¥¢¥¦¥È¤òºîÀ®¤·¤Þ¤¹¡£
res/layout/gallery_item.xml¤ò¿·µ¬ºîÀ®¤·¡¢¼¡¤ÎÆâÍƤ˽ñ¤­´¹¤¨¤Æ¤¯¤À¤µ¤¤¡£
¿·µ¬ºîÀ®ÊýË¡
layout¥Õ¥©¥ë¥À¡¼¤ò±¦¥¯¥ê¥Ã¥¯¤·¡¢¡Ö¿·µ¬¢Í¤½¤Î¾¢ÍAndroid¥Õ¥©¥ë¥À¡¼¢ÍAndroid XML¥Õ¥¡¥¤¥ë¡×¤òÁªÂò
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  <ImageView
    android:id="@+id/gallery_image_view"
    android:layout_width="130dip"
    android:layout_height="130dip"
    android:background="?android:attr/galleryItemBackground"
    android:scaleType="fitXY" />
</LinearLayout>

Step4

¥®¥ã¥é¥ê¡¼ÍѤΥ¢¥À¥×¥¿¡¼¥¯¥é¥¹¤òºîÀ®¤·¤Þ¤¹¡£
src/jp.co.example.practice/ImageAdapter.java¥Õ¥¡¥¤¥ë¤ò¿·µ¬ºîÀ®¤·¤Þ¤¹¡£
src/jp.co.example.practice¥Ñ¥Ã¥±¡¼¥¸¤ò±¦¥¯¥ê¥Ã¥¯¤·¡¢¿·µ¬¢Í¥¯¥é¥¹¤òÁªÂò¤·¡¢¼¡¤ÎÆâÍƤòÆþÎϤ·¤Æ¤¯¤À¤µ¤¤¡£



¼¡¤ÎÆâÍƤ˽ñ¤­´¹¤¨¤Æ¤¯¤À¤µ¤¤¡£
public class ImageAdapter extends ArrayAdapter<Integer> {
        // ¢­¤³¤³¤«¤é
	private Context mContext;
	private LayoutInflater mInflater;

	public ImageAdapter(Context context, Integer[] objects) {
		super(context, 0, objects);
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	// ImageView¤ËÂбþ¤¹¤ë¥Ó¥Ã¥È¥Þ¥Ã¥×¤òɽ¼¨
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.gallery_item, parent,
					false);
		}
		imageView = (ImageView) convertView
				.findViewById(R.id.gallery_image_view);
		imageView.setImageResource(getItem(position));

		return convertView;
	}
        // ¢¬¤³¤³¤Þ¤Ç
}

Step5

res/jp.co.example.practice/MainActivity.java¤ò¡¢¼¡¤Î¤è¤¦¤Ë½ñ¤­´¹¤¨¤Æ¤¯¤À¤µ¤¤¡£
public class MainActivity extends Activity {
        // ÄɲÃ
	private ImageView mImageView;
	private Integer[] mThumbIds = { R.drawable.photo1, R.drawable.photo8,
			R.drawable.photo7, R.drawable.photo4, R.drawable.photo5,
			R.drawable.photo6, R.drawable.photo9 };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
                // ¢­¤³¤³¤«¤é
		mImageView = (ImageView) findViewById(R.id.image_view);
		// ¥µ¥à¥Í¥¤¥ë¤Î¥®¥ã¥é¥ê¡¼¤òºîÀ®
		setGallery();
                // ¢¬¤³¤³¤Þ¤Ç
	}
    
        // ¢­¤³¤³¤«¤é
	public void setGallery() {
		Gallery g = (Gallery) findViewById(R.id.gallery);
		g.setAdapter(new ImageAdapter(this, mThumbIds));

		g.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
			}
		});
	}

	public void setImageBitmap(Bitmap bmp) {
		mImageView.setImageBitmap(bmp);
	}
        // ¢¬¤³¤³¤Þ¤Ç
}

Step6

¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£²èÁü¤ò²¡²¼¤¹¤ë¤È¡¢¥È¡¼¥¹¥È¤Ë¤ÆÈֹ椬ɽ¼¨¤µ¤ì¤Þ¤¹¡£

Step7

¥Æ¡¼¥Þ¤ÎŬÍÑ

GalleryÍѤΥơ¼¥Þ¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¡ÖÇطʲèÁü¡×¤òŬÍѤ·¤Þ¤¹¡£
res/values/attrs.xml¤ò¿·µ¬ºîÀ®¤·¡¢¼¡¤ÎÆâÍƤ˽ñ¤­´¹¤¨¤Æ¤¯¤À¤µ¤¤¡£
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="MyGallery">
    <attr name="android:galleryItemBackground" />
  </declare-styleable>
</resources>

¥¢¥À¥×¥¿¡¼¤Î½¤Àµ

public class ImageAdapter extends ArrayAdapter<Integer> {
	private Context mContext;
	private LayoutInflater mInflater;
	private int mGalleryItemBackground; 	//--- Äɲà ---

	public ImageAdapter(Context context, Integer[] objects) {
		super(context, 0, objects);
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                //--- ¢­¤³¤³¤«¤é ---
		TypedArray a = mContext.obtainStyledAttributes(R.styleable.MyGallery);
		// GalleryÍѥơ¼¥Þ¤ÇÀßÄꤵ¤ì¤Æ¤¤¤ëÇطʲèÁü¤Î¥ê¥½¡¼¥¹ID¤ò¼èÆÀ
		mGalleryItemBackground = a.getResourceId(
				R.styleable.MyGallery_android_galleryItemBackground, 0);
		a.recycle();
		//--- ¢­¤³¤³¤Þ¤Ç ---
	}

	// ImageView¤ËÂбþ¤¹¤ë¥Ó¥Ã¥È¥Þ¥Ã¥×¤òɽ¼¨
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.gallery_item, parent,
					false);
		}
		imageView = (ImageView) convertView
				.findViewById(R.id.gallery_image_view);
		imageView.setBackgroundResource(mGalleryItemBackground); 	//--- Äɲà ---
		imageView.setImageResource(getItem(position));

		return convertView;
	}

Step8

¼Â¹Ô¤·¡¢³Îǧ¤·¤Æ¤¯¤À¤µ¤¤¡£

¥³¥á¥ó¥È¤ò¤«¤¯


¡Öhttp://¡×¤ò´Þ¤àÅê¹Æ¤Ï¶Ø»ß¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

ÍøÍѵ¬Ìó¤ò¤´³Îǧ¤Î¤¦¤¨¤´µ­Æþ²¼¤µ¤¤

´ÉÍý¿Í/Éû´ÉÍý¿Í¤Î¤ßÊÔ½¸¤Ç¤­¤Þ¤¹