1,在drawable資料夾底下新增walking.xml
放入要製作成動畫的圖片:
walking01.png
walking02.png
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:drawable="@drawable/walking01" android:duration="800"/> | |
<item android:drawable="@drawable/walking02" android:duration="800"/> | |
</animation-list> |
2,在main_activity.xml中新增ImageView,並將背景設成剛剛新增的walking.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ImageView | |
android:id="@+id/ivWalking" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintBottom_toBottomOf="parent" | |
android:background="@drawable/walking"/> |
3,在ActivityMain.java中設定動畫
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ivWalking = findViewById(R.id.ivWalking); | |
AnimationDrawable animationDrawable = (AnimationDrawable) ivWalking.getBackground(); | |
animationDrawable.start(); | |
} |
執行結果:
留言
張貼留言