1.XML文件的代码方式实现: 核心类 AnimationDrawable
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}
//在res/drawable/中定义XML文件rocket_thrust.xml
注:把xml文件设置给Image控件属性设置:
1. 通过XML来是实现:android:src="@drawable/frame_anim"
2. 通过代码实现::iv.setImageResource(R.drawable.frame_anim);
2.直接以代码的方式实现:
// 创建一个帧动画
AnimationDrawable drawable = new AnimationDrawable();
// 添加一帧的动画
drawable.addFrame(context.getResources()
.getDrawable(R.drawable.desktop_rocket_launch_1), 200);
drawable.addFrame(context.getResources()
.getDrawable(R.drawable.desktop_rocket_launch_2), 200);
// 为Imageview设置帧动画
rocketView.setImageDrawable(drawable);
drawable.start();
注:
1.透明 : AlphaAnimation
//方式1:直接以代码的方式实现
public void alpha1(View view) {
/*
* 参数1:从完全透明 啥都看不见 参数2:到完全不透明 啥的看的见
*/
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(100);
// 设置重复次数
alphaAnimation.setRepeatCount(Animation.INFINITE);
// 设置重复模式
alphaAnimation.setRepeatMode(Animation.REVERSE);
// 让ImageView执行这个动画效果
iv.startAnimation(alphaAnimation);
// iv.setAnimation(alphaAnimation);
// alphaAnimation.start(); 不兼容高版本
}
//方式2:XML文件的方式实现,XML文件定义在res/anim/中
public void alpha2(View view){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
iv.startAnimation(animation);
}
2.平移: TranslateAnimation
public void translate(View view){
/*
* 坐标系相对自己的
* 参数1/2:从x的哪个坐标到x的哪个坐标
* 参数3/4:从y的哪个坐标到y的哪个坐标
*/
TranslateAnimation translateAnimation = new TranslateAnimation(0, 100, 0, 100);
translateAnimation.setDuration(2000);
//让动画保持执行完后的效果
translateAnimation.setFillAfter(true);
view2.startAnimation(translateAnimation);
}
3.缩放: ScaleAnimation
public void scale(View view){
/*
* 参数1:x方向从哪个倍数开始
* 参数2:x方向到哪个倍数
* 参数3:y方向从哪个倍数开始
* 参数4:y向下到哪个倍数
* 参数5:x中心的类型(x方向参考的坐标系)
* 参数6:中心点的x坐标(如果参考自己,传递的是百分比)
* 参数7:y中心的类型(y方向参考的坐标系)
* 参数8:中心点的y坐标(如果参考自己,传递的是百分比)
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(0.1f, 10, 0.1f, 10, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.25f);
scaleAnimation.setDuration(5000);
iv.startAnimation(scaleAnimation);
}
4.旋转: RotateAnimation
public void rotate(View view){
RotateAnimation rotateAnimation = new RotateAnimation(0, 45, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
//long duration = rotateAnimation.getDuration();
rotateAnimation.setDuration(1000);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setRepeatMode(Animation.REVERSE);
rotateAnimation.setFillAfter(true);
iv.startAnimation(rotateAnimation);
}
5.集合: AnimationSet
public void set(View view){
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(1000);
//设置重复次数
alphaAnimation.setRepeatCount(Animation.INFINITE);
//设置重复模式
alphaAnimation.setRepeatMode(Animation.REVERSE);
//将透明动画添加到集合
animationSet.addAnimation(alphaAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0, 45, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
rotateAnimation.setDuration(1000);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setRepeatMode(Animation.REVERSE);
//将旋转动画添加到集合
animationSet.addAnimation(rotateAnimation);
//让ImageView执行动画集合
iv.startAnimation(animationSet);
}
1.透明 : ObjectAnimator-alpha
public void alpha1(View view) {
// iv.setAlpha(alpha)
/*
* 参数1:要修改哪个控件 参数2:要修改的控件的属性的名称 参数3:要将属性的值修改为(可变参数)
*/
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv, "alpha", 0, 1, 0.5f);
objectAnimator.setDuration(2000);
objectAnimator.start();
}
//方式一:以XML文件的方式实现,定义在res/animator/
public void alpha2(View view) {
ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.alpha_animtor);
// 要作用在哪个控件上
objectAnimator.setTarget(iv);
objectAnimator.start();
}
2.平移,旋转,缩放 : ObjectAnimator-alpha-translationX-rotationY
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv, "translationX", 0,200);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv, "scaleX", 1,0.1f);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv, "rotationY", 0,10);
3.集合 : AnimatorSet
public void set(View view) {
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator rotationX = ObjectAnimator.ofFloat(iv, "rotationX", 0, 45);
rotationX.setDuration(1000);
ObjectAnimator rotationY = ObjectAnimator.ofFloat(iv, "rotationY", 0, 45);
rotationY.setDuration(1000);
// 让他们同时执行
// animatorSet.playTogether(rotationX,rotationY);
// 连续执行
animatorSet.playSequentially(rotationX, rotationY);
// 开始
animatorSet.start();
}
4.ValueAnimator.
private void moveRocket2Original() {
int startX = paramsRocket.x;
int endX = 0;
//只负责值的变化.
ValueAnimator animatorX = ValueAnimator.ofInt(startX, endX);
//值变化的监听
animatorX.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (Integer) animation.getAnimatedValue();
paramsRocket.x = value;
mWM.updateViewLayout(rocketView, paramsRocket);
}
});
//动画一个监听
animatorX.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
@Override
public void onAnimationEnd(Animator animation) {
rocketView.setImageResource(R.drawable.desktop_bg_2);
}
@Override
public void onAnimationCancel(Animator animation) {}
});
animatorX.start();
}