AndroidDevelopersのチュートリアルのGalleryを実行します。
【手順1】
1.「Galleryのチュートリアル」にアクセス。
2.「sample images」をクリック。
【手順2】
1.ファイルの保存先を選択。
2.「保存」ボタンをクリック。
【手順3】
1.ダウンロードした「sample_images.zip」を選択し、WinRARの「ここに解凍」を選択。
【手順4】
1.「sample_images」に解凍される。
【手順5】
1.「Androidプロジェクトの作成方法」の手順で、「AndroidSample023-HelloGallery」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
2.「res/drawable/ディレクトリの作成方法」の手順で、「res/drawable/」ディレクトリを作成。
3.「res/drawable/」を選択し、「Ctrl+V」でコピーした画像を貼り付け。
4.「res/layout/main.xml」を以下の様に入力。
<?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="wrap_content" />
5.「任意のファイルの作成方法」の手順で、「res/values/」ディレクトリに「attrs.xml」というファイルを作成。
6.「attrs.xml」を以下の様に入力。
<?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="wrap_content" />
7.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング。
8.「HelloGallery.java」を以下の様に入力。
package com.example.androidsample024;
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;
import android.widget.Toast;
public class HelloGallery extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.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.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;
            TypedArray attr = mContext
                    .obtainStyledAttributes(R.styleable.HelloGallery);
            mGalleryItemBackground = attr.getResourceId(
                    R.styleable.HelloGallery_android_galleryItemBackground, 0);
            attr.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 imageView = new ImageView(mContext);
            imageView.setImageResource(mImageIds[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundResource(mGalleryItemBackground);
            return imageView;
        }
    }
}
9.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング。10.「Androidプロジェクトの実行方法」の手順で、「Androidプロジェクト」を実行。
11.エミュレータが起動しロックを解除し、以下の様にアプリケーションが実行されれば成功です。
以上です。
Gallery-01.png)
Gallery-02.png)
Gallery-03.png)
Gallery-04.png)
Gallery-05.png)
0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。