Android/기본
Gallery ImageView
Bohemian life
2012. 4. 28. 19:40
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="fill_parent" android:spacing="5px" android:animationDuration="1500" /> <!-- android:animationDuration="1500" 1000분의 1초 이미지 속도(슬라이딩?) --> </LinearLayout>
package kr.android.gallery; import android.app.Activity; import android.os.Bundle; import android.content.Context; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast; import android.view.View; import android.view.ViewGroup; public class GalleryTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //이벤트 소스 Gallery g = (Gallery) findViewById(R.id.gallery); //Gallery에 ImageAdapter2를 이용해서 데이터 바인딩 g.setAdapter(new ImageAdapter2(this)); //익명 내부 클래스 형식의 이벤트 처리 g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<!--?--> parent, View v, int position, long id) { //공지 (알림 메세지)처리를 위해 Toast 객체 생성 //Toast = 단순 공지(알림) Toast.makeText(GalleryTest.this, position + "번째 이미지", Toast.LENGTH_SHORT).show(); } }); } } //사용자 정의 Adapter 클래스 class ImageAdapter2 extends BaseAdapter{ //추상 클래스 (get시리즈 갖고있음) private Context cxt; //이미지 저장소 private int[] mImageIds ={ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i, }; //생성자 public ImageAdapter2(Context cxt){ this.cxt = cxt; } //아이템의 갯수 public int getCount(){ return mImageIds.length; } //전달되는 position에 해당하는 데이터 반환 public Object getItem(int position){ return mImageIds[position]; } //전달되는 position반환(데이터 베이스 연동시 프라이머리키를 대입시켜서 사용) public long getItemId(int position) { return position; } //ImageView를 생성해서 Gallery에 데이터 바인딩하는 메소드 //전달되는 인자 //int position : position 정보 //View convertView : 데이터가 표시되는 객체 //ViewGroup parent : 사용중인 Gallery 객체 public View getView(int position, View convertView, ViewGroup parent){ ImageView imageView; if(convertView == null){ imageView = new ImageView(cxt); }else{ imageView = (ImageView)convertView; } imageView.setImageResource(mImageIds[position]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams(300,150)); return imageView; } }