动画

<?xml version="1.0" encoding="utf-8"?>
<!--透明度的变化  渐变


    0.0  不可见,  1.0  可见


    android:fromAlpha=""    开始的透明度
    android:toAlpha=""      结束的透明度


    通用属性:
    android:duration=""     持续时间  毫秒
    android:fillAfter=""   是否保存结束时的状态
    android:repeatMode="reverse"  设置重复的模式
        reverse     反向执行
        restart     再次执行
    android:repeatCount = "2"   重复的次数   最终执行的次数 = 重复次数 + 执行 1


-->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"


    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="3000"
    android:fillAfter="true"
    android:repeatMode="reverse"
    android:repeatCount = "2"
    >

</alpha>

<?xml version="1.0" encoding="utf-8"?>
<!--旋转
      android:fromDegrees=""  开始角度
    android:toDegrees=""       结束的角度
    android:pivotX=""           X轴的中心的
    android:pivotY=""           Y轴的中心的
-->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"


    android:fromDegrees="0"
    android:toDegrees="720"
    android:pivotX="50%"
    android:pivotY="50%"


    android:duration="3000"
    >


</rotate>

<?xml version="1.0" encoding="utf-8"?>


<!--缩放
    android:fromXScale=""       X轴起始比例
    android:toXScale=""         X轴的结束比例


    android:fromYScale=""       Y轴起始比例
    android:toYScale=""         Y轴的结束比例


     android:pivotX=""          X轴的中心的
    android:pivotY=""           Y轴的中心的
-->
<scale xmlns:android="http://schemas.android.com/apk/res/android"


    android:fromXScale="1"
    android:toXScale="3"
    android:fromYScale="1"
    android:toYScale="3"
    android:pivotX="50%"
    android:pivotY="50%"


    android:duration="3000"
    >


</scale>

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


    <alpha android:fromAlpha="1.0"
            android:toAlpha="0.0"
        android:duration = "3000"/>




    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="800"
        android:toYDelta="800"
        android:duration = "3000"
        />


</set>

<?xml version="1.0" encoding="utf-8"?>


