首先,创建一个简单的只包含gallery的布局文件grallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="30dp" />
</LinearLayout>
然后在创建一个相关联的Activity类GralleryDemo,在gallery里面通过setAdapter方法用自定义的ImageAdapter加载进布局
package com.example.grallerydemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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 GralleryDemo extends Activity {
private Gallery gallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grallery);
/**
* 初始化gallery
*/
gallery=(Gallery) findViewById(R.id.gallery);
try {
gallery.setAdapter(new ImageAdapter(GralleryDemo.this));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
//单击事件
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int itemid,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(GralleryDemo.this, ""+itemid,Toast.LENGTH_LONG).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.grallery_demo, menu);
return true;
}
}
最后,设计好你自己定义的ImageAdapter就OK了。
package com.example.grallerydemo;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
public class ImageAdapter extends BaseAdapter {
private Context context;
private Integer image[]={
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher
};
public ImageAdapter(Context c) {
// TODO Auto-generated constructor stub
context=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//return image.length;
return Integer.MAX_VALUE;
}
@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;
}
@Override
public int getItemViewType(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageview = new ImageView(context);
//给imageView设置图像资源
//imageview.setImageResource(image[position]);
imageview.setImageResource(image[position%image.length]);
//设置比例类型
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
//设置图像布局和显示大小
imageview.setLayoutParams(new Gallery.LayoutParams(100, 100));
//设置图像之间的距离
imageview.setPadding(15, 0, 15, 0);
return imageview;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void registerDataSetObserver(DataSetObserver arg0) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver arg0) {
// TODO Auto-generated method stub
}
@Override
public View getDropDownView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return null;
}
}
最终是循环显示,效果如下图