android 中的一个动画,3种方式实现。

这里写图片描述
这里写图片描述

实现这个小猪的来回跑动

所需共同的是让小猪看起来再跑的样子,需要在drawable下建立两个animation-list

pig1_2.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@mipmap/pig1"
        android:duration="100" />
    <item
        android:drawable="@mipmap/pig2"
        android:duration="100" />
</animation-list>

pig3_4.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@mipmap/pig3"
        android:duration="100" />
    <item
        android:drawable="@mipmap/pig4"
        android:duration="100" />
</animation-list>

第一种方式,使用两只猪跑,一只向左跑,一只向右跑,适当时间控制就好

在res下建立anim文件夹,在里面写自己想要的动画

run1.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="10000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:repeatCount="infinite"
        android:toXDelta="200%p"
        android:toYDelta="0" />
</set>
run2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="10000"
        android:fromXDelta="120%p"
        android:fromYDelta="0"
        android:toXDelta="-100%p"
        android:repeatCount="infinite"
        android:toYDelta="0" />
</set>

main

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //第一只小猪
        TextView tv = (TextView) findViewById(R.id.main_iv);
        //帧动画的设置
        AnimationDrawable animationDrawable = (AnimationDrawable) tv.getBackground();
        animationDrawable.start();
        //补间动画的设置
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.run1);
        tv.setAnimation(animation);


        //对第二个小猪图片
        TextView tv2 = (TextView) findViewById(R.id.main_iv2);
        //帧动画的设置
        AnimationDrawable animationDrawable2 = (AnimationDrawable) tv2.getBackground();
        animationDrawable2.start();
        //补间动画的设置
        Animation animation2 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.run2);
        tv2.setAnimation(animation2);
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/background"
    tools:context="com.example.anim_pig.MainActivity">

    <TextView
        android:id="@+id/main_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="130dp"
        android:background="@drawable/pig1_2" />

    <TextView
        android:id="@+id/main_iv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="150dp"
        android:background="@drawable/pig3_4" />
</LinearLayout>

第二种方式一个小猪,用一个线程来控制,把背景资源和动画资源换掉

anim下的动画

run3.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="85%p"
    android:toYDelta="0"
    >

</translate>
run4.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromXDelta="90%p"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="0">

</translate>

MyActivity

public class MyActivity extends AppCompatActivity {

    //布局内的控件
    TextView tv;
    //一个游标值,单数显示第一个动画,双数显示第二个动画
    int index = 0;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        //实例化小猪
        tv = (TextView) findViewById(R.id.main_iv);
        runAlong(R.anim.run3);
    }

    /**
     * 这个方法涉及到不断的递归
     *
     * @param run
     */
    private void runAlong(int run) {
        if (index % 2 == 0) {
            //设置单次的TextView的background属性
            //帧动画的值
            tv.setBackgroundResource(R.drawable.pig1_2);
            //补间动画的属性值
            run = R.anim.run3;
        }
        //帧动画的设置
        AnimationDrawable animationDrawable = (AnimationDrawable) tv.getBackground();
        animationDrawable.start();
        //补间动画的设置
        Animation animation = AnimationUtils.loadAnimation(this, run);
        tv.setAnimation(animation);
        //给补间动画设置监听事件
        animation.setAnimationListener(new Animation.AnimationListener() {

            //动画结束时开始另一个动画
            @Override
            public void onAnimationEnd(Animation animation) {
                //游标值加1
                index++;
                //设置第二次的TextView的background属性
                //帧动画的值
                tv.setBackgroundResource(R.drawable.pig3_4);
                //设置补间动画的属性值,并且调用方法
                runAlong(R.anim.run4);
            }

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }
}

activity_my

<?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"
    android:background="@mipmap/background"
    >

    <TextView
        android:id="@+id/main_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="250dp"
        android:background="@drawable/pig1_2" />


</LinearLayout>

第三种方式,一个小猪,根据动画的点击事件设置

anim下的动画

run5
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="4000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="650"
        android:toYDelta="0"></translate>

</translate>
run6
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="4000"
        android:fromXDelta="650"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="0" />
</translate>

Three

public class Three extends AppCompatActivity{



    TextView pig3;
    TextView pig6;
    Animation animationAA;
    Animation animationBB;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
         pig3= (TextView) findViewById(R.id.pig3);
        pig6= (TextView) findViewById(R.id.pig6);
        AnimationDrawable drawable3 = (AnimationDrawable) pig3.getBackground();
        drawable3.start();


        animationBB = AnimationUtils.loadAnimation(this,R.anim.run6);
        animationBB.setRepeatCount(3);
        animationBB.setRepeatMode(Animation.RELATIVE_TO_PARENT);
        animationBB.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                pig6.setBackgroundResource(R.drawable.pig1_2);
                AnimationDrawable drawable = (AnimationDrawable) pig6.getBackground();
                drawable.start();
                pig6.setAnimation(animationAA);
            }
        });

        AnimationDrawable drawable = (AnimationDrawable) pig6.getBackground();
        drawable.start();
        animationAA = AnimationUtils.loadAnimation(this,R.anim.run5);

        animationAA.setRepeatCount(3);
        animationAA.setRepeatMode(Animation.RELATIVE_TO_PARENT);

        pig6.setAnimation(animationAA);
        animationAA.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                pig6.setBackgroundResource(R.drawable.pig1_2);
                AnimationDrawable drawable = (AnimationDrawable) pig6.getBackground();
                drawable.start();
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                pig6.setBackgroundResource(R.drawable.pig3_4);
                AnimationDrawable drawable = (AnimationDrawable) pig6.getBackground();
                drawable.start();
                pig6.setAnimation(animationBB);
            }
        });
    }


}

activity_three

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@mipmap/background"
    android:layout_height="match_parent">



    <TextView
        android:layout_marginTop="200dp"
        android:id="@+id/pig3"
        android:layout_width="40dp"
        android:layout_height="30dp"
        android:background="@drawable/pig1_2"/>




    <TextView
        android:layout_marginTop="350dp"
        android:id="@+id/pig6"
        android:layout_width="40dp"
        android:layout_height="30dp"
        android:background="@drawable/pig1_2"/>

</RelativeLayout>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值