ウェブアプリケーション,インジェクション,コマンドインジェクション

Androidアプリ開発 画像とアイテムのリストを表示していく

画像のリストを表示する

Displaying a List of Images

ListViewを使ってアイテムのリストを表示していく
ListViewを改良する
複数のListViewを表示していく
改良ListViewを作っていく
追加のTextViewでそれぞれの行をさらに改良
SpinnerViewを使ってアイテムのリストを表示していく
と紹介してきまして、今のところ上記の内容は、
主にテキスト、たまに画像を表示することに関わったものになります。
もし開発者が一連の画像をユーザに見せたかったら、
Calleryを使用することができます。
Galleryは中央にロックされ、水平にスクロールするリストにある
画像アイテムを見せるViewで、ここではこのGallaryをどのように
使うかを示します。
ここでは、プロジェクトのres/drawable-mdpiフォルダの中に
いくつかの画像を保存してあると想定します。
attrs.xmlというXMLファイルを作りres/valueフォルダに保存します。
} attrs.xmlの内容は下記のようになります。

<resources>
  <declare-styleable name="MyGallely">
   <attr name="android:gallelyItemBackgroud" />
  </declare-styleable>
<:/resources>

Gallelyを使うためには、<Gallaey>要素を
使うユーザインターフェースに加え、以下のような
activity_main.xmlファイルにします。

<LinearLayout>
  xmlns:androud="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <Gallery
   android:id="@+id/gallery1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" />

  <ImageView
   android:id="@+id/image1"
   android:layout_width="fill_parent"
   android:layout_height="250dp"
   android:scaleType="fitXY" />
<LinearLayout>

アクティビティでは、以下のようなコードをラインに加えます。

package net.lean2develop.gallery;

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;

//---The Gallery view is deprecated in Android 4.1;
//however it is still a usefull view
//The following statement suppresses the compiler warning---
@SuppressWarnings("deprecation")
public class MainActivity extends Activity{
  //---the images to display---
  Integet[] imageIDs = {
        R.drawable.pic1;
        R.drawable.pic2;
        R.drawable.pic3;
        R.drawable.pic4;
        R.drawable.pic5;
        R.drawable.pic6;
        R.drawable.pic7;
  };

  @Override
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   //---Note that Gallery view is deprecated in Android 4.1---
   Gallery gallery = (Gallery) findViewById(R.id.gallery1);
   gallery.setAdapter(new.ImageAdapter(this));
   gallery.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View v, int position,
    long id) {
     Toast.makeText(getBaseContext(),
        "pic" + (position + 1) + " selected",
        Toast.LENGTH_SHORT).show();

     //---display the images selected---
     ImageView imageView = (ImageView) findViewById(R.id.image1);
     imageView.setImageResource(imageIDs[position]);
    }
   });
  }

  public class ImageAdapter extends BaseAdapter{
   private Context context;
   private int itemBackgroud;
   public ImageAdapter(Context c){
    context = c;

    //---sets a grey backgroud;wraps around the images
    TypeArray a =
     obtainStyleAttrivutes(R.styleable.MyGallery);
    itemBackground = a.getResourceId(
     R.styleable.MyGallery_android_galleryItemBackground, 0);
    a.recycle();
   }

   //---return the number of images---
   public int getCount(){
     return imageIDs.length;
   }

   //---return the ID of an item---
   public Object getItem(int position){
    return position;
   }

   //---return the ID of an item---
   public long getItemId(int position){
    return position;

   //---returns an ImageView view---
   public View getView(int pocition, View convertView, ViewGroup parent){
    ImageView imageView = new ImageView(context);
    imageView.setImageResource(imageIDs[position]);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setLayoutParams(new Gallery.LayoutParams(150.120));
    return imageView;
   }
  }
}

まず、BaseAdapterクラスを拡張したImageAdapterクラスを
一連のImageViewのviewでGallery viewをバインドさせるために
使います。
BaseAdapterクラスはAdapterViewとデータがフィードされる
データソースを橋渡しするもので、
例としては以下のようなものがあります。
●ListView
●GridView
●Spinner
●Gallery
そしてAndroidには以下の様なBaseAdapterのサブクラスがあります。
●ListAdapter
●ArrayAdapter
●CursorAdapter
●SpinnerAdapter
ImageAdapterクラスのため以下のメソッドを実行させます。
●getCount()
●getItem()
●getItemId()
●getView()
その中でも特に、getView()メソッドは指定されたポジションの
Viewを返します。
上にあったコードの場合、ImageViewオブジェクトを返してます。
Galleryにある画像が選択された(クリックされた)時には、
選択された画像のポジションが表示され、
ImageViewの中に画像が表示されます。



ホーム
便利堂ロゴ
inserted by FC2 system