import android.app.Activity; import android.os.Bundle; import android.content.Context; import android.content.res.TypedArray; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class GDD01 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery g =(Gallery)findViewById(R.id.mygallery); g.setAdapter(new ImageAdapter(this)); g.setOnItemClickListener(new Gallery.OnItemClickListener(){ @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(arg1.getContext(), getString(R.string.my_gallery_text_pre) +arg2+getString(R.string.my_gallery_text_post), Toast.LENGTH_SHORT).show(); } }); } public class ImageAdapter extends BaseAdapter { int BackGround; Context m_context; Integer[]imageId={ R.drawable.png001,R.drawable.png002, R.drawable.png003,R.drawable.png004, R.drawable.png005,R.drawable.png006, R.drawable.png007,R.drawable.png008, R.drawable.png009,R.drawable.png010, R.drawable.png011 }; //建構子只有一個參數,即要儲存的Context public ImageAdapter(Context c) { super(); m_context=c; TypedArray array=m_context.obtainStyledAttributes(R.styleable.Gallery); BackGround=array.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); } //回傳所有已定義的圖片總數量 public int getCount() { return imageId.length; } public Object getItem(int position) { return position; } //取得圖片編號 public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView i = new ImageView(m_context); i.setImageResource(imageId[position]); i.setLayoutParams(new Gallery.LayoutParams(236, 188)); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setBackgroundResource(BackGround); return i; } } //使用android.R.drawable裡的圖片當成圖庫來源 }
2012年3月9日 星期五
Android-TQC106畫廊展示
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言