import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitcherTest extends Activity {
private ImageSwitcherTest ist=this;//ist
private LinearLayout mainLayout=null;//定义一个线性布局,主布局
private ImageSwitcher is=null;//定义图片切换控件
private RelativeLayout layout=null;//定义一个相对布局,用于盛放两个按钮
private TextView tv=null;//定义一个文本
private Button preButton=null;//按钮,上一张
private Button nextButton=null;//按钮,下一张
private int[] imageList={ R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4};//图片资源数组
private int index=0;//图片索引
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
mainLayout_init();
setContentView(mainLayout);
}
/*mianLayout初始化*/
void mainLayout_init(){
mainLayout=new LinearLayout(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-1);
mainLayout.setLayoutParams(lp);
mainLayout.setOrientation(LinearLayout.VERTICAL);
is_init();
mainLayout.addView(is);
layout_init();
mainLayout.addView(layout);
}
/*is初始化*/
void is_init(){
is=new ImageSwitcher(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);
is.setLayoutParams(lp);
is.setFactory(new ViewFactory(){
/*
* 此处ViewFacktroy是一个接口,类似于OnClickeLisnter之类的
* 我们可以新建一个ViewFactroy,也可以让Activity implements ViewFactory
*/
public View makeView() {
ImageView iv=new ImageView(ist);
iv.setScaleType(ScaleType.FIT_CENTER);
iv.setLayoutParams(new ImageSwitcher.LayoutParams(-1,-1));
return iv;
}
});
is.setImageResource(imageList[index]);
}
/*layout初始化*/
void layout_init(){
layout=new RelativeLayout(this);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-1,-2);
layout.setLayoutParams(lp);
tv_init();
layout.addView(tv);
preButton_init();
layout.addView(preButton);
nextButton_init();
layout.addView(nextButton);
}
/*tv初始化*/
void tv_init(){
tv=new TextView(this);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2,-2);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
tv.setLayoutParams(lp);
tv.setText(String.valueOf(index+1)+"/4");
}
/*preButton初始化*/
void preButton_init(){
preButton=new Button(this);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2,-2);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
preButton.setLayoutParams(lp);
preButton.setText("上一张");
preButton.setEnabled(false);
preButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(index==3){
nextButton.setEnabled(true);
}
if(index>0){
index--;
is.setImageResource(imageList[index]);
if(index==0){
preButton.setEnabled(false);
}
}
tv.setText(String.valueOf(index+1)+"/4");
}
});
}
/*nextButton初始化*/
void nextButton_init(){
nextButton=new Button(this);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2,-2);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
nextButton.setLayoutParams(lp);
nextButton.setText("下一张");
nextButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(index==0){
preButton.setEnabled(true);
}
if(index<3){
index++;
is.setImageResource(imageList[index]);
if(index==3){
nextButton.setEnabled(false);
}
}
tv.setText(String.valueOf(index+1)+"/4");
}
});
}
}