推荐阅读:
Android动画之补间动画用法最全详解
Android 属性动画:一文让你完全了解和掌握属性动画用法java
在Android中,帧动画的本质是把一组预先准备好的图片循环切换播放,形成一种动画效果。
android
实现帧动画有两种方式,即xml
和java
web
把准备的素材放到drawable目录
app
在drawable
目录下建立一个animation_flower.xml
的文件,往文件中添加ide
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">// 是否只播放一次 false 循环播放 <item android:drawable="@drawable/img01" android:duration="200" /> <item android:drawable="@drawable/img02" android:duration="200" /> <item android:drawable="@drawable/img03" android:duration="200" /> <item android:drawable="@drawable/img04" android:duration="200" /> <item android:drawable="@drawable/img05" android:duration="200" /> </animation-list>
布局文件:布局文件中添加两个点击按钮和一个图片控件
svg
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:text="开始动画" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="结束动画" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" /> <ImageView android:id="@+id/image" android:layout_width="200dp" android:layout_height="200dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:background="@drawable/animation_flower" app:layout_constraintTop_toBottomOf="@+id/button2" /> </androidx.constraintlayout.widget.ConstraintLayout>
在Activity
中添加启动&中止动画代码布局
public class MainActivity extends AppCompatActivity { Button mButtonStart; Button mButtonStop; ImageView mImageViewShow; AnimationDrawable mAnimationDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageViewShow = findViewById(R.id.image); // 获取动画对象 mAnimationDrawable = (AnimationDrawable) mImageViewShow.getBackground(); mButtonStart = findViewById(R.id.button); mButtonStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //开始动画 mAnimationDrawable.start(); } }); mButtonStop = findViewById(R.id.button2); mButtonStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //结束动画 mAnimationDrawable.stop(); } }); } }
Java代码实现帧动画跟xml很相似,只有Activity部分有点区别动画
public class MainActivity extends AppCompatActivity { Button mButtonStart; Button mButtonStop; ImageView mImageViewShow; AnimationDrawable mAnimationDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageViewShow = findViewById(R.id.image); // 获取动画对象 mAnimationDrawable =new AnimationDrawable(); mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img01),200); mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img02),200); mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img03),200); mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img04),200); mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img05),200); mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img06),200); mAnimationDrawable.setOneShot(false);//设置循环播放 mImageViewShow.setBackground(mAnimationDrawable); mButtonStart = findViewById(R.id.button); mButtonStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //开始动画 mAnimationDrawable.start(); } }); mButtonStop = findViewById(R.id.button2); mButtonStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //结束动画 mAnimationDrawable.stop(); } }); } }
做者:lucashu
出处:https://blog.csdn.net/huweiliyi/article/details/105669298
原创不易,欢迎转载,但未经做者赞成请保留此段声明,并在文章页面明显位置给出原文连接。spa