Gallery和ImageView使用
来源:互联网 发布:python 函数声明 编辑:程序博客网 时间:2024/05/14 09:58
Gallery可以显示一系列的图片,并且可以横向滑动。下面展示如何使用Gallery去显示一系列的图片。
1. 创建一个工程,Gallery。
2. main.xml中的代码。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Images of San Francisco" />
- <Gallery
- android:id="@+id/gallery1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <ImageView
- android:id="@+id/image1"
- android:layout_width="320dp"
- android:layout_height="250dp"
- android:scaleType="fitXY" />
- </LinearLayout>
4. attrs.xml中的代码。
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="Gallery1">
- <attr name="android:galleryItemBackground" />
- </declare-styleable>
- </resources>
5. 准备一些图片。将这些图片放在res/drawable-mdpi下面。
6. GalleryActivity.java中的代码。
- public class GalleryActivity extends Activity {
- Integer[] imageIDs = {
- R.drawable.pic1,
- R.drawable.pic2,
- R.drawable.pic3,
- R.drawable.pic4,
- R.drawable.pic5,
- R.drawable.pic6,
- R.drawable.pic7
- };
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Gallery gallery = (Gallery) findViewById(R.id.gallery1);
- gallery.setAdapter(new ImageAdapter(this));
- gallery.setOnItemClickListener(new OnItemClickListener()
- {
- public void onItemClick(AdapterView<?> parent, View v,
- int position, long id)
- {
- Toast.makeText(getBaseContext(),
- "pic" + (position + 1) + " selected",
- Toast.LENGTH_SHORT).show();
- ImageView imageView = (ImageView) findViewById(R.id.image1);
- imageView.setImageResource(imageIDs[position]);
- }
- });
- }
- public class ImageAdapter extends BaseAdapter
- {
- Context context;
- int itemBackground;
- public ImageAdapter(Context c)
- {
- context = c;
- //---setting the style---
- TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
- itemBackground = a.getResourceId(
- R.styleable.Gallery1_android_galleryItemBackground, 0);
- a.recycle();
- }
- //---returns the number of images---
- public int getCount() {
- return imageIDs.length;
- }
- //---returns the item---
- public Object getItem(int position) {
- return position;
- }
- //---returns the ID of an item---
- public long getItemId(int position) {
- return position;
- }
- //---returns an ImageView view---
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView;
- if (convertView == null) {
- imageView = new ImageView(context);
- imageView.setImageResource(imageIDs[position]);
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
- } else {
- imageView = (ImageView) convertView;
- }
- imageView.setBackgroundResource(itemBackground);
- return imageView;
- }
- }
- }
7. 按F11在模拟器上面调试。会看见一系列的图片,这些图片可以左右滑动。当单击单个图片的时候,会弹出消息。
首先,我们在main.xml中添加Gallery和ImageView控件:
- <Gallery
- android:id="@+id/gallery1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <ImageView
- android:id="@+id/image1"
- android:layout_width="320dp"
- android:layout_height="250dp"
- android:scaleType="fitXY" />
这些图片的id被保存在imageIDs数组中:
- Integer[] imageIDs = {
- R.drawable.pic1,
- R.drawable.pic2,
- R.drawable.pic3,
- R.drawable.pic4,
- R.drawable.pic5,
- R.drawable.pic6,
- R.drawable.pic7
- };
使用BaseAdapter的视图的还有:
- ListView
- GridView
- Spinner
- Gallery
BaseAdapter也有一些子类:
- ListAdapter
- ArrayAdapter
- CursorAdapter
- SpinnerAdapter
在ImageAdapter中我们主要实现以下的方法:
- public class ImageAdapter extends BaseAdapter
- {
- Context context;
- int itemBackground;
- public ImageAdapter(Context c)
- {
- context = c;
- //---setting the style---
- TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
- itemBackground = a.getResourceId(
- R.styleable.Gallery1_android_galleryItemBackground, 0);
- a.recycle();
- }
- //---returns the number of images---
- public int getCount() {
- return imageIDs.length;
- }
- //---returns the item---
- public Object getItem(int position) {
- return position;
- }
- //---returns the ID of an item---
- public long getItemId(int position) {
- return position;
- }
- //---returns an ImageView view---
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView;
- if (convertView == null) {
- imageView = new ImageView(context);
- imageView.setImageResource(imageIDs[position]);
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
- } else {
- imageView = (ImageView) convertView;
- }
- imageView.setBackgroundResource(itemBackground);
- return imageView;
- }
- }
0 0
- Gallery和ImageView使用
- ImageView和Gallery的使用
- 03 Android Gallery 和 ImageView 的组合使用
- android Gallery与ImageView的结合使用
- 用Gallery和ImageView实现图片滑动
- 【Android 开发教程】Gallery和ImageView
- 利用Gallery和ImageView实现图片浏览器
- ImageSwitcher 和 Gallery 的使用
- ImageSwitcher和Gallery的使用
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- Gallery+ImageView实现图片浏览
- Android -- (10),Gallery ImageView
- RelativeLayout和ImageView 使用体验
- ImageButton和ImageView的使用
- Android ImageSwitcher和Gallery 综合使用
- Android ImageSwitcher和Gallery 综合使用
- Android 中的ImageSwitcher 和Gallery结合使用
- “大数据、云计算”决胜企业网络安全
- 使用Homebrew编译iOS linphone SDK
- NPOI读写Excel
- 苹果应用审核指南及翻译版地址
- ios unit test IDEBundleInjection.c: Error 3585
- Gallery和ImageView使用
- VS2013中集成QT 5.3.2
- 八皇后代码--九度oj-1140
- 使安卓/system目录变为可写
- java设计模式--代理模式
- vbox 安装增强功能失败,连不上cn.archive.ubuntu.com的问题
- Android系统的震动demo
- windows下用eclipse+goclipse插件+gdb搭建go语言开发调试环境
- Apple Watch、Watch Kit