心得體會
- 前一陣子,由于電腦的原因,簡書聽了好一陣子沒寫~~,接下來我會陸續(xù)把我欠的補回來的。關(guān)鍵幀動畫(FrameAnimation)相對還是比較簡單的, 它只能實現(xiàn)比較小的動畫效果,如果復(fù)雜而且?guī)瑪?shù)比較多的動畫不太建議使用逐幀動畫
目錄
-
1.關(guān)鍵幀動畫簡介
-
2.關(guān)鍵幀動畫具體實現(xiàn)過程
具體內(nèi)容
一、關(guān)鍵幀動畫簡介:
- 關(guān)鍵幀動畫是一種常見的動畫形式(Frame By Frame),其原理是在“連續(xù)的關(guān)鍵幀”中分解動畫動作,也就是在時間軸的每幀上逐幀繪制不同的內(nèi)容,使其連續(xù)播放而成動畫。
- 這種動畫更多的依賴于完善的UI資源,原理就是將一張張單獨的圖片連貫的進行播放,從而在視覺上產(chǎn)生一種動畫的效果;有點類似于某些軟件制作gif動畫的方式
二、關(guān)鍵幀動畫具體實現(xiàn)過程
-
1.使用xml配置動畫(動畫是固定)
1.將所需圖片資源拖到res/drawable里面

image.png
2.在res/drawable目錄下新建一個Drawable resource file文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/campfire01"
android:duration="200"/>
<item android:drawable="@drawable/campfire02"
android:duration="200"/>
<item android:drawable="@drawable/campfire03"
android:duration="200"/>
<item android:drawable="@drawable/campfire04"
android:duration="200"/>
<item android:drawable="@drawable/campfire05"
android:duration="200"/>
<item android:drawable="@drawable/campfire06"
android:duration="200"/>
<item android:drawable="@drawable/campfire07"
android:duration="200"/>
<item android:drawable="@drawable/campfire08"
android:duration="200"/>
<item android:drawable="@drawable/campfire09"
android:duration="200"/>
<item android:drawable="@drawable/campfire10"
android:duration="200"/>
<item android:drawable="@drawable/campfire11"
android:duration="200"/>
<item android:drawable="@drawable/campfire12"
android:duration="200"/>
<item android:drawable="@drawable/campfire13"
android:duration="200"/>
<item android:drawable="@drawable/campfire14"
android:duration="200"/>
<item android:drawable="@drawable/campfire15"
android:duration="200"/>
<item android:drawable="@drawable/campfire16"
android:duration="200"/>
<item android:drawable="@drawable/campfire17"
android:duration="200"/>
</animation-list>
**關(guān)鍵幀動畫配置文件所需根節(jié)點是animation-list(動畫列表),里面有一個或者多個item節(jié)點組成
其中:
oneshot屬性表示是否只播放一次,true表示只會播放一次,false表示一直循環(huán)播放,- 一個
item對應(yīng)一幀:即一張圖片android:drawable:配置動畫的圖片android:druation代表此幀持續(xù)的時間,整數(shù),單位為毫秒**
3.在 activity_main.xml里面用ImageView控件作為動畫載體來顯示動畫
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv_anim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/fire_anim"
android:scaleType="fitXY"/>
</RelativeLayout>
4.在MainActivity.java里面啟動動畫
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
//獲取動畫的控件
ImageView iv=findViewById(R.id.iv_anim);
//2.通過控件獲取動畫
AnimationDrawable anim=(AnimationDrawable)iv.getDrawable();
//3.啟動動畫
anim.start();
}
return true;
}
}
-
2.使用代碼創(chuàng)建(在運行過程中有變化)
1.在 activity_main.xml里面用ImageView控件作為動畫載體來顯示動畫
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv_anim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/campfire01"
android:scaleType="fitXY"/>
</RelativeLayout>
2.在MainActivity.java里面啟動動畫
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.創(chuàng)建一個動畫資源
AnimationDrawable anim = new AnimationDrawable();
//2.添加每一幀的動畫
int[] resID = {R.drawable.campfire01, R.drawable.campfire02, R.drawable.campfire03,
R.drawable.campfire04, R.drawable.campfire05, R.drawable.campfire06,
R.drawable.campfire07, R.drawable.campfire08, R.drawable.campfire09,
R.drawable.campfire10, R.drawable.campfire11, R.drawable.campfire12,
R.drawable.campfire13, R.drawable.campfire14, R.drawable.campfire15,
R.drawable.campfire16, R.drawable.campfire17};
for (int id:resID) {
anim.addFrame(getResources().getDrawable(id,
null),
100);
}
//3.使用這個動畫資源
ImageView iv=findViewById(R.id.iv_anim);
iv.setImageDrawable(anim);
//4.啟動動畫
anim.start();
}
void start()- 開始播放動畫
void stop()- 停止播放動畫
addFrame(Drawable frame, int duration)- 添加一幀,并設(shè)置該幀顯示的持續(xù)時間
boolean isRunning()- 是否正在播放