首页 技术 正文
技术 2022年11月8日
0 收藏 379 点赞 1,270 浏览 2737 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,122
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,594
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,439
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,210
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,846
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,931