Android中Gallery(画廊)的使用
1、Gallery
1.1、使用BaseAdapter完成Gallery
思路:通过BaseAdapter类加载一个布局文件信息。把一个布局问价转换成一个View,加载到Gallery中。步骤:-->新建一个布局文件gallery_layout.xml,其中只有一个Gallery组件。-->新建一个布局文件gallery_item_layout.xml,其中只有一个ImageView组件。-->新建一个GalleryDemoActivity类,此类继承自Activity。-->新建一个GalleryDemoAdapter类,此类继承自BaseAdapter。1)、建立gallery_layout.xml,代码如下:2)、建立gallery_item_layout.xml,其中只有一个ImageView组件,该组件用于显示图片。<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Gallery android:id="@+id/gly_showglyimg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:spacing="2dp" /> </RelativeLayout>
3)、新建一个GalleryDemoAdapter,继承自BaseAdapter类,并覆写相应方法。<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/iv_showglyimg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> </LinearLayout>
4)、新建一个GalleryDemoActivity类继承自Activitypackage com.example.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import com.example.highcomponent.R; public class GalleryDemoAdapter extends BaseAdapter { //定义上下文Context,用于接收传过来的Activity类GalleryDemo2Activity private Context context; //定义资源文件resource,用于接收穿过来的gallery_layout.xml private int resource; //定义list集合,用于接收传过来的图片 private List<Integer> list; //定义一个视图容器,用于把一个布局文件转换为view private LayoutInflater inflater; //定义构造方法,接收相应参数并初始化 public GalleryDemo2Adapter(Context context,int resource,List<Integer> list){ this.context = context; this.resource = resource; this.list = list; inflater = LayoutInflater.from(context); } @Override public int getCount() { if(list!=null){ return list.size();//返回集合大小 } return 0; } @Override public Object getItem(int arg0) { return null; } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ convertView = inflater.inflate(resource, null);//布局文件转换为View } //声明一个图片视图,用于接收资源文件gallery_item_layout.xml的ImageView组件 ImageView iv_showimg =(ImageView)convertView.findViewById(R.id.iv_showglyimg); //把list集合中的图片设置在gallery_item_layout.xml的ImageView中 iv_showimg.setImageResource(list.get(position%list.size())); return convertView; } }
package com.example.highcomponent; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.Gallery; import com.example.adapter.GalleryDemo2Adapter; public class GalleryDemo2Activity extends Activity { private Gallery gly_showglyimg = null;// 声明Gallery控件 private List<Integer> list = new ArrayList<Integer>();// 定义list存储图片 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.gallery_layout);// 设置布局文件 gly_showglyimg = (Gallery) super.findViewById(R.id.gly_showglyimg);// 实例化组件 // 往list中添加图片 list.add(R.drawable.jaychou1); list.add(R.drawable.jaychou2); list.add(R.drawable.jaychou3); list.add(R.drawable.jaychou4); list.add(R.drawable.jaychou5); // 实例化adapter加载信息 GalleryDemo2Adapter adapter = new GalleryDemo2Adapter( GalleryDemo2Activity.this, R.layout.gallery_item_layout, list); // 设置adapter gly_showglyimg.setAdapter(adapter); } }
5)、Gallery实现图片切换,可以通过鼠标滑动,运行效果(静态截图):
1.2、使用BaseAdapter完成Gallery实例
需求:1、拖动图片的时候,单选按钮也默认选中相应的按钮。2、在单击单选按钮的时候,图片也跟着切换。
思路:通过BaseAdapter类加载一个布局文件信息。
步骤:-->新建一个布局文件gallery_radio_layout.xml,其中只有一个Gallery组件。-->沿用之前的布局文件gallery_item_layout.xml,其中只有一个ImageView组件。-->新建一个GalleryRadioDemoActivity类,此类继承自Activity。-->新建一个GalleryRadioDemoAdapter类,此类继承自BaseAdapter。1)、gallery_radio_layout.xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Gallery android:id="@+id/gly_showglyradioimg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:spacing="2dp" /> <RadioGroup android:id="@+id/rg_showglyradio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignRight="@id/gly_showglyradioimg" android:layout_alignBottom="@id/gly_showglyradioimg"> <RadioButton android:id="@+id/rb_radio1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/rb_radio2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/rb_radio3" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/rb_radio4" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/rb_radio5" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RadioGroup> </RelativeLayout>
2)、GalleryRadioDemoAdapter
package com.example.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import com.example.highcomponent.R; public class GalleryRadioDemoAdapter extends BaseAdapter { private Context context; // 声明上下文信息,用于接收传过来的Activity类,GalleryRadioDemoActivity private int resource; // 声明资源文件信息,用于接收传过来的R.layout.gallery_item_layout private List<Integer> list; // 声明list集合,用于接收传过来的list集合数据 private LayoutInflater inflater;// 声明布局容器 public GalleryRadioDemoAdapter(Context context, int resource, List<Integer> list) { this.context = context; this.resource = resource; this.list = list; inflater = LayoutInflater.from(context);// 初始化布局容器 } @Override public int getCount() { if (list != null) { return list.size();// 如果list集合不为空,则放回集合大小 } return 0; } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(resource, null);// 如果视图为空,则初始化视图 } // 初始化gallery_item_layout.xml中的组件 ImageView iv_showglyradioimg = (ImageView) convertView .findViewById(R.id.iv_showglyimg); // 设置图片 iv_showglyradioimg.setImageResource(list.get(position % list.size())); return convertView; } }
3)、GalleryRadioDemoAdapterpackage com.example.highcomponent; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Gallery; import android.widget.RadioButton; import android.widget.RadioGroup; import com.example.adapter.GalleryRadioDemoAdapter; public class GalleryRadioDemoActivity extends Activity { private Gallery gly_showglyradioimg = null; // 声明gallery组件 private RadioGroup rg_showglyradio = null; // 声明RadioGroup组件 private RadioButton rb_radio1 = null; // 声明RadioButton组件 private RadioButton rb_radio2 = null; // 声明RadioButton组件 private RadioButton rb_radio3 = null; // 声明RadioButton组件 private RadioButton rb_radio4 = null; // 声明RadioButton组件 private RadioButton rb_radio5 = null; // 声明RadioButton组件 private List<Integer> list = new ArrayList<Integer>();// 声明list集合,用于存储图片id @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.gallery_radio_layout);// 设置布局文件 // 实例化组件 gly_showglyradioimg = (Gallery) super .findViewById(R.id.gly_showglyradioimg); rg_showglyradio = (RadioGroup) super.findViewById(R.id.rg_showglyradio); rb_radio1 = (RadioButton) super.findViewById(R.id.rb_radio1); rb_radio2 = (RadioButton) super.findViewById(R.id.rb_radio2); rb_radio3 = (RadioButton) super.findViewById(R.id.rb_radio3); rb_radio4 = (RadioButton) super.findViewById(R.id.rb_radio4); rb_radio5 = (RadioButton) super.findViewById(R.id.rb_radio5); // 声明整型数组,用于存储图片,总共存储5张图片 int radioimg[] = { R.drawable.jaychou1, R.drawable.jaychou2, R.drawable.jaychou3, R.drawable.jaychou4, R.drawable.jaychou5, }; // 把数组中的图片添加到list集合中 for (int i = 0; i < radioimg.length; i++) { list.add(radioimg[i]); } // 调用Adapter类,加载信息 GalleryRadioDemoAdapter adapter = new GalleryRadioDemoAdapter( GalleryRadioDemoActivity.this, R.layout.gallery_item_layout, list); // 设置设置Adapter gly_showglyradioimg.setAdapter(adapter); // 为Gallery组件设置监听事件,当图片改变时,RadioButton也会跟着改变 gly_showglyradioimg .setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) { switch (position) { case 0: rb_radio1.setChecked(true); break; case 1: rb_radio2.setChecked(true); break; case 2: rb_radio3.setChecked(true); break; case 3: rb_radio4.setChecked(true); break; case 4: rb_radio5.setChecked(true); break; default: break; } } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); // 为RadioGroup设置事件,当单机RadioButton时,图片会跟着切换 rg_showglyradio .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rb_radio1: gly_showglyradioimg.setSelection(0); break; case R.id.rb_radio2: gly_showglyradioimg.setSelection(1); break; case R.id.rb_radio3: gly_showglyradioimg.setSelection(2); break; case R.id.rb_radio4: gly_showglyradioimg.setSelection(3); break; case R.id.rb_radio5: gly_showglyradioimg.setSelection(4); break; default: break; } } }); } }
4)、运行效果