在布局中加入一个ImageView控件,为其设置动画效果


  • 淡入淡出
AnimationSet animationSet = new AnimationSet(true);
        //0,1表示从完全透明到完全不透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setDuration(2000);//设置动画执行时间
        //将alphaAnimation对象添加到animationSet中
        animationSet.addAnimation(alphaAnimation);
        image.startAnimation(alphaAnimation);

  • 旋转
AnimationSet animationSet = new AnimationSet(true);
        //参数1:从那个旋转角度开始
        //参数2:转到什么角度
        //后四个参数用于设置围绕着旋转的圆的圆心在哪里
        //参数3:确定轴坐标的类型
        //ABSOULUT绝对坐标,RELATIVE_TO_SELF,相对于自身的坐标;RELATIVE_TO_PARENT,相对于父控件的坐标
        //参数4:X轴的值,0.5f表示以自身控件长度一半长度为X轴
        //参数5:确定Y轴坐标类型
        //参数6:Y轴的值,0.5f表示以自身控件长度一半长度为X轴
        RotateAnimation rotateAnimation = new RotateAnimation(0,360,
                Animation.RELATIVE_TO_SELF,1,
                Animation.RELATIVE_TO_SELF,1);
        rotateAnimation.setDuration(100);
        animationSet.addAnimation(animationSet);
        image.startAnimation(animationSet);```

  • 移动
AnimationSet animationSet = new AnimationSet(true);
      Transformation translateAnimation= new Transformation();
        TranslateAnimation translateAnimation = new TranslateAnimation(
                //表示X轴的起始位置 1表示从右下角开始,0表示从父控件中间开始
                Animation.RELATIVE_TO_PARENT,0,
                //表示X轴的结束位置-1表示向左移出父控件
                Animation.RELATIVE_TO_PARENT,-1,
                //表示Y轴的起始位置 1表示从右下角开始,0表示从父控件中间开始
                Animation.RELATIVE_TO_PARENT,0,
                //表示Y轴的结束位置 -1表示向上移出父控件
                Animation.RELATIVE_TO_PARENT,-1
        );
        translateAnimation.setDuration(2000);
        animationSet.addAnimation(translateAnimation);
        image.startAnimation(animationSet);```

  • 缩放
AnimationSet animationSet = new AnimationSet(true);
        //参数1:X轴的初始值,>1表示由大变小,<1表示由小变大
        //参数2:X轴收缩后的值,表示动画结束时相对于自身控件倍
        //参数3:Y轴的初始值>1表示由大变小,<1表示由小变大
        //参数4:Y轴收缩后的值,表示动画结束时相对于自身控件Y倍
        //参数5:确定X轴坐标的类型
        //参数6:X轴的值,0.5f表示以自身控件一半长度为X轴,表示动画起始位置
        //参数7:确定Y轴坐标的类型
        //参数8:Y轴的值,0.5f表示以自身控件一半长度为Y轴,表示动画起始位置
        ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,
                Animation.RELATIVE_TO_PARENT,0.5f,
                Animation.RELATIVE_TO_PARENT,0.5f);
                scaleAnimation.setDuration(3000);
                animationSet.addAnimation(scaleAnimation);
                image.startAnimation(animationSet);

  • -

XML文件

<?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="com.neo.hello.Second">

    <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/btnRotate"
         android:text="旋转"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnScale"
        android:text="缩放"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnAlpha"
        android:text="淡入淡出"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnTraslate"
        android:text="移动"
        />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageBackground"
            android:layout_gravity="center"
            android:src="@drawable/picture"
            />


    </FrameLayout>


</LinearLayout>

代码

