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