Android动画之逐帧动画
2016-07-21 10:02
471 查看
Frame Animation
在学习http://blog.csdn.net/liuhe688/article/details/6657776这篇文章的逐帧动画时,遇到很多问题,但是通过自己的不懈努力,最后还是解决了。<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.animation.frameanimation.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" android:id="@+id/btn_start" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结束" android:id="@+id/btn_stop" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv_anim" android:layout_below="@+id/btn_start" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/btn_stop" android:layout_alignEnd="@+id/btn_stop"/> </RelativeLayout>
下面是动画xml代码fat_po.xml
<?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/girl_1" android:duration="100"/> <item android:drawable="@drawable/girl_2" android:duration="100"/> <item android:drawable="@drawable/girl_3" android:duration="100"/> <item android:drawable="@drawable/girl_4" android:duration="100"/> <item android:drawable="@drawable/girl_5" android:duration="100"/> <item android:drawable="@drawable/girl_6" android:duration="100"/> <item android:drawable="@drawable/girl_7" android:duration="100"/> <item android:drawable="@drawable/girl_8" android:duration="100"/> <item android:drawable="@drawable/girl_9" android:duration="100"/> <item android:drawable="@drawable/girl_10" android:duration="100"/> <item android:drawable="@drawable/girl_11" android:duration="100"/> </animation-list>
接下来是MainActivity的代码
package com.example.animation.frameanimation; import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.Drawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.animation.Animation; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { Button button; Button button2; ImageView imageView; AnimationDrawable animation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.btn_start); button2 = (Button) findViewById(R.id.btn_stop); imageView = (ImageView) findViewById(R.id.iv_anim); imageView.setBackgroundResource(R.drawable.fat_po);//这里应该调用setBackgroundResource()方法,而不是setImageResource(),否则下面imageView.getBackground()会报空。 animation = (AnimationDrawable)imageView.getBackground(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animation.start(); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animation.stop(); } }); } }
相关文章推荐
- Android Studio创建的Android项目一般需要忽略
- geekband android #5 第一周分享(初识Android)
- android studio入门
- About Location of the android sdk has not been setup in the preferences
- 安卓端打电话和发送短信
- android命名规范
- 如何在mac本上安装android sdk
- MaterialDesign设计风格之自定义toolbar的简单实现
- Android Studio快捷键
- 详解Android中AsyncTask的使用
- Android studio中.so文件的提交
- Android的两种上下文的区别
- Android Studio 使用Gradle引入第三方库文件的总结
- Android学习进阶和IoC
- 收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation
- Android 6.0下出错: UnsatisfiedLinkError: dlopen failed . text relocations
- Android API之ExternalStorage操作
- Android中国开发精英必须知道的内容
- 关于shape
- android---重写TextView实现跑马灯