Wednesday, September 28, 2011

Android Gallery example

Android 에서 Gallery 위젯을 사용하는 예제
Gallery 상에서 중앙에 위치하는 이미지의 위치를 확인하여 Gallery 하단에 표시하는 기능을 추가해 보았다.

사용자 삽입 이미지


사용된 이미지들

res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 <declare-styleable name="GalleryTheme"> 
  <attr name="android:galleryItemBackground" />
 </declare-styleable>
</resources>




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="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff">
   
 <TextView 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/hello" />
    
 <Gallery
  android:id="@+id/gallery"   
  android:layout_width="fill_parent"   
  android:layout_height="wrap_content"/>
 
 <LinearLayout
  android:id="@+id/dotPanel"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center">
 </LinearLayout>
 
</LinearLayout>



GalleryTestActivity.java
package com.dearpeople.android.test.gallery;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout.LayoutParams;
public class GalleryTestActivity extends Activity {
    /** Called when the activity is first created. */

 ImageView [] imageView;

    @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));   
   
     g.setOnItemClickListener(new AdapterView.OnItemClickListener() {       
      public void onItemClick(AdapterView parent, View v, int position, long id) {           
       //클릭된 경우에 실행됨
       //Fling시에는 실행안됨

      }   
     });
     /* Gallery 중앙의 아이템(선택된 아이템)이 변경되면 실행됨 */
     g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    /* 첫번째 아이템이 선택된 경우 arg2의 값은 0 이다 */
    //Log.i("Gallery Event", "Gallery Current Viewing Item "+arg2);

    selectDot(arg2);
   }
   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
   }
  });
     LinearLayout layout = (LinearLayout)findViewById(R.id.dotPanel);
     /* Gallery하단의 위치를 표시하는 작은 원의 갯수를 지정하여 그린다 */
     createDotPanel(this,layout,10);
    }
   
    private void createDotPanel(Context context, LinearLayout layout, int count){
     imageView = new ImageView[count];
     for(int i=0;i<count;i++){
      imageView[i] = new ImageView(context);
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, 0);
      params.width = LayoutParams.WRAP_CONTENT;
      params.height = LayoutParams.WRAP_CONTENT;
      params.leftMargin= 25;
      params.gravity = Gravity.CENTER;
      imageView[i].setLayoutParams(params);
      imageView[i].setImageResource(R.drawable.gray_circle);
      layout.addView(imageView[i]);
     }
    }
   
    private void selectDot(int position){
     for(int i=0;i<imageView.length;i++){
      if(i==position) imageView[i].setImageResource(R.drawable.black_circle);
      else imageView[i].setImageResource(R.drawable.gray_circle);
     }
    }
}



ImageAdapter.java
package com.dearpeople.android.test.gallery;
import android.content.*;
import android.content.res.*;
import android.view.*;
import android.widget.*;
public class ImageAdapter extends BaseAdapter {   
 int mGalleryItemBackground;   
 private Context mContext;   
 private Integer[] mImageIds = {           
   R.drawable.images01,           
   R.drawable.images02,           
   R.drawable.images03,           
   R.drawable.images04,           
   R.drawable.images05,           
   R.drawable.images06,           
   R.drawable.images07,
   R.drawable.images08,
   R.drawable.images09,
   R.drawable.images10
   };   
 public ImageAdapter(Context c) {       
  mContext = c;       
  TypedArray a = c.obtainStyledAttributes(R.styleable.GalleryTheme);       
  mGalleryItemBackground = a.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);      
  a.recycle();   
 }   
 public int getCount() {       
   return mImageIds.length;   
 }   
 public Object getItem(int position) {       
  return position;   
 }   
 public long getItemId(int position) {       
  return position;   
 }   
 public View getView(int position, View convertView, ViewGroup parent) {       
  ImageView i = new ImageView(mContext);       
  i.setImageResource(mImageIds[position]);       
  i.setLayoutParams(new Gallery.LayoutParams(250, 200));       
  i.setScaleType(ImageView.ScaleType.FIT_XY);       
  i.setBackgroundResource(mGalleryItemBackground );       
  return i;   
 }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.