Android 补间动画之平移动画TranslateAnimation

Android动画系列

 

博客导航:

 

 

 

 

1.介绍:

Android补间动画之平移动画,在实际的开发过程中,其实有好多地方需要用到平移动画,这是对于平移动画的简单介绍。

 

2.属性

duration时间
fromYDelta

动画开始点的Y轴坐标点,可以用三种方式表示:

1.数字50,表示当前View左上角的Y轴坐标+50px。

2.百分比50%,表示当前View的左上角Y轴坐标+此View的长度的50%。

3.百分数p 50%p,当前View左上角Y轴坐标+父控件长度的50%。

toYDelta动画结束Y轴坐标。
fromXDelta动画开始的X轴坐标。
toXDelta动画结束的X轴坐标。

3.实现方式

3.1 xml方式

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000">
    <translate
        android:fromYDelta="0%p"
        android:toYDelta="80%p"/>
</set>

<!--
duration :时间
fromYDelta :动画开始点的Y轴坐标点,可以用三种方式表示:
             1.数字50,表示当前View左上角的Y轴坐标+50px。
             2.百分比50%,表示当前View的左上角Y轴坐标+此View的长度的50%。
             3.百分数p 50%p,当前View左上角Y轴坐标+父控件长度的50%。
toYDelta :动画结束Y轴坐标。
fromXDelta : 动画开始的X轴坐标。
toXDelta :动画结束的X轴坐标。
-->

3.2 代码方式实现

        TranslateAnimation translateAnimation1 = new TranslateAnimation(0,0,0,1000);
//        使用java代码的方式创建TranslateAnimation,传入六个参数,fromXType、fromXValue、toXType、toXValue和fromYType、fromYValue、toYType、toYValue,使用如下构造方法。
//        参数说明:
//        fromXType:动画开始前的X坐标类型。取值范围为ABSOLUTE(绝对位置)、RELATIVE_TO_SELF(以自身宽或高为参考)、RELATIVE_TO_PARENT(以父控件宽或高为参考)。
//        fromXValue:动画开始前的X坐标值。当对应的Type为ABSOLUTE时,表示绝对位置;否则表示相对位置,1.0表示100%。
//        toXType:动画结束后的X坐标类型。
//        toXValue:动画结束后的X坐标值。
//        fromYType:动画开始前的Y坐标类型。
//        fromYValue:动画开始前的Y坐标值。
//        toYType:动画结束后的Y坐标类型。
//        toYValue:动画结束后的Y坐标值。
        translateAnimation1.setDuration(1000);
        translateAnimation1.setInterpolator(new DecelerateInterpolator());
        translate_img.startAnimation(translateAnimation1)

 

4.动画的监听事件

translateAnimation1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //动画开始
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //动画结束
                translate_img.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //动画重复
            }
        });

5.方法解释

setInterpolator设置插值器解释
LinearInterpolator匀速
AccelerateInterpolator先减速后加速
AnticipateInterpolator动画开始之前有回弹效果
BounceInterpolator结束回弹效果
CycleInterpolator跳一跳效果
OvershootInterpolator动画结束时向前弹一定距离再回到原来位置
AccelerateDecelerateInterpolator系统默认的动画效果,先加速后减速
AnticipateOvershootInterpolator开始之前向前甩,结束的时候向后甩
DecelerateInterpolator开始加速再减速

6.案例实现

6.1 Activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fff"
    tools:context="com.menglong.animatordemo.translate.TranslateActivity">

    <include layout="@layout/titlr_layout"></include>

    <Button
        android:background="#99ff0ff0"
        android:layout_marginTop="100px"
        android:id="@+id/translate_but"
        android:text="平移"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/translate_img"
        android:layout_marginTop="100px"
        android:layout_centerHorizontal="true"
        android:src="@drawable/translate"
        android:layout_width="150px"
        android:layout_height="150px" />

</RelativeLayout>

6.2 Activity代码实现

package com.menglong.animatordemo.translate;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import com.menglong.animatordemo.R;
import com.menglong.animatordemo.base.BaseActivity;

public class TranslateActivity extends BaseActivity implements View.OnClickListener {

    private ImageView translate_img;
    private Animation translateAnimation;
    private Button translate_but;

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

    private void initView() {
        super.initTitle("平移动画");
        translate_img = (ImageView) findViewById(R.id.translate_img);
        translateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate_anim);
//        Interpolator interpolator = new LinearInterpolator();//匀速
//        Interpolator interpolator = new AccelerateInterpolator();//先慢后快
//        Interpolator interpolator = new AnticipateInterpolator();//开始回弹效果
//        Interpolator interpolator = new BounceInterpolator();//结束回弹效果
//        Interpolator interpolator = new CycleInterpolator(2);//跳一跳效果
//        Interpolator interpolator = new OvershootInterpolator(1);//动画结束时向前弹一定距离再回到原来位置
//        Interpolator interpolator = new AccelerateDecelerateInterpolator();//系统默认的动画效果,先加速后减速
//        Interpolator interpolator = new AnticipateOvershootInterpolator();//开始之前向前甩,结束的时候向后甩
        Interpolator interpolator = new DecelerateInterpolator();//开始加速再减速
        translateAnimation.setInterpolator(interpolator);
        translate_but = (Button) findViewById(R.id.translate_but);
        translate_but.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.translate_but:
//                translate_img.startAnimation(translateAnimation);
                code();
                break;
            default:
                break;
        }
    }

    //代码实现方式
    private void code(){
        TranslateAnimation translateAnimation1 = new TranslateAnimation(0,0,0,1000);
//        使用java代码的方式创建TranslateAnimation,传入六个参数,fromXType、fromXValue、toXType、toXValue和fromYType、fromYValue、toYType、toYValue,使用如下构造方法。
//        参数说明:
//        fromXType:动画开始前的X坐标类型。取值范围为ABSOLUTE(绝对位置)、RELATIVE_TO_SELF(以自身宽或高为参考)、RELATIVE_TO_PARENT(以父控件宽或高为参考)。
//        fromXValue:动画开始前的X坐标值。当对应的Type为ABSOLUTE时,表示绝对位置;否则表示相对位置,1.0表示100%。
//        toXType:动画结束后的X坐标类型。
//        toXValue:动画结束后的X坐标值。
//        fromYType:动画开始前的Y坐标类型。
//        fromYValue:动画开始前的Y坐标值。
//        toYType:动画结束后的Y坐标类型。
//        toYValue:动画结束后的Y坐标值。
        translateAnimation1.setDuration(1000);
        translateAnimation1.setInterpolator(new DecelerateInterpolator());
        translate_img.startAnimation(translateAnimation1);
        translateAnimation1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //动画开始
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //动画结束
                translate_img.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //动画重复
            }
        });
    }
}

6.3 translat_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000">
    <translate
        android:fromYDelta="0%p"
        android:toYDelta="80%p"/>
</set>

7.案例效果展示

                                                                         

 

8.项目地址

https://github.com/SunMengLong/AnimatorDemo
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值