[Android] Gallery Android 2011. 12. 21. 14:38

캘러리는 수평으로 스크롤이 되는 어댑터 뷰로 중안에는 선택 항목으로 보이고 앞, 뒤 항목이 표시된다.

무난히 웹사이트에서 이미지 보이는 효과에서 많이들 봐 왔을 것이다.

xml에서 spacing 속성은 이미지간의 간격, animationDuration 은 이미지 스크롤 교체 시간을 이야기 한다.

xml 파일은 다음과 같다.

<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:spacing="5px"
    android:animationDuration="1500"
    />

자바 파일은 다음과 같다.

public class ImageViewActivity 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.gallery);
         g.setAdapter(new ImageAdapter(this));
     }
}

class ImageAdapter extends BaseAdapter{
 Context _context;

 int[] _img = {R.drawable.calendar,R.drawable.ic_launcher};
 
 public ImageAdapter(Context context){
  _context = context;
 }
 
 public int getCount() {
  return _img.length;
 }

 public Object getItem(int position) {
  return _img[position];
 }

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

 public View getView(int position, View convertView, ViewGroup parent) {
  ImageView view;
  if(convertView == null){
   view = new ImageView(_context);
  }
  else{
   view = (ImageView)convertView;
  }
  view.setImageResource(_img[position]);
  view.setScaleType(ImageView.ScaleType.FIT_XY);
  view.setLayoutParams(new Gallery.LayoutParams(45,45));
  return view;
 } 
}

여기에서 보면 LayoutParams 의 Width, Height 설정하는 부분의 사이즈는 이미지 사이즈를 말 하는 것이다.

이미지 파일을 만들어서 보여 줄 때 픽셀 단위로 확실하게 통일 해야 할 것이다. (필자 생각이다.)