|
相簿浏览 Gallery
Gallery 与衍生 BaseAdapter 容器
①新建项目
②定义 layout 外部 resource 的 xml 文件,用来改变 layout 的背景
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
<!-- 定义layout 外部resource 的xml 文件,用来改变layout 的背景图。 -->
</resources>
复制代码
③修改 main.xml 布局,添加一个 Gallery 和一个 ImageView
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget_absolutelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Gallery
android:layout_width="fill_parent"
android:layout_height="143px"
android:layout_x="0px"
android:layout_y="51px"
android:id="@+id/Gallery_preView">
</Gallery>
<ImageView
android:layout_width="239px"
android:layout_height="218px"
android:layout_x="38px"
android:layout_y="184px"
android:id="@+id/ImageView_photo">
</ImageView>
</AbsoluteLayout>
复制代码
④新建一个 myImageAdapter 类--Gallery 的适配器,它继承于 BaseAdapter 类
package zyf.Ex_Ctrl_10ME;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class myImageAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
}
复制代码
⑤修改 mainActivity.java,添加 Gallery 相关操作
package zyf.Ex_Ctrl_10ME;
import
import
import
import
import
import
import
android.app.Activity;
android.os.Bundle;
android.view.View;
android.widget.AdapterView;
android.widget.Gallery;
android.widget.ImageView;
android.widget.Toast;
public class Ex_Ctrl_10ME extends Activity {
/** Called when the activity is first created. */
/*定义要使用的对象*/
private Gallery gallery;
private ImageView imageview;
private myImageAdapter imageadapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageadapter=new myImageAdapter(this);
/* 通过findViewById 取得 资源对象*/
gallery=(Gallery)findViewById(R.id.Gallery_preView);
imageview=(ImageView)findViewById(R.id.ImageView_photo);
/*给Gallery设置适配器 把Ex_Ctrl_10ME类传入参数*/
gallery.setAdapter(imageadapter);
/*设置Gallery的点击事件监听器*/
gallery.setOnItemClickListener(new Gallery.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//
TODO Auto-generated method stub
/*显示该图片是几号*/
Toast.makeText(Ex_Ctrl_10ME.this,
"这是图片:"+position+"号", Toast.LENGTH_SHORT).show();
/*设置大图片*/
imageview.setBackgroundResource(imageadapter.myImageIds[position]);
}
});
}
}
复制代码
⑥修改 myImageAdapter.java 文件,实现相簿浏览效果
package zyf.Ex_Ctrl_10ME;
import
import
import
import
import
import
import
android.content.Context;
android.content.res.TypedArray;
android.view.View;
android.view.ViewGroup;
android.widget.BaseAdapter;
android.widget.Gallery;
android.widget.ImageView;
public class myImageAdapter extends BaseAdapter{//自定义的类变量
/*变量声明*/
int mGalleryItemBackground;
private Context context;//上下文
/* 构建一Integer array 并取得预加载Drawable 的图片id */
public Integer[] myImageIds = { R.drawable.photo1, R.drawable.photo2,
R.drawable.photo3, R.drawable.photo4, R.drawable.photo5,
R.drawable.photo6, };
/*自定义的构造方法*/
public myImageAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
/*
* 使用在res/values/attrs.xml 中的<declare-styleable>定义 的Gallery 属性.
*/
TypedArray
typed_array=context.obtainStyledAttributes(R.styleable.Gallery);
/* 取得Gallery 属性的Index id */
mGalleryItemBackground=typed_array.getResourceId(R.styleable.Gallery_andro
id_galleryItemBackground, 0);
/* 让对象的styleable 属性能够反复使用 */
typed_array.recycle();
}
/* 重写的方法getCount,返回图片数目 */
@Override
public int getCount() {
// TODO Auto-generated method stub
return myImageIds.length;
}
/* 重写的方法getItemId,返回图像的数组id */
@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;
}
/* 重写的方法getView,返回一View 对象 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
/* 产生ImageView 对象 */
ImageView imageview = new ImageView(context);
/* 设置图片给imageView 对象 */
imageview.setImageResource(myImageIds[position]);
/* 重新设置图片的宽高 */
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
/* 重新设置Layout 的宽高 */
imageview.setLayoutParams(new Gallery.LayoutParams(128, 128));
/* 设置Gallery 背景图 */
imageview.setBackgroundResource(mGalleryItemBackground);
/* 返回imageView 对象 */
return imageview;
}
}
复制代码结果:
|
|