标签:
public class ImageSwitcherActivity extends Activity implements OnClickListener,
ViewFactory {
// 所有要显示图片资源索引
private static final Integer[] imageList = { R.drawable.button1,
R.drawable.button2, R.drawable.button3, R.drawable.button31,
R.drawable.ic_launcher };
// 创建ImageSwitcher对象
private ImageSwitcher m_Switcher;
// 索引
private static int index = 0;
// “下一张”按钮ID
private static final int BUTTON_DWON_ID = 0X123456;
// "上一张"按钮ID
private static final int BUTTON_UP_ID = 0X123457;
// ImageSwitcher对象的ID
private static final int SWITCHER_ID = 0X123458;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 创建一个线性布局LinearLayout
LinearLayout main_view = new LinearLayout(this);
// 创ImageSwithcer对象
m_Switcher = new ImageSwitcher(this);
// 在线性布局中添加ImageSwitcher视图
main_view.addView(m_Switcher);
// 设置ImageSwither对象的ID
m_Switcher.setId(SWITCHER_ID);
// 设置ImageSwithcer对象的数据源
m_Switcher.setFactory(this);
m_Switcher.setImageResource(imageList[index]);
// 设置显示上面创建的线性布局
setContentView(main_view);
// 设置按钮位置属性
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
100, 100);
// 创建“下一张”按钮
Button next = new Button(this);
next.setId(BUTTON_DWON_ID);
next.setText("下一张");
next.setOnClickListener(this);
main_view.addView(next, param);
// 创建“上一张”按钮
Button pre = new Button(this);
pre.setId(BUTTON_UP_ID);
pre.setText("上一张");
pre.setOnClickListener(this);
main_view.addView(pre, param);
}
@Override
public View makeView() {
// 将所有图片通过ImageView来显示
return new ImageView(this);
}
// 事件监听、处理
@Override
public void onClick(View v) {
switch (v.getId()) {
// 下一页
case BUTTON_DWON_ID:
index++;
if (index >= imageList.length) {
index = 0;
}
// 设置ImageSwithcer对象资源索引
m_Switcher.setImageResource(imageList[index]);
break;
case BUTTON_UP_ID:
index--;
if (index < 0) {
index = imageList.length - 1;
}
// 设置ImageSwithcer对象资源索引
m_Switcher.setImageResource(imageList[index]);
break;
default:
break;
}
}
}
标签:
原文地址:http://www.cnblogs.com/Defry/p/4401779.html