Android TranslateAnimation 实现位移动画(Butterfly)

ImageView background 是一个逐帧动画
TranslateAnimation 动ImageView做位移动操作实现动画效果

activity_butterfly.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"
    tools:context="shortcut.song.com.myapplication.ButterflyActivity">
    <ImageView
        android:id="@+id/img_butterfly"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/butterfly"/>
</LinearLayout>

butterfly.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/butterfly_f01" android:duration="120" />
    <item android:drawable="@drawable/butterfly_f02" android:duration="120" />
    <item android:drawable="@drawable/butterfly_f03" android:duration="120" />
    <item android:drawable="@drawable/butterfly_f04" android:duration="120" />
    <item android:drawable="@drawable/butterfly_f05" android:duration="120" />
    <item android:drawable="@drawable/butterfly_f06" android:duration="120" />
</animation-list>
package shortcut.song.com.myapplication;

import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class ButterflyActivity extends AppCompatActivity {

    private float curX = 0;
    private float curY = 30;

    private float nextX = 0;
    private float nextY = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_butterfly);
        final ImageView imageView = (ImageView)findViewById(R.id.img_butterfly);

        final Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0x123)
                {
                    if ( nextX > 320)
                    {
                        curX = nextX = 0;
                    }
                    else
                    {
                        nextX += nextY;
                    }

                    nextY = curY + (float)(Math.random() * 10 - 5);

                    TranslateAnimation anim = new TranslateAnimation(curX, nextX, curY , nextY);
                    curX = nextX;
                    curY = nextY;
                    anim.setDuration(200);

                    imageView.startAnimation(anim);
                }

            }
        };

        final AnimationDrawable butterfly = (AnimationDrawable)imageView.getBackground();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                butterfly.start();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(0x123);
                    }
                }, 0, 200 );
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongYuLong的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值