<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, GalleryActivity!</string>
    <string name="app_name">07.Gallery</string>
    <integer-array name="images">
        <item >0x7f020000</item>
        <item >0x7f020001</item>
        <item >0x7f020002</item>
        <item >0x7f020003</item>
    </integer-array>
</resources>
/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.gusfree.gallery;

public final class R {
    public static final class array {
        public static final int images=0x7f050000;
    }
    public static final class attr {
    }
    public static final class drawable {
        public static final int a=0x7f020000;
        public static final int b=0x7f020001;
        public static final int c=0x7f020002;
        public static final int d=0x7f020003;
        public static final int ic_launcher=0x7f020004;
    }
    public static final class id {
        public static final int gallery1=0x7f060000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

package com.gusfree.gallery;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryActivity extends Activity {
    Gallery gallery=null;
    int[] datas;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        gallery=(Gallery)this.findViewById(R.id.gallery1);      
        
        Resources res=this.getResources(); // 리소스 다가져와라
        datas=res.getIntArray(R.array.images); // 인트배열 가져와라
        gallery.setAdapter(new MyAdapter());
    }
    
    //innerClass  
    class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {			
			return datas.length; //4
		}

		@Override
		public Object getItem(int arg0) {			
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {			
			return arg0;
		}

		@Override
		//getCount()의 리턴값만큼 실행됩니다.
		//position 0,1,2,3
		public View getView(int position, View v, ViewGroup arg2) {
			//뷰를 만들어서 AdapterView 에게 넘겨주는 역활.
			// 갤러리에서 일하는 adapter 직원이 그림4장을 받아서
			//그림을 액자에 한장씩 끼워넣고 갤러리 벽에 거는 모습
			
			int imageResId = datas[position];//그림
			ImageView imageView= new ImageView(GalleryActivity.this);//액자
			
			imageView.setImageResource(imageResId);//액자에 그림 넣기
			
			imageView.setPadding(20, 20, 20, 20);//액자에 여백 주기
			
			return imageView;//갤러리 벽에 액자 걸기
		}
    }
}





+ Recent posts