[size=large][color=red][b]Gallery 可以实现相册浏览效果:画廊[/b][/color][/size]
[size=medium][color=blue][b]布局文件:[/b][/color][/size]
[size=large][color=blue][b]主程序入口:[/b][/color][/size]
[size=large][color=darkblue][b]适配器:[/b][/color][/size]
[size=medium][color=blue][b]布局文件:[/b][/color][/size]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/myImg"
android:layout_width="250dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher" />
<!-- 图片浏览组件,android:spacing 设置图片间距 -->
<Gallery
android:id="@+id/mygallery"
android:layout_width="fill_parent"
android:layout_marginTop="20dp"
android:spacing="10dp"
android:gravity="center"
android:layout_height="100dp" />
</LinearLayout>
[size=large][color=blue][b]主程序入口:[/b][/color][/size]
package com.example.advancedview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageView;
/**
* Gallery :相簿浏览
* 实现步骤:
* 1、初始化Gallery 组件
* 2、创建一个新的Adapter,继承BaseAdapter ,这个新的Adapter 负责获取图片资源,图片名字、尺寸等信息;
* 并通过重写getView 方法,实现设置图片的尺寸以及显示方式;
* 3、为Gallery 组件添加Adapter,为新构建的类
* 4、通过Gallery的setOnItemClickListener 实现单机图片的效果
* @author Administrator
*
*/
public class GalleryActivity extends Activity {
private Gallery myGallery ;
private ImageView myImg;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_layout);
myGallery = (Gallery) findViewById(R.id.mygallery);
myImg = (ImageView) findViewById(R.id.myImg);
try {
myGallery.setAdapter(new ImageAdapter(this));// 添加适配器
myGallery.setOnItemClickListener(listener);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
// 图片点击事件
private OnItemClickListener listener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
GalleryActivity.this.setTitle(String.valueOf(position));
try{
myImg.setImageResource(new ImageAdapter(GalleryActivity.this).myImgList.get(position).intValue());
}catch(Exception ex){
ex.printStackTrace();
}
}
};
}
[size=large][color=darkblue][b]适配器:[/b][/color][/size]
package com.sun.gallery;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/**
* 图片适配器 -- 结合 画廊进行使用
* @author Administrator
*
*/
public class ImageAdapter extends BaseAdapter {
private int[] images; // 数据源
private Context context;
public ImageAdapter(Context context,int[] images) {
super();
this.context = context;
this.images = images;
}
// 数量
public int getCount() {
return images.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
return position;
}
// 显示的View
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context); // 创建一个ImageView视图
imageView.setImageResource(images[position]); // 为当前对象设置图片资源
imageView.setScaleType(ImageView.ScaleType.FIT_XY); // 图片的布局方式
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
return imageView;
}
}