最近,做项目,发现很多软件,都到更换图标,一个切换效果,从上到下切换之类的。于是用ImageSwicher实现了下。比较简单。
直接看代码。(当然我还没放进项目里。测试了效果还是不错。)
直接看代码。(当然我还没放进项目里。测试了效果还是不错。)
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
使
用
自
定
义View
<
com.
example.
MyImageSwitcher
android:id
=
"@+id/imageSwitcher"
android:layout_width
=
"100dp"
android:layout_height
=
"100dp"
android:layout_marginTop
=
"10dp"
/>
<
Button
android:id
=
"@+id/btn1"
android:layout_below
=
"@+id/imageSwitcher"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:layout_marginTop
=
"150dp"
android:text
=
"Next"
/>
</
RelativeLayout
>
public
class
MyImageSwitcher
extends
ImageSwitcher {
private
Context
context;
public
MyImageSwitcher(
Context
context) {
super(
context);
// TODO Auto-generated constructor stub
}
public
MyImageSwitcher(
Context
context,
AttributeSet
attrs) {
super(
context,
attrs);
// TODO Auto-generated constructor stub
this.
context
=
context;
initImageSwitcher();
}
private
void
initImageSwitcher() {
// TODO Auto-generated method stub
Animation
in
=
AnimationUtils.
loadAnimation(
context,
android.
R.
anim.
slide_in_left);
Animation
out
=
AnimationUtils.
loadAnimation(
context,
android.
R.
anim.
slide_out_right);
setInAnimation(
in);
setOutAnimation(
out);
setFactory(
new
ViewFactory() {
@
Override
public
View
makeView() {
// TODO Auto-generated method stub
ImageView
myImage
=
new
ImageView(
context);
myImage.
setScaleType(
ImageView.
ScaleType.
FIT_CENTER);
LayoutParams
params
=
new
ImageSwitcher.
LayoutParams(
LayoutParams.
MATCH_PARENT,
LayoutParams.
MATCH_PARENT);
myImage.
setLayoutParams(
params);
return
myImage;
}
});
}
}
切换文字。。。同理。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
public
class
MyTextSwitcher
extends
TextSwitcher {
private
Context
context;
public
MyTextSwitcher(
Context
context) {
super(
context);
// TODO Auto-generated constructor stub
}
public
MyTextSwitcher(
Context
context,
AttributeSet
attrs) {
super(
context,
attrs);
// TODO Auto-generated constructor stub
this.
context
=
context;
initImageSwitcher();
}
private
void
initImageSwitcher() {
// TODO Auto-generated method stub
Animation
in
=
AnimationUtils.
loadAnimation(
context,
android.
R.
anim.
slide_in_left);
Animation
out
=
AnimationUtils.
loadAnimation(
context,
android.
R.
anim.
slide_out_right);
setInAnimation(
in);
setOutAnimation(
out);
setFactory(
new
ViewFactory() {
@
Override
public
View
makeView() {
// TODO Auto-generated method stub
TextView
myText
=
new
TextView(
context);
myText.
setGravity(
Gravity.
TOP |
Gravity.
CENTER_HORIZONTAL);
myText.
setTextSize(
42);
myText.
setTextColor(
Color.
RED);
return
myText;
}
});
}
}