요즘 박대표의 안드로이드 명강의를 2주째 듣고 있습니다.
드뎌 안드로이드의 바다에 빠진것 같네요.ㅋㅋㅋ
이번주에 만들어본 이미지 갤러리 데모 입니다.
아래는 주요 소스 입니다.
public class HelloGallery extends Activity {
private ImageAdapter ia;
private ImageView iv;
public int current_pos=0;
public static HelloGallery instance = null;
private int click_cnt = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
instance = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
ia = new ImageAdapter(this);
g.setAdapter(ia);
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
current_pos = position;
// Case 1
//iv.setImageResource(ia.getImage(position));
//iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
// Case 2
//Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
// Case 3
startActivity (new Intent(HelloGallery.this, Bom.class));
if( (click_cnt++ % 2) == 0)
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
else
overridePendingTransition(R.anim.shake, R.anim.cycle_7);
}
});
iv = (ImageView) findViewById(R.id.ImageView01);
iv.setImageResource ( R.drawable.sample_1 );
// resize to want width/height
iv.setLayoutParams(new android.widget.LinearLayout.LayoutParams(100, 100) );
}
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
public ImageAdapter(Context c) {
mContext = c;
//obtainStyledAttributes(android.R.styleable.Theme);
TypedArray a = c.obtainStyledAttributes(
//android.R.styleable.Theme
R.styleable.HelloGallery
);
mGalleryItemBackground = a.getResourceId(
//android.R.styleable.Theme_galleryItemBackground
R.styleable.HelloGallery_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(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
public int getImage (int position) { return mImageIds[position]; }
}
public class Bom extends Activity {
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bom);
ImageView v = (ImageView) findViewById(R.id.bomImg);
//v.setImageResource(R.drawable.sample_3);
//v.setImageResource(mImageIds[ ((HelloGallery) this.getParent()).current_pos ]);
v.setImageResource(mImageIds[ HelloGallery.instance.current_pos ]);
//i.setScaleType(ImageView.ScaleType.FIT_XY);
v.setScaleType(ImageView.ScaleType.CENTER);
}
}