关闭

逐帧动画

标签: 动画
157人阅读 评论(0) 收藏 举报
分类:

1、需要在drawable文件夹下添加一组图片,并且写一个my_animation.xml的文件(内容如下),其中false代表循环播放,duration代表帧的时间间隔

<?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/aa" android:duration="60"/>
    <item android:drawable="@drawable/bb" android:duration="60"/>
    <item android:drawable="@drawable/cc" android:duration="60"/>
    <item android:drawable="@drawable/dd" android:duration="60"/>
    <item android:drawable="@drawable/ee" android:duration="60"/>
    <item android:drawable="@drawable/ff" android:duration="60"/>
    <item android:drawable="@drawable/gg" android:duration="60"/>
    <item android:drawable="@drawable/hh" android:duration="60"/>

</animation-list>

2、activity代码

package com.example.administrator.myanimation;

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;

/**
 * Created by Administrator on 2015/11/20.
 */
public class FrameAnimationActivity extends AppCompatActivity {
    private Button mButtonStart;
    private Button mButtonStop;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame);
        mButtonStart = (Button) findViewById(R.id.button_start);
        mButtonStop = (Button) findViewById(R.id.button_stop);
        mImageView = (ImageView) findViewById(R.id.imageview);
        final AnimationDrawable anim = (AnimationDrawable) mImageView.getBackground();
        mButtonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                anim.start();
            }
        });
        mButtonStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                anim.stop();
            }
        });
    }
}

3、布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"/>
    <Button
        android:id="@+id/button_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结束"/>
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="70dp"
        android:layout_height="100dp"
        android:background="@drawable/my_animation"/>

</LinearLayout>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22303次
    • 积分:786
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类