ºÇ½ª¹¹¿·¡§ t_yokky2002 2011ǯ12·î01Æü(ÌÚ) 16:17:08ÍúÎò
¼¡¤Î¥×¥í¥¸¥§¥¯¥È¤òºîÀ®¤·¤Æ¤¯¤À¤µ¤¤¡£
¹àÌÜ̾ | ÀßÄêÃÍ |
---|---|
¥×¥í¥¸¥§¥¯¥È̾ | image_gallery |
¥Ó¥ë¥É¥¿¡¼¥²¥Ã¥È | Android 2.2 |
¥¢¥×¥ê¥±¡¼¥·¥ç¥ó̾ | ¥¤¥á¡¼¥¸¥®¥ã¥é¥ê¡¼ |
¥Ñ¥Ã¥±¡¼¥¸Ì¾ | jp.co.example.practice |
¥¢¥¯¥Æ¥£¥Ó¥Æ¥£¤ÎºîÀ® | MainActivity |
ºÇ¾®SDK¥Ð¡¼¥¸¥ç¥ó | 8 |
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>
¥µ¥à¥Í¥¤¥ë²èÁü¤ò¥®¥ã¥é¥ê¡¼¤ËÇÛÃÖ¤¹¤ëºÝ¤Î¥ì¥¤¥¢¥¦¥È¤òºîÀ®¤·¤Þ¤¹¡£
res/layout/gallery_item.xml¤ò¿·µ¬ºîÀ®¤·¡¢¼¡¤ÎÆâÍƤ˽ñ¤´¹¤¨¤Æ¤¯¤À¤µ¤¤¡£
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>
¥®¥ã¥é¥ê¡¼ÍѤΥ¢¥À¥×¥¿¡¼¥¯¥é¥¹¤òºîÀ®¤·¤Þ¤¹¡£
src/jp.co.example.practice/ImageAdapter.java¥Õ¥¡¥¤¥ë¤ò¿·µ¬ºîÀ®¤·¤Þ¤¹¡£
src/jp.co.example.practice¥Ñ¥Ã¥±¡¼¥¸¤ò±¦¥¯¥ê¥Ã¥¯¤·¡¢¿·µ¬¢Í¥¯¥é¥¹¤òÁªÂò¤·¡¢¼¡¤ÎÆâÍƤòÆþÎϤ·¤Æ¤¯¤À¤µ¤¤¡£
¼¡¤ÎÆâÍƤ˽ñ¤´¹¤¨¤Æ¤¯¤À¤µ¤¤¡£
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; } // ¢¬¤³¤³¤Þ¤Ç }
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); } // ¢¬¤³¤³¤Þ¤Ç }
GalleryÍѤΥơ¼¥Þ¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¡ÖÇطʲèÁü¡×¤òŬÍѤ·¤Þ¤¹¡£
res/values/attrs.xml¤ò¿·µ¬ºîÀ®¤·¡¢¼¡¤ÎÆâÍƤ˽ñ¤´¹¤¨¤Æ¤¯¤À¤µ¤¤¡£
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; }
¥³¥á¥ó¥È¤ò¤«¤¯