GalleryProject 畫廊

MyDemo.java

package com.jackie.galleryproject;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;


public class MyDemo extends Activity {
private Gallery gallery=null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.gallery=(Gallery) super.findViewById(R.id.gallery);
this.gallery.setAdapter(new ImageGalleryAdapter(this));
this.gallery.setOnItemClickListener(new OnItemClickListenerImpl());
}
    
private class OnItemClickListenerImpl implements OnItemClickListener{


@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
   Toast.makeText(MyDemo.this, String.valueOf(position), Toast.LENGTH_SHORT).show();

}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_demo, menu);
return true;
}

}

ImageGalleryAdapter

package com.jackie.galleryproject;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;


public class ImageGalleryAdapter extends BaseAdapter {  //負責Gallery 組件數據填充的適配器類就完成了
private Context context=null;
private int[] imgRes = new int[] { R.drawable.pic_0, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
R.drawable.pic_7, R.drawable.pic_8 };
public ImageGalleryAdapter(Context context){
this.context=context;
}


@Override
public int getCount() {
return this.imgRes.length;
}


@Override
public Object getItem(int position) {

return this.imgRes[position];
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return this.imgRes[position];
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img=new ImageView(this.context);
img.setBackgroundColor(0xFFFFFFFF);
img.setImageResource(this.imgRes[position]);
img.setScaleType(ImageView.ScaleType.CENTER);
img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return img;
}


}

Main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyDemo" >


    <Gallery
        android:id="@+id/gallery"
        android:gravity="center_vertical"
        android:spacing="3dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />


</LinearLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章