วันพุธที่ 18 กันยายน พ.ศ. 2556

Android : เริ่มต้นเขียน โปรแกรม ImageContent

บทนี้ จะเป็นการทดลองเขียน Source Code และ การออกแบบหน้าจอ โปรแกรม ImageContent ซึ่งเป็นโปรแกรมสำหรับเก็บภาพและแสดงภาพ
     เรามาดูรายละเอียด Source Code การสร้างโปรแกรมกัน
โดยเราต้องสร้างๆไฟล์หลักๆ 3 ไฟล์ คือ Main.java , main.xml และ strings.xml

//Main.java
package com.test;


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.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class Main extends Activity {
// Step 1
Integer[] imageIds = {
R.drawable.pic1,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7
};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Step 4
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener() {
       
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        ImageView imageView = (ImageView) findViewById(R.id.image1);
        imageView.setImageResource(imageIds[position]);
        }
});
    }
    
    // Step 2
    public class ImageAdapter extends BaseAdapter{
    // Step 2.1
    private Context context;
    private int itemBackground;
    // Step 2.2
    public ImageAdapter(Context c){
    context = c;
    TypedArray arr = c.obtainStyledAttributes(R.styleable.Gallery1);
    itemBackground = arr.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
    arr.recycle();
    }
   
    @Override
    public int getCount() {
    return imageIds.length;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // Step 3
    ImageView imageView = new ImageView(context);
    imageView.setImageResource(imageIds[position]);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
    imageView.setBackgroundResource(itemBackground);
    return imageView;
    }

    }
}

-------------------------------------------------------------------------------------------------
//main.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Gallery>

<ImageView
android:id="@+id/image1"
android:layout_width="220dp"
android:layout_height="250dp"
android:scaleType="fitXY">
</ImageView>
</LinearLayout>
-------------------------------------------------------------------------------------------------
//string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Main!</string>
    <string name="app_name">ImageContent</string>
    
     <declare-styleable name="Gallery1">
    <attr name="android:galleryItemBackground"/>
    </declare-styleable>
</resources>
-------------------------------------------------------------------------------------------------
ทดลอง Run โปรแกรม