Gallery意思是"画廊",就是一个水平可左右拖动的列表,里面可以放置多个图片,经常和ImageSwitcher一起使用

在使用ImageSwitcher时,需要传入一个ViewFactory对象,并且需要给gallery设置数据适配器;   代码如下

public class MainActivity extends Activity {
int[] images = new int[] { R.drawable.shuangzi, R.drawable.shuangyu,
R.drawable.chunv, R.drawable.tiancheng, R.drawable.tianxie,
R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping,
R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu,
R.drawable.mojie };
private ImageSwitcher is; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1); is = (ImageSwitcher) findViewById(R.id.is); Gallery gallery = (Gallery) findViewById(R.id.gallery); is.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView iv = new ImageView(MainActivity.this);
iv.setScaleType(ScaleType.FIT_XY);
iv.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return iv;
}
});
gallery.setAdapter(new BaseAdapter() { @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(MainActivity.this);
iv.setImageResource(images[position % images.length]); iv.setScaleType(ScaleType.FIT_XY);
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery);
iv.setLayoutParams(new Gallery.LayoutParams(, )); iv.setBackgroundResource(ta.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, )); return iv;
} @Override
public long getItemId(int position) {
return position;
} @Override
public Object getItem(int position) {
return images[position];
} @Override
public int getCount() {
return images.length;
}
}); gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
is.setImageResource(images[position%images.length]);
} @Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}

上面getView方法作用是:显示画廊的水平列表
这里TypedArray作用是引入自定义控件的自定义属性

布局文件main.xml

<?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" > <ImageSwitcher
android:id="@+id/is"
android:layout_width="320dp"
android:layout_height="320dp" >
</ImageSwitcher> <Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:spacing="3dp"
android:unselectedAlpha="0.5" /> </LinearLayout>

自定义属性attrs.xml文件位于values目录下

<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable> </resources>

关于自定义属性参考文章:http://www.cnblogs.com/carlosk/archive/2012/06/06/2538336.html

http://blog.csdn.net/congqingbin/article/details/7869730

http://blog.csdn.net/tinafhx/article/details/5290878

最新文章

  1. c语言中遇到“警告: the `gets&#39; function is dangerous and should not be used.”的解决办法
  2. PyQt4软件打包成exe文件
  3. Linux内核分析之扒开系统调用的三层皮(上)
  4. Hello.class所在路径下, 输入命令:java Hello.class,会出现什么结果,为什么?
  5. 深入PHP内核之ZVAL
  6. RTC系统【转】
  7. javassist动态修改class
  8. 武汉Uber优步司机奖励政策(2月1日~2月7日)
  9. 如何判断网页中引入jquery
  10. Jquery EasyUI远程校验,Jquery EasyUI多个自定义校验,EasyUI自定义校验
  11. 第1章-Struts2 概述 --- Struts2和MVC
  12. Slim 文档-First Application 翻译
  13. locust压测websocket协议
  14. Mui中常见问题记录
  15. 探寻TP-Link路由器的登录验证
  16. mac安装Redis可视化工具-Redis Desktop Manager
  17. js call 理解
  18. Xeon Phi 《协处理器高性能编程指南》随书代码整理 part 3
  19. 解决Win10 Virtualbox5.2.18桥接不能联网小记
  20. 51Nod 1667 概率好题 - 容斥原理

热门文章

  1. Compatibility模式安装windows7后改为AHCI模式无法启动Windows7的解决办法
  2. Source Maps简介
  3. web项目jsp中无法引入js问题
  4. rwcheck:为嵌入式设备设计的读写压测工具
  5. net core WebApi——文件分片上传与跨域请求处理
  6. python实现RSA加密和签名以及分段加解密的方案
  7. 几图理解BeautifulSoup
  8. Go语言学习——如何实现一个过滤器
  9. 分布式任务调度框架 Azkaban —— Flow 1.0 的使用
  10. Python--函数参数类型、用法及代码示例