第17天-01-补间动画两种实现方式

第一种方式,直接写代码
mainactivity代码:

package com.glsite.tweenanim;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView mIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIv = findViewById(R.id.iv);
    }

    /**
     * 透明变化的动画
     * @param view
     */

    public void alpha(View view) {
        //0.0表示完全透明,1.0表示完全不透明
        AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
        //持续时间2秒
        aa.setDuration(2000);
        //重复次数2次
        aa.setRepeatCount(2);
        //重复模式,反转,即从1.0到0.0,如果换成restart,那么就是还是从0.0到1.0
        aa.setRepeatMode(Animation.REVERSE);
        mIv.startAnimation(aa);
    }
    /**
     *透明缩放的动画
     * @param view
     */

    public void scale(View view) {
        //x的值从0变为了原来的两倍,y同理.relative_to_self表示以自身为参照。0.5表示原来的中心点
        ScaleAnimation sa = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(2000);
        sa.setRepeatCount(2);
        sa.setRepeatMode(Animation.REVERSE);
        mIv.startAnimation(sa);
    }
    /**
     *透明位移的动画
     * @param view
     */

    public void trans(View view) {
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        ta.setDuration(2000);
        ta.setRepeatCount(2);
        ta.setRepeatMode(Animation.REVERSE);
        mIv.startAnimation(ta);
    }
    /**
     *透明旋转的动画
     * @param view
     */

    public void rotate(View view) {
        RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        );
        ra.setDuration(2000);
        ra.setRepeatCount(2);
        ra.setRepeatMode(Animation.REVERSE);
        mIv.startAnimation(ra);

    }
    /**
     *动画变化集合
     * @param view
     */

    public void set(View view) {
        AnimationSet set = new AnimationSet(false);
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        ta.setDuration(2000);
        ta.setRepeatCount(2);
        ta.setRepeatMode(Animation.REVERSE);


        RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        );
        ra.setDuration(2000);
        ra.setRepeatCount(2);
        ra.setRepeatMode(Animation.REVERSE);


        ScaleAnimation sa = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(2000);
        sa.setRepeatCount(2);
        sa.setRepeatMode(Animation.REVERSE);


        set.addAnimation(ta);
        set.addAnimation(ra);
        set.addAnimation(sa);
        mIv.startAnimation(set);
    }
}

布局文件代码:

<?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=".MainActivity"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="alpha"
        android:text="alpa"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="scale"
        android:text="scale"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="trans"
        android:text="trans"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="rotate"
        android:text="rotate"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="set"
        android:text="set"/>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>



</LinearLayout>

第二种方式,借助布局文件
mainactivity代码:

package com.glsite.tweenanim;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity2 extends AppCompatActivity {

    private ImageView mIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIv = findViewById(R.id.iv);
    }

    /**
     * 透明变化的动画
     * @param view
     */

    public void alpha(View view) {
        Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);
        mIv.startAnimation(aa);
    }


    /**
     *透明缩放的动画
     * @param view
     */

    public void scale(View view) {
        Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);
        mIv.startAnimation(sa);

    }
    /**
     *透明位移的动画
     * @param view
     */

    public void trans(View view) {
        Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans);
        mIv.startAnimation(ta);

    }
    /**
     *透明旋转的动画
     * @param view
     */

    public void rotate(View view) {
        Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);
        mIv.startAnimation(ra);

    }
    /**
     *动画变化集合
     * @param view
     */

    public void set(View view) {
        Animation sa = AnimationUtils.loadAnimation(this, R.anim.set);
        mIv.startAnimation(sa);

    }
}

alpha.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1.0"
    android:repeatCount="2"
    android:repeatMode="reverse"
    android:duration="2000">

</alpha>

rotate.xml:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000"
    android:repeatCount="2"
    android:repeatMode="reverse">

</rotate>

scale.xml:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="2.0"
    android:toYScale="2.0"
    android:repeatCount="2"
    android:repeatMode="reverse">

</scale>

set.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="2"
        android:repeatMode="reverse"
        android:toDegrees="360">

    </rotate>


    <scale
        android:duration="2000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="2"
        android:repeatMode="reverse"
        android:toXScale="2.0"
        android:toYScale="2.0">

    </scale>

    /


    <translate
        android:duration="2000"
        android:fromXDelta="-50%"
        android:fromYDelta="-50%"
        android:repeatMode="reverse"
        android:toXDelta="50%"
        android:toYDelta="50%">

    </translate>


</set>

trans.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="-50%"
    android:fromYDelta="-50%"
    android:toXDelta="50%"
    android:toYDelta="50%"
    android:duration="2000"
    android:repeatMode="reverse">

</translate>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值