Where The Streets Have No Name

android 에서 gallery를 이용한 이미지 슬라이더 본문

Developement/Mobile

android 에서 gallery를 이용한 이미지 슬라이더

highheat 2011. 9. 6. 19:16
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class HelloGallery extends Activity {

	@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 OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				Toast.makeText(HelloGallery.this, "" + position,
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	public class ImageAdapter extends BaseAdapter {
		int mGalleryItemBackground;
		private Context mContext;

		private Integer[] mImageIds = { R.drawable.f1, R.drawable.f2,
				R.drawable.f3, R.drawable.f4, R.drawable.f5, R.drawable.f6 };
		
		private String[] mImages = {"http://30park.com/data/geditor/1107/1846061413_4537ca02_justin_verlander_wallpaper.jpg",
				                    "http://192.168.0.59/test/manual/manual1/K-002.jpg",
				                    "http://192.168.0.59/test/manual/manual1/K-003.jpg",
				                    "http://192.168.0.59/test/manual/manual1/K-004.jpg",
				                    "http://192.168.0.59/test/manual/manual1/K-005.jpg",
				                    "http://192.168.0.59/test/manual/manual1/K-006.jpg",
				                    "http://192.168.0.59/test/manual/manual1/K-007.jpg",
				                    "http://192.168.0.59/test/manual/manual1/K-008.jpg",
				                    "http://192.168.0.59/test/manual/manual1/K-009.jpg",
				                    "http://192.168.0.59/test/manual/manual1/K-010.jpg"};

		public ImageAdapter(Context c) {
			mContext = c;
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
			mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
			a.recycle();
		}

		public int getCount() {
			return mImages.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);
						
			Display display = ((Activity)mContext).getWindowManager().getDefaultDisplay();
			int height = display.getHeight();
			int width = display.getWidth();
			
			i.setLayoutParams(new Gallery.LayoutParams(width, height));
			i.setScaleType(ImageView.ScaleType.FIT_CENTER);
			i.setBackgroundColor(0x00FF0000);

			try {
				URL url = new URL(mImages[position]);
				URLConnection conn = url.openConnection();
				conn.connect();
				BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
				Bitmap bm = BitmapFactory.decodeStream(bis);
				bis.close();
				i.setImageBitmap(bm);
			} catch (IOException e) {
				Log.e("HelloGallery", e.getMessage());
			}

			return i;
		}
	}
}
<?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:layout_gravity="center"
    android:spacing="10dip"
    android:gravity="center_vertical|center_horizontal|center"
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>