FrameAnimation(DrawableAnimation):逐帧动画


首先在布局定义------------------------------
···
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/bt_1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="播放动画" />
<Button
android:id="@+id/bt_2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="暂停动画" />
<ImageView
android:id="@+id/iv_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
···
在res 下的anim 文件夹中 定义xml
···
<animation-list
xmlns:android="http://schemas.androi
d.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/fat_po_f01" android:duration="30"></item>
<item android:drawable="@drawable/fat_po_f02" android:duration="30"></item>
</animation-list>
···
*************Mactivtiy****************
···
package com.example.yframanimation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView img;
private AnimationDrawable ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.iv_img);
//获取动画的图片
img.setBackgroundResource(R.anim.donghua);
//给ImageView 设置背景图片
ad = (AnimationDrawable) img.getBackground();
findViewById(R.id.bt_1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ad.start();//播放
// ad.stop();//暂停
}
});
findViewById(R.id.bt_2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ad.stop();//暂停
}
});
}
}
···
补间动画


AlphaAnimation : 透明动画

ScaleAnimation :缩放动画

TranslateAnimation: 位移动画


RoatateAnimation:旋转动画

AnimationSet :动画集合

xml
···
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/bt_1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="透明动画" />
<Button
android:id="@+id/bt_2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="位移动画" />
<Button
android:id="@+id/bt_3"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="旋转动画" />
<Button
android:id="@+id/bt_4"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="缩放动画" />
<Button
android:id="@+id/bt_5"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="动画集合" />
<Button
android:id="@+id/bt_6"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="动画插入器" />
<Button
android:id="@+id/bt_7"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="开启" />
<ImageView
android:id="@+id/img"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/f10"
android:layout_gravity="center"
/>
</LinearLayout>
···
设置动画的两种方式一种是纯代码
一种是xml( res 下创建anim 里面有透明,缩放,位移,旋转,动画集合 )
透明xml
···
<?xml version="1.0" encoding="utf-8"?>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
xmlns:android="http://schemas.android.com/apk/res/android">
</alpha>
···
缩放xml
···
<?xml version="1.0" encoding="utf-8"?>
<scale
android:fromXScale="0.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000"
android:repeatCount="2"
xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
···
旋转xml
···
<?xml version="1.0" encoding="utf-8"?>
<rotate
android:fromDegrees="0"
android:toDegrees="90"
android:duration="2000"
android:pivotX="50%"
android:pivotY="50%"
xmlns:android="http://schemas.android.com/apk/res/android">
</rotate>
···
位移xml
···
<?xml version="1.0" encoding="utf-8"?>
<translate
android:fromXDelta="0"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="200"
android:fillAfter="true"
android:duration="2000"
xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
···
//开启Activity动画




