WEB开发网
开发学院手机开发Android 开发 可循环显示图像的Android Gallery组件 阅读

可循环显示图像的Android Gallery组件

 2010-08-24 05:24:00 来源:WEB开发网   
核心提示:在本例的main.xml文件中配置了一个Gallery组件,代码如下:代码< ?xml version="1.0" encoding="utf-8"?>< LinearLayout xmlns:android="http://schemas.andro

在本例的main.xml文件中配置了一个Gallery组件,代码如下:

代码

< ?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">

< Gallery android:id="@+id/gallery" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:layout_marginTop="30dp" />

< /LinearLayout>

现在在onCreate方法中装载这个组件,代码如下:

代码

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// 装载Gallery组件

Gallery gallery = (Gallery) findViewById(R.id.gallery);

// 创建用于描述图像数据的ImageAdapter对象

ImageAdapter imageAdapter = new ImageAdapter(this);

// 设置Gallery组件的Adapter对象

gallery.setAdapter(imageAdapter);

}

在上面的代码中涉及到一个非常重要的类:ImageAdapter。该类是android.widget.BaseAdapter的子类,用于描述图像信息。下面先看一下这个类的完整代码。

代码

public class ImageAdapter extends BaseAdapter

{

int mGalleryItemBackground;

private Context mContext;

public ImageAdapter(Context context)

{

mContext = context;

// 获得Gallery组件的属性

TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);

mGalleryItemBackground = typedArray.getResourceId(

R.styleable.Gallery_android_galleryItemBackground, 0);

}

// 返回图像总数

public int getCount()

{

return resIds.length;

}

public Object getItem(int position)

{

return position;

上一页  1 2 3 4 5 6 7  下一页

Tags:循环 显示 图像

编辑录入:coldstar [复制链接] [打 印]
赞助商链接