易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Android入门之ImageSwitcher(纯Java)
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"
);
}
});
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图