Android三种动画之(二)补间动画

介绍说明:补间动画是一种定义开始状态和结束状态,其中播放的时候还能添加设定的一种动画,它的动画方式有平移(TranslateAnimation)、缩放(ScaleAnimation)、旋转(RotateAnimation)、透明度(AlphaAnimation)四个子类,四种变化,也可以将这四种动画结合起来一起使用set

 TweenAnimation补间动画的动画分类

alph 透明度渐变效果动画
scale 缩大缩小渐变效果动画
translate 平移效果动画
rotate 旋转效果动画

TweenAnimation的属性:

java方法 xml属性 效果
setDuration(long) android:duration 动画持续时间,毫秒为单位
setFillAfter(boolean) android:fillAfter 控件动画结束时是否保持动画最后的状态
setFillBefore(boolean) android:fillBefore 控件动画结束时是否还原到开始动画前的状态
setFillEnabled(boolean) android:fillEnabled 与android:fillBefore效果相同
setRepeatCount(int) android:repeatCount 重复次数
setDetachWallpaper(boolean) android:detachWallpaper 是否在壁纸上运行
setInterpolator(Interpolator) android:interpolator 设定插值器(指定的动画效果,譬如回弹等)
setRepeatMode(int) android:repeatMode 重复类型有两个值,reverse表示倒序回放,restart表示从头播放
setZAdjustment(int) android:zAdjustment 表示被设置动画的内容运行时在Z轴上的位置(top/bottom/normal),默认为normal
setStartOffset(long) android:startOffset 调用start函数之后等待开始运行的时间,单位为毫秒

Alpha(渐变)属性详解

java方法 xml属性 效果
AlphaAnimation(float fromAlpha, …) android:fromAlpha 动画开始的透明度(0.0到1.0,0.0是全透明,1.0是不透明)
AlphaAnimation(…, float toAlpha) android:toAlpha 动画结束的透明度(0.0到1.0,0.0是全透明,1.0是不透明)

使用方法1:在xml代码里面设置

在res文件夹下创建anim文件夹代表存放动画资源,在anim文件夹下创建tween_animation.xml文件,命名无要求,示例代码: 

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"/>
<!-- android:duration="2000" 代表动画播放的时间,是四种动画公共的属性-->
<!-- android:fromAlpha="1.0" 和 android:toAlpha="0.0" 是动画自己的属性-->

然后在代码中使用这个动画资源

Animation animation = AnimationUtils.loadAnimation(this, R.anim.tween_animation);
mIvFrame.startAnimation(animation);

使用方法2:在java代码中设置

AlphaAnimation animation=new AlphaAnimation(1,0);//创建动画并设置动画属性
animation.setDuration(2000);//公共属性--动画播放时间
mIvFrame.startAnimation(animation);//使用动画

Rotate(旋转)属性详解

java方法 xml属性 效果
RotateAnimation(float fromDegrees, …) android:fromDegrees 旋转开始角度,正代表顺时针度数,负代表逆时针度数
RotateAnimation(…, float toDegrees, …) android:toDegrees 旋转结束角度,正代表顺时针度数,负代表逆时针度数
RotateAnimation(…, float pivotX, …) android:pivotX 缩放起点X坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%做为初始点、50%p表示以当前View的左上角加上父控件宽高的50%做为初始点)
RotateAnimation(…, float pivotY) android:pivotY 缩放起点Y坐标,同上规律

Scale(缩放)属性详解

java方法 xml属性 效果
ScaleAnimation(float fromX, …) android:fromXScale 初始X轴缩放比例,1.0表示无变化
ScaleAnimation(…, float toX, …) android:toXScale 结束X轴缩放比例
ScaleAnimation(…, float fromY, …) android:fromYScale 初始Y轴缩放比例
ScaleAnimation(…, float toY, …) android:toYScale 结束Y轴缩放比例
ScaleAnimation(…, float pivotX, …) android:pivotX 缩放起点X轴坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%做为初始点、50%p表示以当前View的左上角加上父控件宽高的50%做为初始点)
ScaleAnimation(…, float pivotY) android:pivotY 缩放起点Y轴坐标,同上规律