<!--位移


    android:fromXDelta=""       X轴的起始位置
    android:toXDelta=""         X轴的结束位置


    android:fromYDelta=""       Y轴的起始位置
    android:toYDelta=""         Y轴的结束位置
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"  插值器
    `                       @android:anim/bounce_interpolator  弹跳




-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"


    android:fromXDelta="0"
    android:toXDelta="0"


    android:fromYDelta="0"
    android:toYDelta="1000"


    android:duration = "3000"
    android:interpolator="@android:anim/bounce_interpolator"
    >


</translate>


package bw.com.bw_day02_animation.demo03;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import bw.com.bw_day02_animation.R;

/**
 * 资源文件创建的补间动画
 * 1, res 的文件夹中 , 创建 anim文件夹
 * 2, 在res/anim 文件夹中, 创建一个xml 个是的动画, 根节点<alpha , scale, rotate,translate, set>
 * 3, 在Activity 中, 启动动画
 *     mIv.startAnimation(AnimationUtils.loadAnimation(上下文对象, 动画的资源id));
 */
public class TweenActivity01 extends AppCompatActivity {

    private ImageView mIv;

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

        mIv = (ImageView) findViewById(R.id.iv_id);
    }

    //点击按钮, 切换动画
    public void onClick(View view) {

        switch (view.getId())
        {
            case R.id.but_01:
                //渐变
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.alpha_anim));
                break;
            case R.id.but_02:
                //缩放
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.scale_anim));
                break;
            case R.id.but_03:
                //旋转
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.rotate_anim));
                break;
            case R.id.but_04:
                //位移
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.translate_anim));
                break;
            case R.id.but_05:
                //集合
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.set_anim));
                break;

        }
    }
}
//代码创建动画
package bw.com.bw_day02_animation.demo04;

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;

import bw.com.bw_day02_animation.R;

/**
 * 代码创建补间动画
 *
 * 1, 声明每一种动画资源, 设置参数信息
 * 2, 设置持续时间
 * 3, 为控件启动动画
 *      mIv.startAnimation(animation);
 *
 */
public class TweenActivity02 extends AppCompatActivity {

    private ImageView mIv;

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

        mIv = (ImageView) findViewById(R.id.iv_id);
    }


    public void onClick(View view) {

        switch (view.getId())
        {
            case R.id.but_01:
                //渐变
                //开始的透明度 , 结束的透明度
                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);//创建动画资源
                alphaAnimation.setDuration(3000);  //设置持续时间
                alphaAnimation.setFillAfter(true);//保持结束时的状态
                mIv.startAnimation(alphaAnimation);//为图片启动动画

                break;

            case R.id.but_02:
                //缩放
                /**
                 * X轴的起始比例
                 * X轴的结束比例
                 * Y轴的起始比例
                 * Y轴的结束比例
                 * X轴中心点的参考
                 * X轴的中心位置
                 * Y轴中心点的参考
                 * Y轴的中心位置
                 */
               // ScaleAnimation scaleAnimation = new ScaleAnimation(1f,3f,1f,3f);//左上角为参考点
                ScaleAnimation scaleAnimation = new ScaleAnimation(1f,3f,1f,3f,
                        Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f
                        );//以X和Y 的中心的为参考

                scaleAnimation.setDuration(3000);

                mIv.startAnimation(scaleAnimation);

                break;

            case R.id.but_03:
                //旋转
                RotateAnimation rotateAnimation = new RotateAnimation(0,720,
                        Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f
                        );

                rotateAnimation.setDuration(3000);
                mIv.startAnimation(rotateAnimation);
                break;


            case R.id.but_04:
                //位移
                TranslateAnimation translateAnimation = new TranslateAnimation(0,800,0,800);
                translateAnimation.setDuration(3000);
                mIv.startAnimation(translateAnimation);
                break;

            case R.id.but_05:
                //集合
                //1, 实例化动画集合的对象
                //true  代表使用结合的插值器,  false  代表使用动画自己的插值器
                AnimationSet animationSet = new AnimationSet(false);

                //2, 创建每一个动画
                TranslateAnimation tAnimation = new TranslateAnimation(0,300,0,300);
                AlphaAnimation aAnimation = new AlphaAnimation(1.0f,0.0f);

                //3, 把每个动画, 添加到集合中
                animationSet.addAnimation(tAnimation);
                animationSet.addAnimation(aAnimation);

                //4, 为集合动画, 设置属性
                animationSet.setDuration(3000);
                animationSet.setFillAfter(true);

                //5, 为图片启动动画
                mIv.startAnimation(animationSet);

                break;
        }
    }
}


代码创建帧动画

package bw.com.bw_day02_animation.demo02;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import bw.com.bw_day02_animation.R;

/**
 *  代码的方式创建帧动画
 *  1, 初始化动画管理器对象
 *      AnimationDrawable drawable = new AnimationDrawable();
 *  2, 向动画管理器中添加图片资源
 *      drawable.addFrame(Drawable类型的图片, 持续时间 毫秒);
 *  3, 设置图片是否执行一次
 *      drawable.setOneShot(false); // true 执行一次; false 执行多次
 *  4, 将动画资源, 设置到图片中
 *      mIv.setImageDrawable(drawable);
 *  5, 调用方法 start() 开启动画  --- drawable.isRunning()  判断动画是否正在执行
 *      drawable.start();
 *  6, 调用方法 stop() 停止动画
 *      drawable.stop();
 */
public class FrameAnimationActivity02 extends AppCompatActivity {

    private ImageView mIv;

    private AnimationDrawable animationDrawable;

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

        mIv = (ImageView) findViewById(R.id.iv_id);

        //1, 初始化动画资源管理器对象
         animationDrawable = new AnimationDrawable();

        //2, 向动画资源中, 添加图片
        //Drawable类型的图片 ,  持续时间
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0001),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0002),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0003),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0004),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0005),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0006),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0007),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0008),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0009),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0010),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0011),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0012),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0013),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0014),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0015),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0016),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0017),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0018),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0019),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0020),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0021),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0022),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0023),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0024),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0025),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0026),100);

        //3, 设置动画是否执行一次 -- true :  只执行一次;    false  执行多次
        animationDrawable.setOneShot(false);

        //4, 将动画资源设置到图片控件中
        mIv.setImageDrawable(animationDrawable);
    }

    //4, 开启动画
    public void start(View view) {
        if(!animationDrawable.isRunning())
        {
            animationDrawable.start();
        }
    }

    //5, 结束动画
    public void stop(View view) {
        animationDrawable.stop();
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值