首页 技术 正文
技术 2022年11月6日
0 收藏 315 点赞 1,088 浏览 2900 个字

网格布局GridView和Listview很相似,只不过前者是多列的.如果把GridView的android:numColumns设置为1,他就是ListView了.

通过GridView和ImageSwitcher以及ImageView做一个”带有预览的图片浏览器”

ImageSwitcher和ImageView十分相似,只不过比后者多了一个功能—它所显示的图片切换时可以设置动画效果

使用ImageSwitcher时,需要给他设置ImageSwitcher.ViewFactory, 实现ImageSwitcher.viewFactory时,需要实现一个makeView()方法, 该方法通常返回一个ImageView对象, 而ImageSwitcher则负责显示这个Imageview对象.

<?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" > <GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="" >
</GridView> <ImageSwitcher
android:id="@+id/is"
android:layout_width="320dp"
android:layout_height="320dp" >
</ImageSwitcher></LinearLayout>
<?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" > <ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /></LinearLayout>

主程序如下

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid); GridView gv = (GridView) findViewById(R.id.gv);
final ImageSwitcher is = (ImageSwitcher) findViewById(R.id.is); final int[] images = new int[] { R.drawable.bomb5, R.drawable.bomb6,
R.drawable.bomb7, R.drawable.bomb8, R.drawable.bomb9,
R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12,
R.drawable.bomb13, R.drawable.bomb14, R.drawable.bomb15,
R.drawable.bomb16 }; List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for (int i = ; i < images.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", images[i]);
data.add(map);
} // 设置动画
is.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
is.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out)); // 设置图片切换效果
is.setFactory(new ViewFactory() { @Override
public View makeView() {
ImageView iv = new ImageView(MainActivity.this);
iv.setBackgroundColor(0xff0000); // 设置背景色
iv.setScaleType(ScaleType.FIT_CENTER);// 设置缩放类型
iv.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return iv;
}
}); //在适配器中引入 cell布局文件,
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.cell,
new String[] { "image" }, new int[] { R.id.image}); gv.setAdapter(adapter); gv.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
is.setImageResource(images[position % images.length]); }
}); gv.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) {
}
}); }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905