***********************Mactivity************
···
package com.example.ytest4_tweenanimation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
//透明动画
findViewById(R.id.bt_1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//方式一
// //1.创建透明动画对象 0.0--1.0
// AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
// //2.设置动画时间
// alphaAnimation.setDuration(5000);
//
// //3.设置循环次数
// // alphaAnimation.setRepeatCount(1);
//
// //4. 设置循环模式
// //alphaAnimation.setRepeatMode(AlphaAnimation.REVERSE);
// //5.设置动画执行完之后的状态
// alphaAnimation.setFillAfter(true);
// //6. 开启动画
// img.startAnimation(alphaAnimation);
//
//方式二
Animation loadAnimation = new AnimationUtils().loadAnimation(MainActivity.this, R.anim.alpha);
//执行动画
img.startAnimation(loadAnimation);
}
});
//位移动画
findViewById(R.id.bt_2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// //100f
// TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 100f);
// translateAnimation.setDuration(3000);
// img.startAnimation(translateAnimation);
Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
img.startAnimation(loadAnimation);
}
});
//旋转
findViewById(R.id.bt_3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// // RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
//
// RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//
// //旋转次数 360--720 两圈
// rotateAnimation.setRepeatCount(10);
//
//
// rotateAnimation.setDuration(2000);
// //旋转后停留的位置
// rotateAnimation.setFillAfter(true);
// img .startAnimation(rotateAnimation);
//
Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
img.startAnimation(loadAnimation);
}
});
//缩放动画
findViewById(R.id.bt_4).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// //1.创建对象 0.0f :缩小到没有 1.0f:本身大小 2.0本身大小的2倍 以总布局进行缩放
// // ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f,1.0f,2.0f);
//
// //自身位置缩放
// ScaleAnimation scaleAnimation= new ScaleAnimation
// (1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//
// //2.执行时间
// scaleAnimation.setDuration(2000);
// //停留在动画缩放完后的状态
// scaleAnimation.setFillAfter(true);
// //3.开启动画
// img.startAnimation(scaleAnimation);
//
//方式二
Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
img.startAnimation(loadAnimation);
}
});
//动画集合
findViewById(R.id.bt_5).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// //1.创建集合动画对象
// AnimationSet animationSet = new AnimationSet(true);
// //2.创建动画
// //2.1创建透明动画对象
// AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
// //alphaAnimation.setDuration(2000);
// //旋转动画
// RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// // rotateAnimation.setDuration(2000);
// //缩放
// ScaleAnimation scaleAnimation = new ScaleAnimation(0.1f, 1.0f, 1.0f, 2.0f);
//
//
// animationSet.addAnimation(alphaAnimation);
// animationSet.addAnimation(rotateAnimation);
// animationSet.addAnimation(scaleAnimation);
// animationSet.setDuration(2000);//集体设置时间
// //开启动画
// img.startAnimation(animationSet);
//
Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.set);
img.startAnimation(loadAnimation);
}
});
//动画插入器
findViewById(R.id.bt_6).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//位移 x轴 从0 - 200 y轴不变
TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 200.0f);
translateAnimation.setDuration(2000);
//动画插入器
//translateAnimation.setInterpolator(new AccelerateInterpolator());
//正弦变化次数
translateAnimation.setInterpolator(new CycleInterpolator(2));
//减速
translateAnimation.setInterpolator(new DecelerateInterpolator());
img.startAnimation(translateAnimation);
}
});
//开启Activity动画
findViewById(R.id.bt_7).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, HomeActivity.class));
//位移切换Activity
overridePendingTransition(R.anim.enter, R.anim.exit);
}
});
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击了", Toast.LENGTH_SHORT).show();
}
});
}
}
···
属性动画: PropertyAnimation
逐帧动画与属性动画的区别:
逐帧动画的实际图片位置不改变
属性动画改变


MainActivity********************
···package com.example.ytest5_propertyanimation;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
//透明
findViewById(R.id.bt_1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//方式一
// ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "alpha", 0.0f,0.5f);
// ofFloat.setDuration(2000);
// ofFloat.start();
// 方式二 xml方式
ObjectAnimator loadAnimation = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.object);
loadAnimation.setTarget(img);
loadAnimation.start();
}
});
//位移
findViewById(R.id.bt_2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "translationX", 0,50,-100,300);
ofFloat.setDuration(5000);
ofFloat.start();
}
});
//旋转
findViewById(R.id.bt_3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "rotation", 0,90,360);
ofFloat.setDuration(5000);
ofFloat.start();
}
});
//缩放
findViewById(R.id.bt_4).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "scaleX", 1.0f,2.0f,0.5f);
ofFloat.setDuration(5000);
ofFloat.start();
//更新的监听
ofFloat.addUpdateListener(new AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
String string = animation.getAnimatedValue().toString();
Log.e("TAG", "string " +string);
}
});
}
});
//集合
findViewById(R.id.bt_5).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "rotation", 0,90,360);
ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(img, "scaleX", 1.0f,2.0f,0.5f);
ObjectAnimator ofFloat3 = ObjectAnimator.ofFloat(img, "translationX", 0,50,-100,300);
animatorSet.setDuration(5000);
//playSequentially 单个执行
animatorSet.playSequentially(ofFloat,ofFloat2,ofFloat3);
//playTogether 一起执行
//animatorSet.playTogether(ofFloat,ofFloat2,ofFloat3);
animatorSet.start();
}
});
}
}
···
网友评论