一、android中的动画
动画分类:
1、帧动画
2、补间动画
3、属性动画
二、帧动画
类似于电影,一张张图片连续播放
1、xml方式
anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/anim1" android:duration="100"/>
<item android:drawable="@drawable/anim2" android:duration="100"/>
<item android:drawable="@drawable/anim3" android:duration="100"/>
<item android:drawable="@drawable/anim4" android:duration="100"/>
<item android:drawable="@drawable/anim5" android:duration="100"/>
<item android:drawable="@drawable/anim6" android:duration="100"/>
<item android:drawable="@drawable/anim7" android:duration="100"/>
<item android:drawable="@drawable/anim8" android:duration="100"/>
<item android:drawable="@drawable/anim9" android:duration="100"/>
<item android:drawable="@drawable/anim10" android:duration="100"/>
<item android:drawable="@drawable/anim11" android:duration="100"/>
<item android:drawable="@drawable/anim12" android:duration="100"/>
</animation-list>
2、代码方式
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop" />
<Button
android:id="@+id/java_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java code frame animation" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_view" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
imageView.setImageResource(R.drawable.anim);
findViewById(R.id.start).setOnClickListener(v -> {
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getDrawable();
if (animationDrawable != null && !animationDrawable.isRunning()) {
animationDrawable.start();
}
});
findViewById(R.id.stop).setOnClickListener(v -> {
AnimationDrawable animationDrawable = (AnimationDrawable)