public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
/**
* 透明度渐变的动画
*
* @param view
*/
public void alpha(View view) {
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
// 动画播放的时间长度
aa.setDuration(2000);
// 设置重复播放的次数
aa.setRepeatCount(Animation.INFINITE);
// 设置重复播放的模式
aa.setRepeatMode(Animation.REVERSE);
// 让iv播放aa动画
iv.startAnimation(aa);
}
/**
* 平移动画
*
* @param view
*/
public void trans(View view) {
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f);
// 动画播放的时间长度
ta.setDuration(2000);
// 设置重复播放的次数
ta.setRepeatCount(Animation.INFINITE);
// 设置重复播放的模式
ta.setRepeatMode(Animation.REVERSE);
// 让iv播放aa动画
iv.startAnimation(ta);
}
/**
* 缩放动画
*/
public void scale(View view) {
ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 动画播放的时间长度
sa.setDuration(2000);
// 设置重复播放的次数
sa.setRepeatCount(Animation.INFINITE);
// 设置重复播放的模式
sa.setRepeatMode(Animation.REVERSE);
// 让iv播放aa动画
iv.startAnimation(sa);
}
/**
* 旋转动画
*/
public void rotate(View view) {
RotateAnimation ra = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 动画播放的时间长度
ra.setDuration(2000);
// 设置重复播放的次数
ra.setRepeatCount(Animation.INFINITE);
// 设置重复播放的模式
ra.setRepeatMode(Animation.REVERSE);
// 让iv播放aa动画
iv.startAnimation(ra);
}
/**
* 动画合集 集合
*/
public void set(View view) {
AnimationSet set = new AnimationSet(false);
RotateAnimation ra = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 动画播放的时间长度
ra.setDuration(2000);
// 设置重复播放的次数
ra.setRepeatCount(Animation.INFINITE);
// 设置重复播放的模式
ra.setRepeatMode(Animation.REVERSE);
ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 动画播放的时间长度
sa.setDuration(2000);
// 设置重复播放的次数
sa.setRepeatCount(Animation.INFINITE);
// 设置重复播放的模式
sa.setRepeatMode(Animation.REVERSE);
// 让iv播放aa动画
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f);
// 动画播放的时间长度
ta.setDuration(2000);
// 设置重复播放的次数
ta.setRepeatCount(Animation.INFINITE);
// 设置重复播放的模式
ta.setRepeatMode(Animation.REVERSE);
set.addAnimation(ta);
set.addAnimation(sa);
set.addAnimation(ra);
iv.startAnimation(set);
}
}
xml写法
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
/**
* 透明度渐变的动画
* @param view
*/
public void alpha(View view){
Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(aa);
}
/**
* 平移动画
* @param view
*/
public void trans(View view){
Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans);
iv.startAnimation(ta);
}
/**
* 缩放动画
*/
public void scale(View view){
Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);
iv.startAnimation(sa);
}
/**
* 旋转动画
*/
public void rotate(View view){
Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);
iv.startAnimation(ra);
}
/**
* 动画合集 集合
*/
public void set(View view){
Animation set = AnimationUtils.loadAnimation(this, R.anim.set);
iv.startAnimation(set);
}
}
透明渐变
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
android:repeatCount="infinite"
android:repeatMode="reverse" >
</alpha>
平移
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="100%"
android:fromYDelta="0"
android:toYDelta="100%"
android:repeatCount="2"
android:repeatMode="reverse"
android:duration="2000" >
</translate>
旋转
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse" >
</rotate>
缩放
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="20%"
android:toXScale="200%"
android:fromYScale="20%"
android:toYScale="200%"
android:duration="2000"
android:repeatCount="2"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse" >
</scale>
集合
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"
android:toDegrees="360" >
</rotate>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="20%"
android:fromYScale="20%"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"
android:toXScale="200%"
android:toYScale="200%" >
</scale>
</set>
属性动画
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "被点击了", 0).show();
}
});
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationY", new float[]{0,2,4,6,8,10,15,20,25,30,50,80,100,150});
oa.setDuration(2000);
oa.setRepeatCount(ObjectAnimator.INFINITE);
oa.setRepeatMode(ObjectAnimator.RESTART);
oa.start();
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
/**
* 透明度渐变的动画
*
* @param view
*/
public void alpha(View view) {
// iv.setAlpha(alpha);
// iv.getAlpha();
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", new float[] {
0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f });
oa.setDuration(3000);
oa.setRepeatCount(ObjectAnimator.INFINITE);
oa.setRepeatMode(ObjectAnimator.REVERSE);
oa.start();
}
/**
* 平移动画
*
* @param view
*/
public void trans(View view) {
// iv.setTranslationX(translationX);
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX",
new float[] { 10f, 20f, 30f, 40f, 60f, 80f });
oa.setDuration(3000);
oa.setRepeatCount(ObjectAnimator.INFINITE);
oa.setRepeatMode(ObjectAnimator.REVERSE);
oa.start();
}
/**
* 缩放动画
*/
public void scale(View view) {
// iv.setScaleX(scaleX)
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX", new float[] {
1f, 2f, 3f, 4f, 5f, 6f });
oa.setDuration(3000);
oa.setRepeatCount(ObjectAnimator.INFINITE);
oa.setRepeatMode(ObjectAnimator.REVERSE);
oa.start();
}
/**
* 旋转动画
*/
public void rotate(View view) {
// iv.setRotation(rotation);
// iv.getRotation()
// iv.setRotationX(rotationX);
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY",
new float[] { 90f, 180f, 270f, 360f });
oa.setDuration(3000);
oa.setRepeatCount(ObjectAnimator.INFINITE);
oa.setRepeatMode(ObjectAnimator.REVERSE);
oa.start();
}
/**
* 动画合集 集合
*/
public void set(View view) {
AnimatorSet set = new AnimatorSet();
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX",
new float[] { 10f, 20f, 30f, 40f, 60f, 80f });
oa.setDuration(3000);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY",
new float[] { -10f, -20f, -30f, -40f, -60f, -80f });
oa2.setDuration(3000);
set.playTogether(oa, oa2);
set.start();
}
}
xml方式
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
/**
* 透明度渐变的动画
*
* @param view
*/
public void alpha(View view) {
Animator animator = AnimatorInflater.loadAnimator(this,
R.animator.alpha);
animator.setTarget(iv);
animator.start();
}
/**
* 平移动画
*
* @param view
*/
public void trans(View view) {
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX",
new float[] { 10f, 20f, 30f, 40f, 60f, 80f });
oa.setDuration(3000);
oa.setRepeatCount(ObjectAnimator.INFINITE);
oa.setRepeatMode(ObjectAnimator.REVERSE);
oa.start();
}
/**
* 缩放动画
*/
public void scale(View view) {
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX", new float[] {
1f, 2f, 3f, 4f, 5f, 6f });
oa.setDuration(3000);
oa.setRepeatCount(ObjectAnimator.INFINITE);
oa.setRepeatMode(ObjectAnimator.REVERSE);
oa.start();
}
/**
* 旋转动画
*/
public void rotate(View view) {
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY",
new float[] { 90f, 180f, 270f, 360f });
oa.setDuration(3000);
oa.setRepeatCount(ObjectAnimator.INFINITE);
oa.setRepeatMode(ObjectAnimator.REVERSE);
oa.start();
}
/**
* 动画合集 集合
*/
public void set(View view) {
AnimatorSet set = new AnimatorSet();
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX",
new float[] { 10f, 20f, 30f, 40f, 60f, 80f });
oa.setDuration(3000);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY",
new float[] { -10f, -20f, -30f, -40f, -60f, -80f });
oa2.setDuration(3000);
set.playTogether(oa, oa2);
set.start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:propertyName="alpha"
android:repeatCount="3"
android:repeatMode="reverse"
android:valueFrom="0.0"
android:valueTo="1.0" >
</objectAnimator>