废话不多说直接上代码
public class
MainActivity
extends
AppCompatActivity
implements
View.OnClickListener {
ImageView
iv
;
@Override
protected void
onCreate
(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState)
;
setContentView(R.layout.
activity_main
)
;
iv
= (ImageView) findViewById(R.id.
iv
)
;
iv
.setOnClickListener(
this
)
;
}
public void
alphaonclick
(View view) {
AlphaAnimation animation =
new
AlphaAnimation(
1
,
0
)
;
animation.setDuration(
3000
)
;
animation.setFillAfter(
true
)
;
//让动画延时执行
animation.setStartOffset(
2000
)
;
iv
.startAnimation(animation)
;
}
public void
transonclick
(View view) {
/**
* 1.X轴移动的起始位置
* 2.X轴移动的结束位置
* 3.Y轴移动的起始位置
* 4.Y轴移动的结束位置
*/
TranslateAnimation ta =
new
TranslateAnimation(
0
,
450
,
0
,
450
)
;
//动画执行的时间,5000毫秒
ta.setDuration(
1000
)
;
//设置动画结束后ImageView保持动画结束时的状态
// ta.setFillAfter(true);
//让ImageView产生动画效果
iv
.startAnimation(ta)
;
}
@Override
public void
onClick
(View v) {
Toast.
makeText
(
this,
"我还是我"
,
Toast.
LENGTH_SHORT
).show()
;
}
public void
scaleonclick
(View view) {
//ScaleAnimation animation= new ScaleAnimation(0, 2, 0, 1, iv.getWidth() / 2, iv.getHeight() / 2);
ScaleAnimation animation =
new
ScaleAnimation(
1
,
0
,
1
,
0
,
ScaleAnimation.
RELATIVE_TO_SELF
,
0.5f
,
ScaleAnimation.
RELATIVE_TO_SELF
,
0.5f
)
;
animation.setDuration(
1000
)
;
animation.setFillAfter(
true
)
;
iv
.startAnimation(animation)
;
}
public void
rotateonclick
(View view) {
RotateAnimation animation =
new
RotateAnimation(
0
,
359
,
RotateAnimation.
RELATIVE_TO_SELF
,
0.5f
,
RotateAnimation.
RELATIVE_TO_SELF
,
0.5f
)
;
//设置旋转次数
animation.setRepeatCount(RotateAnimation.
INFINITE
)
;
//设置重复旋转的模式
animation.setRepeatMode(RotateAnimation.
REVERSE
)
;
animation.setDuration(
2000
)
;
iv
.startAnimation(animation)
;
}
//组合动画(java)
public void
allonclick
(View view) {
AlphaAnimation animation =
new
AlphaAnimation(
1
,
0
)
;
/* animation.setDuration(3000);
animation.setFillAfter(true);
//让动画延时执行
animation.setStartOffset(2000);*/
TranslateAnimation animation2 =
new
TranslateAnimation(
0
,
300
,
0
,
300
)
;
/* animation2.setDuration(3000);
animation2.setFillAfter(true);
//让动画延时执行
animation2.setStartOffset(2000);*/
ScaleAnimation animation3 =
new
ScaleAnimation(
1
,
2
,
1
,
2
,
ScaleAnimation.
RELATIVE_TO_SELF
,
0.5f
,
ScaleAnimation.
RELATIVE_TO_SELF
,
0.5f
)
;
/* animation3.setDuration(1000);
animation3.setFillAfter(true);*/
RotateAnimation animation4 =
new
RotateAnimation(
0
,
359
,
RotateAnimation.
RELATIVE_TO_SELF
,
0.5f
,
RotateAnimation.
RELATIVE_TO_SELF
,
0.5f
)
;
/* //设置旋转次数
animation.setRepeatCount(RotateAnimation.INFINITE);
//设置重复旋转的模式
animation.setRepeatMode(RotateAnimation.REVERSE);
animation.setDuration(2000);*/
AnimationSet set =
new
AnimationSet(
true
)
;
set.setDuration(
3000
)
;
// set.setFillAfter(true);
set.setStartOffset(
2000
)
;
set.setRepeatMode(Animation.
REVERSE
)
;
set.setRepeatCount(Animation.
INFINITE
)
;
set.setInterpolator(
new
AccelerateInterpolator())
;
set.addAnimation(animation)
;
set.addAnimation(animation2)
;
set.addAnimation(animation3)
;
set.addAnimation(animation4)
;
iv
.startAnimation(set)
;
}
public void
xmlalphaonclick
(View view) {
Animation animation = AnimationUtils.
loadAnimation
(
this,
R.anim.
alpha
)
;
iv
.startAnimation(animation)
;
}
public void
xmlrotateonclick
(View view) {
Animation animation = AnimationUtils.
loadAnimation
(
this,
R.anim.
rotate
)
;
iv
.startAnimation(animation)
;
}
//组合动画(XML)
public void xmlallonclick(View view) {
Animation animation = AnimationUtils.
loadAnimation
(this, R.anim.
animset
);
iv.startAnimation(animation);
}
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:repeatCount="2"
android:repeatMode="reverse"
android:fillBefore="true"
android:startOffset="1000"
android:shareInterpolator="true"
>
<alpha android:fromAlpha="1"
android:toAlpha="0"
/>
<translate android:fromXDelta="0"
android:toXDelta="300"
android:fromYDelta="0"
android:toYDelta="300"
/>
<scale android:fromXScale="1"
android:toXScale="0"
android:fromYScale="1"
android:toYScale="0"
android:pivotY="50%"
android:pivotX="50%"
/>
<rotate android:fromDegrees="0"
android:toDegrees="359"
android:pivotX="50%"
android:pivotY="50%"
/>
</set>
public void
xmltransonclick
(View view) {
Animation animation = AnimationUtils.
loadAnimation
(
this,
R.anim.
translate
)
;
iv
.startAnimation(animation)
;
}
public void
xmlscaleonclick
(View view) {
Animation animation = AnimationUtils.
loadAnimation
(this, R.anim.
scale
);
animation.setInterpolator(new AccelerateInterpolator());
iv.startAnimation(animation);
}
}
xml格式
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<set
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
android
:duration=
"2000"
android
:fillAfter=
"true"
android
:repeatMode=
"reverse"
>
<alpha
android
:fromAlpha=
"1"
android
:toAlpha=
"0"
android
:repeatCount=
"1"
/>
</set>