博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
阅读量:6159 次
发布时间:2019-06-21

本文共 3404 字,大约阅读时间需要 11 分钟。

xml文件

1 
2
10
17 18
26

Java文件

1 public class ChangeTheme extends Activity { 2     //定义图片资源 3     private int[] images = new int[]{ 4        R.drawable.paper_01, 5        R.drawable.paper_02, 6        R.drawable.paper_03, 7        R.drawable.paper_04, 8        R.drawable.paper_05, 9        R.drawable.paper_06,10        R.drawable.paper_0711        12     };13  14     ImageSwitcher switcher ; //声明ImageSwitcher对象,图片显示区域15     Gallery gallery;   //声明Gallery对象,图片列表索引16     @Override17     public void onCreate(Bundle savedInstanceState) {18          super.onCreate(savedInstanceState);19          setContentView(R.layout.theme_activity);20        21         //获取对象22          switcher = (ImageSwitcher)this.findViewById(R.id.switcher);23          gallery = (Gallery)this.findViewById(R.id.gallery);24        25        26          switcher.setFactory(new ViewFactory(){27             public View makeView() {28                  ImageView imageView = new ImageView(ChangeTheme.this);29                  imageView.setBackgroundColor(0x00ff00);30                  imageView.setScaleType(ImageView.ScaleType.CENTER);31 //                 imageView.setLayoutParams(new ImageSwitcher.LayoutParams32 //                          (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));33                  34                  imageView.setLayoutParams(new ImageSwitcher.LayoutParams(35                          LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));36                  return imageView;37             }38         });39         //设置切换动画效果40         switcher.setInAnimation(this, android.R.anim.slide_in_left);41         switcher.setOutAnimation(this, android.R.anim.fade_out);42        43         //创建Gallery的adapter44         BaseAdapter adapter = new BaseAdapter(){45             public int getCount() {46                 return images.length;47             }48             public Object getItem(int position) {49                 return images[position];50             }51             public long getItemId(int position) {52                 return position;53             }54             //该方法返回的view代表每个列表项55            56             public View getView(int position, View convertView, ViewGroup parent) {57                 ImageView imageView = new ImageView(ChangeTheme.this);58                 imageView.setImageResource(images[position%images.length]);59                 imageView.setScaleType(ScaleType.FIT_XY);60                 imageView.setLayoutParams(new Gallery.LayoutParams(75, 100));61                 //设置自定义属性62                 /*TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery);63                 imageView.setBackgroundResource(typeArray.getResourceId(64                             R.styleable.Gallery_android_galleryItemBackground, 0));*/65                 return imageView;66            }67         };68        69         gallery.setAdapter(adapter);70         //Gallery便表现选择时间71         gallery.setOnItemSelectedListener(new OnItemSelectedListener(){72            public void onItemSelected(AdapterView
parent, View v, int position,long id) {73 switcher.setImageResource(images[position % images.length]);74 }75 public void onNothingSelected(AdapterView
arg0) {}76 });77 }78 79 }

 

转载于:https://www.cnblogs.com/zjqlogs/archive/2012/12/25/2832580.html

你可能感兴趣的文章
Javascript Ajax 异步请求
查看>>
DBCP连接池
查看>>
cannot run programing "db2"
查看>>
mysql做主从relay-log问题
查看>>
Docker镜像与容器命令
查看>>
批量删除oracle中以相同类型字母开头的表
查看>>
Java基础学习总结(4)——对象转型
查看>>
BZOJ3239Discrete Logging——BSGS
查看>>
SpringMVC权限管理
查看>>
spring 整合 redis 配置
查看>>
cacti分组发飞信模块开发
查看>>
浅析LUA中游戏脚本语言之魔兽世界
查看>>
飞翔的秘密
查看>>
Red Hat 安装源包出错 Package xxx.rpm is not signed
查看>>
编译安装mysql-5.6.16.tar.gz
查看>>
活在当下
查看>>
每天进步一点----- MediaPlayer
查看>>
PowerDesigner中CDM和PDM如何定义外键关系
查看>>
跨域-学习笔记
查看>>
the assignment of reading paper
查看>>