android帧动画,有两种,一种是在xml,一种是在代码;下面我们来看看怎么逻辑实现;
1.首先是在res/drawable文件夹下添加img图,如15张-不等;
在drawable添加xml文件android_frame_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- oneshot false设置会一直播放,true只播放一遍-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item android:drawable="@mipmap/image_icon1" android:duration="150"/>
<item android:drawable="@mipmap/image_icon2" android:duration="150"/>
<item android:drawable="@mipmap/image_icon3" android:duration="150"/>
<item android:drawable="@mipmap/image_icon4" android:duration="150"/>
<item android:drawable="@mipmap/image_icon5" android:duration="150"/>
<item android:drawable="@mipmap/image_icon6" android:duration="150"/>
<item android:drawable="@mipmap/image_icon7" android:duration="150"/>
<item android:drawable="@mipmap/image_icon8" android:duration="150"/>
<item android:drawable="@mipmap/image_icon9" android:duration="150"/>
<item android:drawable="@mipmap/image_icon10" android:duration="150"/>
<item android:drawable="@mipmap/image_icon11" android:duration="150"/>
<item android:drawable="@mipmap/image_icon13" android:duration="150"/>
<item android:drawable="@mipmap/image_icon14" android:duration="150"/>
<item android:drawable="@mipmap/image_icon15" android:duration="150"/>
</animation-list>
res/layout创建布局;
main_frame_animation_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="230dp"
android:layout_height="70dp"
android:layout_centerInParent="true"
android:background="@drawable/android_frame_animation"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="8dip"
android:paddingTop="8dip">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/butt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
看mainActivity方法;
package com.personal.myapplication;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button bt1;
private ImageView image3;
private AnimationDrawable ani;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_frame_animation_layout);
bt1=(Button)findViewById(R.id.butt1);
image3=(ImageView)findViewById(R.id.imageView1);
image3.setBackgroundResource(R.drawable.vargo_user_dialog);
ani = (AnimationDrawable)image3.getBackground();
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ani.start();
}
});
}
}
第二种,代码实现
/**
* 通过代码添加帧动画方法
*/
public void setCodeFrameAnim() {
animationDrawable = new AnimationDrawable();
// 为AnimationDrawable添加动画帧
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon1), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon2), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon3), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon4), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon5), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon6), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon7), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon8), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon9), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon10), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon11), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon12), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon13), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon14), 150);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.image_icon15), 150);
// 设置为循环播放
animationDrawable.setOneShot(false);
imageView.setBackground(animationDrawable);
以上就算完成了…