
DrawableAnimation 代表的動畫默認(rèn)是不播放的,必須在程序中啟動動畫播放才可以。
播放 start()
停止 stop()
實現(xiàn):DrawableAnimation 通過把圖片設(shè)置成ImageView的背景實現(xiàn)動畫效果
代碼通過獲取tup的背景(就是anim里面的幀)獲取到強(qiáng)轉(zhuǎn)然后可以開啟(start)關(guān)閉(stop)
AnimationDrawable drawable=(AnimationDrawable) tup.getBackground();

一旦在程序中通過通過AnimationUtils得到代表補(bǔ)間動畫的Animation之后,接下來就可以調(diào)用View的startAnimation()方法開始對View執(zhí)行動畫了
Animation anim = AnimationUtils
.loadAnimation(context, R.anim.donghua);
view.startAnimation(anim);
注:imageView1.startAnimation(animation);當(dāng)前動畫沒有執(zhí)行完就會被下一個給替換掉
animator.start();方法可以一起同時用
來看代碼補(bǔ)間動畫
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) {
// 創(chuàng)建AlphaAnimation(透明動畫)的對象參數(shù)1是開始狀態(tài)2時結(jié)束狀態(tài)
AlphaAnimation animation = new AlphaAnimation(1, 0);
// 設(shè)置時間
animation.setDuration(3000);
// 設(shè)置循環(huán)次數(shù)
animation.setRepeatCount(1);
// 設(shè)置循環(huán)模式Animation.REVERSE是把循環(huán)第二個的AlphaAnimation參數(shù)反過來
animation.setRepeatMode(Animation.REVERSE);
// 開啟動畫
imageView1.startAnimation(animation);//給圖片設(shè)置動畫
}
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是坐標(biāo)
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) {
// 創(chuàng)建集合對象
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="旋轉(zhuǎn)動畫" />
<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>
上面都是補(bǔ)間動畫四個效果
幀動畫
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="結(jié)束"
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>
結(jié)合使用通過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);設(shè)置圖片的動畫
切換Activity動畫
startActivity(new Intent(this,Main2Activity.class));
overridePendingTransition(R.anim.enter, R.anim.tui);//先退后進(jìn)
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>
這就設(shè)置了開啟另一個頁面的動畫
然后在設(shè)置一下另一個頁面返回動畫
當(dāng)頁面切換時就是頁面暫停所以在onPause寫,當(dāng)頁面快要被停止銷毀了寫就沒用了
@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動畫集合來執(zhí)行多個動畫
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);// 設(shè)置目標(biāo)
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) {
// 參數(shù)1:使用動畫的對象,2是使用動畫的屬性名,3是更改的值Float..
// ObjectAnimator.ofFloat(imageView1, "alpha", 0,
// 1).setDuration(3000).start();
// 2如果是隨便寫的話不起任何效果
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView1, "aaa", 0,
1);
// 當(dāng)你的動畫發(fā)生改變時我們要實現(xiàn)的功能
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 動畫運行時我們要拿到中間值animatedValue
float animatedValue = (Float) animation.getAnimatedValue();
// 當(dāng)你的動畫更改是我們要實現(xiàn)其他的功能
imageView1.setScaleX(animatedValue);// 設(shè)置了x的0-1縮放瘦正常
imageView1.setScaleY(animatedValue);
imageView1.setAlpha(animatedValue);// 設(shè)置了隱藏
}
});
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);//用來加載布局中屬性動畫
// 用他的對象調(diào)用settarget(控件)
android:oneshot="false"設(shè)置循環(huán)一次
set.setFillAfter(true);//設(shè)置最后一針為針
可以用setAnimationListener監(jiān)聽動畫來監(jiān)聽動畫的結(jié)束
Android Matrix動畫詳解
https://blog.csdn.net/flash129/article/details/8234599
除平移變換(Translate)外,旋轉(zhuǎn)變換(Rotate)、縮放變換(Scale)和錯切變換(Skew)都可以圍繞一個中心點來進(jìn)行,如果不指定,在默認(rèn)情況下是圍繞(0, 0)來進(jìn)行相應(yīng)的變換的
https://blog.csdn.net/zhanhong39/article/details/78956553
屬性動畫https://blog.csdn.net/u011200844/article/details/44594263
給動畫設(shè)置監(jiān)聽setAnimationListener然后結(jié)束操作等
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();
}
});