DrawableAnimation 代表的动画默认是不播放的,必须在程序中启动动画播放才可以。
播放 start()
停止 stop()
实现:DrawableAnimation 通过把图片设置成ImageView的背景实现动画效果
代码通过获取tup的背景(就是anim里面的帧)获取到强转然后可以开启(start)关闭(stop)
AnimationDrawable drawable=(AnimationDrawable) tup.getBackground();
一旦在程序中通过通过AnimationUtils得到代表补间动画的Animation之后,接下来就可以调用View的startAnimation()方法开始对View执行动画了
Animation anim = AnimationUtils
.loadAnimation(context, R.anim.donghua);
view.startAnimation(anim);
注:imageView1.startAnimation(animation);当前动画没有执行完就会被下一个给替换掉
animator.start();方法可以一起同时用
来看代码补间动画
package com.example.tweenanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView imageView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.imageView1);
}
public void AlphaAnimation1(View v) {
// 创建AlphaAnimation(透明动画)的对象参数1是开始状态2时结束状态
AlphaAnimation animation = new AlphaAnimation(1, 0);
// 设置时间
animation.setDuration(3000);
// 设置循环次数
animation.setRepeatCount(1);
// 设置循环模式Animation.REVERSE是把循环第二个的AlphaAnimation参数反过来
animation.setRepeatMode(Animation.REVERSE);
// 开启动画
imageView1.startAnimation(animation);//给图片设置动画
}
public void ScaleAnimation1(View v) {
// 1是原始大小0是没有
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
scaleAnimation.setDuration(2000);
imageView1.startAnimation(scaleAnimation);
}
public void TranslateAnimation1(View v) {
//0是坐标
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 100);
animation.setDuration(2000);
imageView1.startAnimation(animation);
}
public void RotateAnimation1(View v) {
RotateAnimation rotateAnimation = new RotateAnimation(1, 360, 1, 0.5f,
1, 0.5f);
rotateAnimation.setDuration(3000);
imageView1.startAnimation(rotateAnimation);
}
public void jihe(View v) {
// 创建集合对象
AnimationSet set = new AnimationSet(true);
AlphaAnimation animation = new AlphaAnimation(1, 0);
TranslateAnimation animation2 = new TranslateAnimation(0, 0, 0, 100);
set.addAnimation(animation);
set.addAnimation(animation2);
set.setDuration(3000);
imageView1.startAnimation(set);
}
public void caru(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 100);
animation.setDuration(2000);
animation.setInterpolator(new AccelerateInterpolator());// 开始较慢后来快
animation.setInterpolator(new CycleInterpolator(2));// 正弦曲线改变
imageView1.startAnimation(animation);
}
}
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="AlphaAnimation1"
android:text="透明度动画" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="ScaleAnimation1"
android:text="大小缩放动画" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="TranslateAnimation1"
android:text="位移变化动画" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="RotateAnimation1"
android:text="旋转动画" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="jihe"
android:text="动画集合" />
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="动画插入器"
android:onClick="caru" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
上面都是补间动画四个效果
帧动画
package com.example.frameanimation;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends Activity {
AnimationDrawable drawable;
ImageView tup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tup=(ImageView) findViewById(R.id.tup);
drawable=(AnimationDrawable) tup.getBackground();
}
public void open(View v){
drawable.start();
}
public void close(View v){
drawable.stop();
}
}
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"
tools:context="${relativePackage}.${activityClass}"
android:background="#999"
android:orientation="vertical">
<ImageView
android:id="@+id/tup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/fram"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:onClick="open"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束"
android:onClick="close"
/>
</LinearLayout>
在res下新建一个文件夹anim然后写xml文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pp_card_video_play_anm1" android:duration="100"></item>
<item android:drawable="@drawable/pp_card_video_play_anm2" android:duration="100"></item>
<item android:drawable="@drawable/pp_card_video_play_anm3" android:duration="100"></item>
<item android:drawable="@drawable/pp_card_video_play_anm4" android:duration="100"></item>
<item android:drawable="@drawable/pp_card_video_play_anm5" android:duration="100"></item>
<item android:drawable="@drawable/pp_card_video_play_anm6" android:duration="100"></item>
<item android:drawable="@drawable/pp_card_video_play_anm7" android:duration="100"></item>
<item android:drawable="@drawable/pp_card_video_play_anm8" android:duration="100"></item>
<item android:drawable="@drawable/pp_card_video_play_anm9" android:duration="100"></item>
</animation-list>
结合使用通过anim文件xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1"
android:toAlpha="0.1"
android:duration="3000"/>
<rotate
android:fromDegrees="0"
android:toDegrees="1800"
android:duration="5000"
></rotate>
<scale android:fromXScale="1"
android:toXScale="0.3"
android:fromYScale="1"
android:toYScale="0.3"
android:duration="3000" />
<translate
android:fromXDelta="0"
android:toXDelta="400"
android:duration="4000"></translate>
</set>
在代码用
Animation anim=AnimationUtils.loadAnimation(this, R.anim.jiehe);得到对象
tup2.startAnimation(anim);设置图片的动画
切换Activity动画
startActivity(new Intent(this,Main2Activity.class));
overridePendingTransition(R.anim.enter, R.anim.tui);//先退后进
enter的xml代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p"
android:toXDelta="0%p"
android:duration="1000"/>
</set>
tui的xml代码
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%p"
android:toXDelta="-100%p"
android:duration="1000">
</translate>
这就设置了开启另一个页面的动画
然后在设置一下另一个页面返回动画
当页面切换时就是页面暂停所以在onPause写,当页面快要被停止销毁了写就没用了
@Override
protected void onPause() {
// TODO Auto-generated method stub
overridePendingTransition(R.anim.enter2, R.anim.tui2);
super.onPause();
}
enter2
<?xml version="1.0" encoding="utf-8"?>
<translate android:fromXDelta="-100%p" xmlns:android="http://schemas.android.com/apk/res/android"
android:toXDelta="0%p"
android:duration="1000">
</translate>
tui2
<?xml version="1.0" encoding="utf-8"?>
<translate
android:fromXDelta="0%p" xmlns:android="http://schemas.android.com/apk/res/android"
android:toXDelta="100%p"
android:duration="1000"
>
</translate>
效果就是像Viewpager一样水平切换
属性动画
ObjectAnimator
ObjectAnimator object = ObjectAnimator.ofFloat(btObject, "RotationY", 360f);
object.setDuration(2000);
object.start();
属性动画也可以用AnimationSet动画集合来执行多个动画
package com.example.frameanimation;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
public class Main2Activity extends Activity {
ImageView imageView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
imageView1 = (ImageView) findViewById(R.id.imageView1);
}
public void zhi(View v) {
ValueAnimator ofFloat = ValueAnimator.ofFloat(0, 200);//可以穿多个0-200-360...
ofFloat.setTarget(imageView1);// 设置目标
ofFloat.setDuration(3000);// 延迟时间
ofFloat.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// TODO Auto-generated method stub
float animatedValue = (Float) animation.getAnimatedValue();
imageView1.setTranslationX(animatedValue);// 移动
imageView1.setRotation(animatedValue);
}
});
ofFloat.start();// 开启
}
public void ob(View v) {
// 参数1:使用动画的对象,2是使用动画的属性名,3是更改的值Float..
// ObjectAnimator.ofFloat(imageView1, "alpha", 0,
// 1).setDuration(3000).start();
// 2如果是随便写的话不起任何效果
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView1, "aaa", 0,
1);
// 当你的动画发生改变时我们要实现的功能
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 动画运行时我们要拿到中间值animatedValue
float animatedValue = (Float) animation.getAnimatedValue();
// 当你的动画更改是我们要实现其他的功能
imageView1.setScaleX(animatedValue);// 设置了x的0-1缩放瘦正常
imageView1.setScaleY(animatedValue);
imageView1.setAlpha(animatedValue);// 设置了隐藏
}
});
animator.setDuration(3000);
animator.start();
}
}
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="值动画"
android:onClick="zhi" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="ob" />
</LinearLayout>
// AnimatorInflater.loadAnimator(context, id);//用来加载布局中属性动画
// 用他的对象调用settarget(控件)
android:oneshot="false"设置循环一次
set.setFillAfter(true);//设置最后一针为针
可以用setAnimationListener监听动画来监听动画的结束
Android Matrix动画详解
https://blog.csdn.net/flash129/article/details/8234599
除平移变换(Translate)外,旋转变换(Rotate)、缩放变换(Scale)和错切变换(Skew)都可以围绕一个中心点来进行,如果不指定,在默认情况下是围绕(0, 0)来进行相应的变换的
https://blog.csdn.net/zhanhong39/article/details/78956553
属性动画https://blog.csdn.net/u011200844/article/details/44594263
给动画设置监听setAnimationListener然后结束操作等
player.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
finish();
}
});