android 纯java_Android入门之ImageSwitcher(纯java)

0_1315723755SijD.gif    

0_1315723759f9H9.gif

0_1315723762vW3i.gif    

0_1315723764PnB9.gif

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");

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值