Android实战简易教程-第十枪(画廊组件Gallery实用研究)
2015-06-05 15:23
651 查看
Gallery组件用于拖拽浏览图片,下面我们就来看一下如何实现。
2.自定义适配器类,可以直接覆写BaseAdapter类中的几个方法。
3.MainActivity.java:
4.运行实例如下:
1.布局文件:
2.定义显示模板:
3.MainActivity.java程序:
4.运行实例:
一、实现Gallery
1.布局文件很简单:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MyLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <Gallery android:id="@+id/myGallery" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
2.自定义适配器类,可以直接覆写BaseAdapter类中的几个方法。
package org.yayun.demo; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Gallery.LayoutParams; public class ImageGalleryAdapter extends BaseAdapter { private Context context; private int imgRes[] = new int[] { R.drawable.ispic_a, R.drawable.ispic_b, R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e, }; public ImageGalleryAdapter(Context c) {//构造方法,用于获取上下文对象 this.context = c; } public int getCount() { return imgRes.length; } public Object getItem(int position) { return imgRes[position]; } public long getItemId(int position) { return imgRes[position]; } public View getView(int position, View convertView, ViewGroup parent) { ImageView img = new ImageView(this.context); img.setBackgroundColor(0xFFFFFFFF); img.setImageResource(this.imgRes[position]);//设置资源 img.setScaleType(ImageView.ScaleType.CENTER);//居中显示 img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return img; } }
3.MainActivity.java:
package org.yayun.demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; public class MainActivity extends Activity { private Gallery gallery; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 super.setContentView(R.layout.main); // 设置要使用的布局管理器 gallery = (Gallery) findViewById(R.id.myGallery); gallery.setAdapter(new ImageGalleryAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "您选择了第" + String.valueOf(position + 1) + "张图片", Toast.LENGTH_SHORT).show(); } }); } }
4.运行实例如下:
二、Gallery和ImageSwitcher结合
这时的Gallery我们用SimpleAdapter类完成。1.布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MyLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:gravity="bottom" android:orientation="vertical" > <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ImageSwitcher> <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:spacing="5dp" /> </LinearLayout>
2.定义显示模板:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF" android:orientation="horizontal" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center" /> </LinearLayout>
3.MainActivity.java程序:
package org.yayun.demo; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.SimpleAdapter; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery.LayoutParams; import android.widget.Gallery; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { private Gallery gallery; private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>(); private SimpleAdapter simpleAdapter; private ImageSwitcher imageSwitcher; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 super.setContentView(R.layout.main); // 设置要使用的布局管理器 initAdapter(); gallery = (Gallery) findViewById(R.id.gallery); imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); imageSwitcher.setFactory(new ViewFactory() { public View makeView() { ImageView imageView = new ImageView(MainActivity.this); imageView.setBackgroundColor(0xFFFFFFFF); imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return imageView; } }); gallery.setAdapter(simpleAdapter); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String, Integer> map = (Map<String, Integer>) MainActivity.this.simpleAdapter .getItem(position);// 取出map MainActivity.this.imageSwitcher.setImageResource(map.get("img"));// 设置显示图片 } }); } private void initAdapter() { Field[] fields = R.drawable.class.getDeclaredFields();// Java反射机制获取所有资源图片 for (int i = 0; i < fields.length; i++) { if (fields[i].getName().startsWith("ispic_")) {// 判断开头 Map<String, Integer> map = new HashMap<String, Integer>(); try { map.put("img", fields[i].getInt(R.drawable.class)); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.list.add(map); } } simpleAdapter = new SimpleAdapter(this, this.list, R.layout.grid_layout, new String[] { "img" }, new int[] { R.id.img }); } }
4.运行实例:
相关文章推荐
- Android 的布局文件的介绍和每个属性的详解
- Android中通过注解代替findViewById方法
- Android—点击自定义dialog窗体的控件销毁自己
- Android intent属性之Action
- Android 点击两次退后按钮程序退出
- Android intent属性之component
- Android, Ubuntu连接真机测试出现感叹号
- Android读程序包的资源
- Android属性动画完全解析(上),初识属性动画的基本用法
- Android ActionBar使用方法
- Android:简易弹幕效果实现
- android布局属性
- android 小技巧,不断更新中
- Android4.4----Vold挂载管理分析USB挂载(四)
- android设置背景色为透明
- android paint 研究总结
- 中国气象网 气象数据开放平台 API使用方法 (Android)
- android 无法生成R文件的原因剖析
- Android中滑屏实现----手把手教你如何实现触摸滑屏以及Scroller类详解
- Android定位开发之百度定位、高德定位、腾讯定位,三足鼎立一起为我所用!