도전2022

안드로이드 갤러리 만들기 본문

소스코드

안드로이드 갤러리 만들기

hotdigi 2012. 8. 10. 17:25

http://androidimran.blogspot.kr/2012/06/android-gallery-example.html


Android Gallery Example

Gallery is an internal element which can scroll horizontally and layout component of the currently selected child elements are positioned in the center of it.



STEP 1: Creating a new project from File->New->Android Project with Gallerydemo Activity Name

STEP 2: Copy Image files to res/drawable Folder

STEP 3: In res/layout/main.xm layout add ImageView and Gallery View as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <!-- ImageView for display image in background --> 
   <ImageView 
        android:id="@+id/imgview" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:src="@drawable/a01" 
         />
          
         <!-- Gallery To show images Gallery  --> 
        <Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/galleryview" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:spacing="5dp" 
        />
 
</FrameLayout >

STEP 4: In res/values add attrs.xml as:
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="HelloGallery"
        <attr name="android:galleryItemBackground"/> 
   </declare-styleable>
</resources>

STEP 5: In Gallerydemo Activity Create an ImageAdapter which extends BaseAdapter as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.imrankhanandroid.Galleryexp;
 
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
 
public class Gallerydemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
         
        final ImageView imagevew = (ImageView)findViewById(R.id.imgview); 
        Gallery gallryview = (Gallery)findViewById(R.id.galleryview);
        gallryview.setAdapter(new ImageAdapter(this));
        gallryview.setOnItemClickListener(new OnItemClickListener() {
 
    @Override
    public void onItemClick(AdapterView<!--?--> parent,
            View view, int position,long id) {
        // TODO Auto-generated method stub
            // The first few pictures show click
        Toast.makeText(Gallerydemo.this, "" + position, 
                 Toast.LENGTH_LONG).show(); 
        //Set the backgroundPart of theImageView
        imagevew.setImageResource(((ImageView) view).getId());
            }
        });
    }
    public class ImageAdapter extends BaseAdapter {
 
        int mGalleryItemBackground; 
        // Context object
        private Context mContext; 
        // Picture array
        private Integer[] mImageIds ={
                R.drawable.a01,R.drawable.a02,
                R.drawable.a03,R.drawable.a04,
                R.drawable.a05,R.drawable.a06,
                R.drawable.a07,R.drawable.a08,
                R.drawable.a09,R.drawable.a010,
        };
        // ImageAdapter Constructor
        public ImageAdapter(Context c) {
          this.mContext = c;
          //styleable resources
          TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
          mGalleryItemBackground = a.getResourceId(
            R.styleable.HelloGallery_android_galleryItemBackground,0);
            a.recycle();
        }
        // Get  number of items
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mImageIds.length; 
        }
 
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
 
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        //Return  view
        @Override
        public View getView(int position, View convertView,
                   ViewGroup parent) {
            // TODO Auto-generated method stub
            ImageView iv = new ImageView(mContext);
            iv.setImageResource(mImageIds[position]);
            iv.setId(mImageIds[position]); 
            iv.setLayoutParams(new Gallery.LayoutParams(120, 160)); 
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setBackgroundResource(mGalleryItemBackground);
            return iv;
        }
    }
     
}

Download Source Code: GalleryExample