Translate(平移)属性详解

java方法 xml属性 效果
TranslateAnimation(float fromXDelta, …) android:fromXDelta 起始点X轴坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%做为初始点、50%p表示以当前View的左上角加上父控件宽高的50%做为初始点)
TranslateAnimation(…, float fromYDelta, …) android:fromYDelta 起始点Y轴从标,同上规律
TranslateAnimation(…, float toXDelta, …) android:toXDelta 结束点X轴坐标,同上规律
TranslateAnimation(…, float toYDelta) android:toYDelta 结束点Y轴坐标,同上规律
     

 放出代码;

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/iv_frame"
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/alphaanimation"
            android:layout_width="wrap_content"
            android:text="渐变"
            android:background="@color/colorAccent"
            android:textColor="#fff"
            android:padding="10dp"
            android:layout_margin="10dp"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/scaleanimation"
            android:layout_width="wrap_content"
            android:text="缩放"
            android:background="@color/colorAccent"
            android:textColor="#fff"
            android:padding="10dp"
            android:layout_margin="10dp"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/translateanimation"
            android:layout_width="wrap_content"
            android:text="平移"
            android:background="@color/colorAccent"
            android:textColor="#fff"
            android:padding="10dp"
            android:layout_margin="10dp"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/rotateanimation"
            android:layout_width="wrap_content"
            android:text="旋转"
            android:background="@color/colorAccent"
            android:textColor="#fff"
            android:padding="10dp"
            android:layout_margin="10dp"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/setanimation"
            android:layout_width="wrap_content"
            android:text="组合动画"
            android:background="@color/colorAccent"
            android:textColor="#fff"
            android:padding="10dp"
            android:layout_margin="10dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

MainActivity里面的代码

package com.example.administrator.animation;

import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
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.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mIvFrame;
    private TextView mOne, mTwo, mThree, mFour,mFive;
    private AlphaAnimation alpha;
    private ScaleAnimation scale;
    private RotateAnimation rotate;
    private TranslateAnimation translate;
    private AnimationSet set;

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

    public void initView() {
        mIvFrame = findViewById(R.id.iv_frame);
        mOne = findViewById(R.id.alphaanimation);
        mTwo = findViewById(R.id.scaleanimation);
        mThree = findViewById(R.id.translateanimation);
        mFour = findViewById(R.id.rotateanimation);
        mFive=findViewById(R.id.setanimation);
        mOne.setOnClickListener(this);
        mTwo.setOnClickListener(this);
        mThree.setOnClickListener(this);
        mFour.setOnClickListener(this);
        mFive.setOnClickListener(this);
        alpha=new AlphaAnimation(1,0);//创建动画并设置动画属性
        alpha.setDuration(2000);//公共属性--动画播放时间

        scale=new ScaleAnimation(0,2,0,2,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        scale.setDuration(2000);

        translate = new TranslateAnimation(0,500,0,500);
        translate.setDuration(2000);

        rotate = new RotateAnimation(0,270,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotate.setDuration(2000);

        set=new AnimationSet(true);
        set.addAnimation(alpha);
        set.addAnimation(scale);
        set.addAnimation(rotate);
        set.addAnimation(translate);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.alphaanimation:
                mIvFrame.startAnimation(alpha);//使用动画
                break;

            case R.id.scaleanimation:
                mIvFrame.startAnimation(scale);//使用动画
                break;

            case R.id.translateanimation:
                mIvFrame.startAnimation(translate);//使用动画
                break;
            case R.id.rotateanimation:
                mIvFrame.startAnimation(rotate);//使用动画
                break;

            case R.id.setanimation:
                mIvFrame.startAnimation(set);//使用动画
                break;
        }
    }
}

下载地址:https://download.csdn.net/download/lanrenxiaowen/10755780

猜你喜欢

转载自blog.csdn.net/lanrenxiaowen/article/details/83546514