package com.btb.gallery;
import java.util.ArrayList;

public class Gallery_Thumbnail{
    Gallery m_GalleryView = null;        //ListView 의 핸들을 가진다.
    //ListView 에 추가될 Item 배열이다.
    ArrayList<Bitmap> m_arrayListItem = new ArrayList<Bitmap>();
    //ListView와 Data 연결을 위한 Adapter 이다.
    GalleryViewAdapter m_GalleryViewAdapter = null;
    int m_Width = 0;
    int m_Height = 0;
   
    private ImageView[] img = new ImageView[3];
   
    //ListView 를 생성한다. ListView 생성 시 Galleryview 의 핸들값도 넘겨줘야 한다.
    public Gallery_Thumbnail(Context context, Gallery galleryview) {
        m_GalleryView = galleryview;
        m_GalleryView.setUnselectedAlpha(255);
        m_GalleryViewAdapter = new GalleryViewAdapter(context);
        m_GalleryView.setAdapter(m_GalleryViewAdapter);       
    }
    
    public void SetImageLayoutParams(int width, int height)
    {
        m_Width = width;
        m_Height = height;
    }
   
    public void SetSelection(int position)
    {
        m_GalleryView.setSelection(position);
    }
   
    //포커스를 요청하여 현재 설정되어 있는 ListView 로 가져온다.
    public boolean RequestFocus()
    {
        return m_GalleryView.requestFocus();
    }
   
    //현재 설정되어 있는 리스트뷰의 핸들을 리턴한다.
    public Gallery GetListViewHandle()
    {
        return m_GalleryView;
    }
   
    //Item 을 추가한다.
    public void AddItem(Bitmap addItem)
    {
        m_arrayListItem.add(addItem);
    }
   
    //Item 을 삭제한다.
    public void RemoveItem(int position)
    {
        m_arrayListItem.remove(position);
    }
   
    //새로운 아이템이 추가되었을 때 새로고침
    public void notifyDataSetChanged()
    {
        try {
            if(m_GalleryViewAdapter != null)
                m_GalleryViewAdapter.notifyDataSetChanged();
        }
        catch(Exception e) {
            Log.e("notifyDataSetChanged", e.toString());
        }
    }
   
    //Noti 를 받을 Event Listener 을 등록한다.
    public void SetItemClickListener(OnItemClickListener listener)
    {
        m_GalleryView.setOnItemClickListener(listener);
    }

    //ListView 와 Data 를  Adapter 로 연결한다.
    private class GalleryViewAdapter extends BaseAdapter
    {
        boolean isCheck = false;
        private Context mContext = null;

        // Constructor
        public GalleryViewAdapter(Context context) {
            mContext = context;           
        }

        public int getCount()
        {
            return m_arrayListItem.size();
        }

        public Object getItem(int position)
        {
            return position;
        }

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

        public View getView(int position, View convertView, ViewGroup parent)
        {
            //ImageView imageView = null;
            ImageView imageView;
            if(convertView == null)
            {
                //imageView = new ImageView(mContext);
                imageView = new CustomImageView(mContext);
                if(m_Width != 0 && m_Height != 0)
                    //ImageView 컨테이너의 사이즈를 설정한다.
                    imageView.setLayoutParams(new Gallery.LayoutParams(m_Width,m_Height));
                else
                    imageView.setLayoutParams(new Gallery.LayoutParams(100,70));
                //컨테이너 사이즈에 맞게 이미지를 리사이즈를 설정한다.
                imageView.setScaleType(ImageView.ScaleType.CENTER);
            }
            else
            {
                //imageView = (ImageView)convertView;
                imageView = (CustomImageView)convertView;
            }
            imageView.setBackgroundResource(R.drawable.galleryitemselector);

            //배열에 이미 저장되어 있는 이미지를 보여줌.
            //imageView.setImageBitmap(Bitmap.createScaledBitmap(m_arrayListItem.get(position), m_Width-6, m_Height-6, true));           
           
            return imageView;
        }
    }   
}

class CustomImageView extends ImageView
{

    private AnimationDrawable frameAnimation = new AnimationDrawable();
    private final static int duration = 300;
    Gallery_Thumbnail gallery = null;
    /**
     * @param context
     */
    public CustomImageView(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
        setBackgroundColor(0xFFFFFF);
       
        Resources res = context.getResources();
        frameAnimation.addFrame(res.getDrawable(R.drawable.b1_loading_ani_01), duration);
        frameAnimation.addFrame(res.getDrawable(R.drawable.b1_loading_ani_02), duration);
        frameAnimation.addFrame(res.getDrawable(R.drawable.b1_loading_ani_03), duration);
        frameAnimation.addFrame(res.getDrawable(R.drawable.b1_loading_ani_04), duration);
        frameAnimation.addFrame(res.getDrawable(R.drawable.b1_loading_ani_05), duration);
        frameAnimation.addFrame(res.getDrawable(R.drawable.b1_loading_ani_06), duration);
        frameAnimation.setOneShot(false);
    }

    /**
     * @param context
     * @param attrs
     */
    public CustomImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CustomImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
   
    private final Runnable Starter = new Runnable() {
        public void run() {
            frameAnimation.start(); 
        }
    };
   
    @Override
    protected void onDraw(Canvas canvas)
    {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        setImageDrawable(frameAnimation);       
        post(Starter);   
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        // TODO Auto-generated method stub
        //Log.d("Hello Android", "Got a touch event: " + event.getAction());
        return super.onTouchEvent(event);
    }    
}
device1.png device.png
                  [처음 실행시]                                            [좌우 flick시]
소스는 위와 같습니다. 원래의 로딩이미지는 위의 그림에서 보시듯 작습니다.
그런데 초기 실행시 커지고, 좌우 flick을 할 때 다시 원래 이미지로 표현되고 옆에 로딩이미지가 커집니다.
아~... 원인을 찾고 있는데... 아직...
조언 좀 부탁드립니다. ^^