package com.neo.hello;

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.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class Second extends AppCompatActivity implements View.OnClickListener {
    private ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        findViewById(R.id.btnRotate).setOnClickListener(this);
        findViewById(R.id.btnScale).setOnClickListener(this);
        findViewById(R.id.btnAlpha).setOnClickListener(this);
        findViewById(R.id.btnTraslate).setOnClickListener(this);
        image = (ImageView) findViewById(R.id.imageBackground);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnRotate:
                startRotate();
                break;
            case R.id.btnScale:
                startScale();
                break;
            case R.id.btnAlpha:
                startAlpha();
                break;
            case R.id.btnTraslate:
                startTraslate();
                break;

        }
    }
    //启动移动动画
    private void startTraslate() {
        AnimationSet animationSet = new AnimationSet(true);
        //参数1,2:
        //参数3,4:Y轴的起始位置
        //参数5,6:X轴的结束位置
        //参数7,8:轴的Y结束位置
//        Transformation translateAnimation= new Transformation();
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT,0,//表示X轴的起始位置 1表示从右下角开始,0表示从父控件中间开始
                Animation.RELATIVE_TO_PARENT,-1,//表示X轴的结束位置-1表示向左移出父控件
                Animation.RELATIVE_TO_PARENT,0,//表示Y轴的起始位置 1表示从右下角开始,0表示从父控件中间开始
                Animation.RELATIVE_TO_PARENT,-1//表示Y轴的结束位置 -1表示向上移出父控件
        );
        translateAnimation.setDuration(2000);
        animationSet.addAnimation(translateAnimation);
        image.startAnimation(animationSet);
    }
    //启动淡入淡出动画
    private void startAlpha() {
        //创建一个Animationset对象,参数为Boolean型,true表示使用Animation的interpolator,false则是使用自定义的
        AnimationSet animationSet = new AnimationSet(true);
        //0,1表示从完全透明到完全不透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setDuration(2000);//设置动画执行时间
        //将alphaAnimation对象添加到animationSet中
        animationSet.addAnimation(alphaAnimation);
        image.startAnimation(alphaAnimation);

    }
    //启动缩放动画
    private void startScale() {
        AnimationSet animationSet = new AnimationSet(true);
        //参数1:X轴的初始值,>1表示由大变小,<1表示由小变大
        //参数2:X轴收缩后的值,表示动画结束时相对于自身控件倍
        //参数3:Y轴的初始值>1表示由大变小,<1表示由小变大
        //参数4:Y轴收缩后的值,表示动画结束时相对于自身控件Y倍
        //参数5:确定X轴坐标的类型
        //参数6:X轴的值,0.5f表示以自身控件一半长度为X轴,表示动画起始位置
        //参数7:确定Y轴坐标的类型
        //参数8:Y轴的值,0.5f表示以自身控件一半长度为Y轴,表示动画起始位置
        ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,
                Animation.RELATIVE_TO_PARENT,0.5f,
                Animation.RELATIVE_TO_PARENT,0.5f);
        scaleAnimation.setDuration(3000);
        animationSet.addAnimation(scaleAnimation);
        image.startAnimation(animationSet);


    }
    //启动旋转动画
    private void startRotate() {
        AnimationSet animationSet = new AnimationSet(true);
        //参数1:从那个旋转角度开始
        //参数2:转到什么角度
        //后四个参数用于设置围绕着旋转的圆的圆心在哪里
        //参数3:确定轴坐标的类型
        //ABSOULUT绝对坐标,RELATIVE_TO_SELF,相对于自身的坐标;RELATIVE_TO_PARENT,相对于父控件的坐标
        //参数4:X轴的值,0.5f表示以自身控件长度一半长度为X轴
        //参数5:确定Y轴坐标类型
        //参数6:Y轴的值,0.5f表示以自身控件长度一半长度为X轴
        RotateAnimation rotateAnimation = new RotateAnimation(0,360,
                Animation.RELATIVE_TO_SELF,1,
                Animation.RELATIVE_TO_SELF,1);
        rotateAnimation.setDuration(100);
        animationSet.addAnimation(animationSet);
        image.startAnimation(animationSet);
    }
}