Android基础:动画二:XML创建动画

创建逻辑:

1 准备动画资源:在res/anim文件夹下创建4个xml文件
2 加载动画:AnimationUtils.loadAnimation(Context context, int id)
3 开启动画:View.startAnimation(Animation animation)

相关的类:

动画创建方式获取Animation对象的方式例子
帧动画drawable animation代码创建getBackground()AnimationDrawable animation = (AnimationDrawable) imageView.getBackground();
xml不需要获取android:background=”@drawable/animation_main”
补间动画View Animation代码创建new …RotateAnimation animation = new RotateAnimation(1, 10);
xml创建AnimationUtils.loadAnimation(…)Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_translate)
属性动画Property Animator代码创建ObjectAnimator.ofFloat(…)ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, “scaleX”, 1, 3, 1, 3,5, -2)
xml创建AnimatorInflater.loadAnimator(…)ObjectAnimator oa = (ObjectAnimator) AnimatorInflater.loadAnimator(context, R.animator.alpha)
AnimationUtils:动画工具类(Android自带)

分类

rotate + alpha + scale + translate

相关的属性:

4种动画只有transition使用百分比。

android:duration=       动画时长
android:repeatCount=    重复次数(0是不重复)
android:repeatMode=     重复的模式
android:fillAfter=      是否停留在动画结束的位置
android:startOffset=    动画延迟时间

rotate:
    android:fromDegrees= 开始的角度
    android:toDegrees=   结束的角度
    android:pivotX=      轴心的X坐标
    android:pivotY=      轴心的Y坐标
    pivotX+pivotY:加%表示相对于自身,不加%表示像素:移动几个像素的距离

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


scale:
    android:fromXScale=    开始时X轴缩放比
    android:toXScale=      结束时X轴缩放比
    android:fromYScale=    开始时Y轴缩放比
    android:toYScale=      结束时Y轴缩放比

translate
     Delta:加%表示相对于自身,不加%表示像素:移动几个像素的距离
     android:fromXDelta=   开始时轴心X轴的坐标,
     android:fromYDelta=   开始时轴心Y轴的坐标
     android:toXDelta=     结束时轴心X轴的坐标
     android:toYDelta=     结束时轴心Y轴的坐标

效果图:

这里写图片描述

1 准备动画资源:

anim_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fillAfter="true"
    android:fromAlpha="1"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:startOffset="2000"
    android:toAlpha="0.2" >
<!--startOffset:延迟时间  -->
</alpha>

anim_rotate.xml

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

</rotate>

anim_scale.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
    android:fromXScale="1"                
    android:fromYScale="1"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:toXScale="0.5"
    android:toYScale="0.5" >
<!--   android:startOffset="1000" 延迟1秒 -->
</scale>

anim_translate.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:toXDelta="300%"
    android:toYDelta="300%" >
<!-- Delta:点 -->
<!-- toXDelta="30%": 加%表示相对于自身,不加%表示像素:移动几个像素的距离-->
</translate>

anim_set.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fillAfter="false"
    android:repeatCount="3"
    android:repeatMode="reverse"
    android:startOffset="2000" >

    <rotate
        android:fromDegrees="0"
        android:pivotX="0.5"
        android:pivotY="0.5"
        android:toDegrees="360" >
    </rotate>

    <alpha
        android:fromAlpha="1"
        android:toAlpha="0.2" >
    </alpha>

    <scale
        android:fromXScale="1"
        android:fromYScale="1"
        android:toXScale="0.5"
        android:toYScale="0.5" >
    </scale>

    <translate
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="300%"
        android:toYDelta="300%" >
    </translate>

</set>

2 开启动画

MainActivity.java

package com.android.xmlviewanimation;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ImageView imageView;

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

        imageView = (ImageView) findViewById(R.id.imageView);
    }

    // 平移
    public void click1(View view) {
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_translate);
        imageView.startAnimation(animation);

    }

    // 缩放(从100%缩放到50%,最后复原)
    public void click2(View view) {
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_scale);
        imageView.startAnimation(animation);
    }
    // 透明度
    public void click3(View view) {
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_alpha);
        imageView.startAnimation(animation);
    }

    // 旋转
    public void click4(View view) {
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_rotate);
        imageView.startAnimation(animation);
    }
    // 一起
    public void click5(View view) {
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_set);
        imageView.startAnimation(animation);
    }
}

Demo:http://download.csdn.net/detail/ss1168805219/9507659

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值