Android(安卓)UI开发第七篇之Android(安卓)Gallery
16lz
2021-01-24
新建项目
② 定义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 {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubreturn null;}}
⑤ 修改mainActivity.java,添加Gallery相关操作
package zyf.Ex_Ctrl_10ME;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.ImageView;import android.widget.Toast;public class Ex_Ctrl_10ME extends Activity { /*定义要使用的对象*/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(newGallery.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 android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import 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) {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_android_galleryItemBackground, 0);/* 让对象的styleable 属性能够反复使用 */typed_array.recycle();}/* 重写的方法getCount,返回图片数目 */@Overridepublic int getCount() {return myImageIds.length;}/* 重写的方法getItemId,返回图像的数组id */@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}/* 重写的方法getView,返回一View 对象 */@Overridepublic View getView(int position, View convertView, ViewGroup parent) {/* 产生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;}}
更多相关文章
- android 向数据库写入图片信息 读取图片信息
- Android(安卓)Studio导入eclipse项目AAPT err(Facade for 164100
- Android(安卓)matrix 控制图片的旋转、缩放、移动
- Context的子类实现:ContextImpl的初始化
- android中保存Bitmap图片到指定文件夹中的方法
- Android(安卓)N NotificationManagerService源码分析
- Android(安卓)跨进程通信(二)
- Android使用Jsoup解析Html表格的方法
- Android(安卓)intent传递hashMap对象,遍历hashMap,改变menu状态