Android动画机制-View动画

目录

一、View动画的种类

二、XML格式动画

三、代码形式动画

1、平移动画

2、缩放动画

3、旋转动画

4、透明度动画

三、设置动画监听

四、LayoutAnimation

1、LayoutAnimation的定义XML格式

2、LayoutAnimation的定义代码格式

五、注意事项


        View动画是通过对场景对象不断的做图像变换而产生的动画效果,作用对象时View,支持4种动画效果:平移、缩放、旋转、透明度。

一、View动画的种类

        四种动画效果分别对应Animation的四个子类。效果可以通过XML实现,也可以通过代码动态实现,因为View动画效果比较固定,所以使用XML来实现可读性更好些。

名称标签xml子类
平移动画translateTranslateAnimation
缩放动画scaleScaleAnimation
旋转动画rotateRotateAnimation
透明度动画alphaAlphaAnimation

二、XML格式动画

        创建动画的XML文件,需要在res/anim目录下创建xml文件,例如:res/anim/anima_effect.xml

View动画描述文件语法固定,如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:fillAfter="true"
	android:duration="3000">
	<translate
		android:fromXDelta="0"
		android:toXDelta="100"
		android:fromYDelta="0"
		android:toYDelta="100"/>
	<scale
		android:fromXScale="0.5"
		android:toXScale="1.5"
		android:fromYScale="0.5"
		android:toYScale="1.5"
		android:pivotX="100"
		android:pivotY="200"/>
	<rotate
		android:fromDegrees="0"
		android:toDegrees="90"
		android:pivotX="100"
		android:pivotY="200"/>
	<alpha
		android:fromAlpha="0"
		android:toAlpha="1"/>
</set>

<set> :动画集合标签,对应AnimationSet类。
android:fillAfter :表示动画结束以后View是否停留在结束位置,true表示停留在结束问题,false表示不停留。
android:duration :表示动画的持续时间。
<translate> :平移标签,对应TranslateAnimation类,可以使View在水平方向和竖直方向完成平移,对应系列属性如下。
        android:fromXDelta :表示x的起始值,水平方向的起始位置;
        android:toXDelta :表示x的结束值,水平方向的结束值;
        android:fromYDelta :表示y的起始值,竖直方向的起始位置;
        android:toYDelta :表示y的结束值,竖直方向的结束值;
<scale> :缩放标签,对应ScaleAnimation 类,可以使View放大或缩小。
        android:fromXScale :水平方向缩放的起始比例;
        android:toXScale : 水平方向缩放的结束比例;
        android:fromYScale :竖直方向缩放的起始比例;
        android:toYScale :竖直方向缩放的结束比例 ;
        android:pivotX :缩放的轴点x坐标,默认情况下轴点是View的中心点;
        android:pivotY :缩放的轴点y坐标,默认情况下轴点是View的中心点;
<rotate> :旋转标签,对应RotateAnimation类,可以使View旋转。
        android:fromDegrees :旋转开始的角度;
        android:toDegrees :旋转结束的角度;
        android:pivotX :旋转的轴点x坐标;
        android:pivotY :旋转的轴点y坐标;
<alpha> :透明度标签,对应AlphaAnimation类,可以使View淡入淡出。
        android:fromAlpha :表示透明度的起始值;
        android:toAlpha:表示透明度的结束值;

三、代码形式动画

1、平移动画

    平移动画的有三种参数类型,三种类型参考的坐标都是View的左上角的坐标:

    Animation.ABSOLUTE:绝对位置

    Animation.RELATIVE_TO_SELF:相对于View自己的位置

    Animation.RELATIVE_TO_PARENT:相对于父View的位置

如下为一种初始化TranslateAnimation对象的方式,还有两种方式读者可自行查看源码。

//一种初始化TranslateAnimation的方式,默认类型为ABSOLUTE,构造方法中四个参数分别对应view的左上角坐标的绝对位置
TranslateAnimation translateAnimation = new TranslateAnimation(-100,200,-200,300);
//设置时长为4000ms
translateAnimation.setDuration(4000);
//设置动画结束后停在结束位置
translateAnimation.setFillAfter(true);
textView.startAnimation(translateAnimation);


//TranslateAnimation类的构造方法如下:
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
        mFromXValue = fromXDelta;
        mToXValue = toXDelta;
        mFromYValue = fromYDelta;
        mToYValue = toYDelta;

        mFromXType = ABSOLUTE;
        mToXType = ABSOLUTE;
        mFromYType = ABSOLUTE;
        mToYType = ABSOLUTE;
    }

2、缩放动画

初始化时未设置轴点,那么轴点默认为(0,0),即View的左上角坐标。

ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f,2f,0.5f,3f);
//设置时长为4000ms
scaleAnimation.setDuration(4000);
//设置动画结束后停在结束位置
scaleAnimation.setFillAfter(true);
textView.startAnimation(scaleAnimation);


//ScaleAnimation其中一种初始化方法
public ScaleAnimation(float fromX, float toX, float fromY, float toY) {
        mResources = null;
        mFromX = fromX;
        mToX = toX;
        mFromY = fromY;
        mToY = toY;
        mPivotX = 0;
        mPivotY = 0;
    }

3、旋转动画

初始化时未设置轴点,那么轴点默认为(0,0),即View的左上角坐标。

RotateAnimation rotateAnimation = new RotateAnimation(0,90);
//设置时长为4000ms
rotateAnimation.setDuration(4000);
//设置动画结束后停在结束位置
rotateAnimation.setFillAfter(true);
textView.startAnimation(rotateAnimation);


//RotateAnimation其中一种初始化方法
public RotateAnimation(float fromDegrees, float toDegrees) {
    mFromDegrees = fromDegrees;
    mToDegrees = toDegrees;
    mPivotX = 0.0f;
    mPivotY = 0.0f;
}

4、透明度动画

AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
//设置时长为4000ms
alphaAnimation.setDuration(4000);
//设置动画结束后停在结束位置
alphaAnimation.setFillAfter(true);
textView.startAnimation(alphaAnimation);


//AlphaAnimation其中一种初始化方法
public AlphaAnimation(float fromAlpha, float toAlpha) {
    mFromAlpha = fromAlpha;
    mToAlpha = toAlpha;
}

三、设置动画监听

可以通过Aniamtion的setAnimationListener方法为View动画设置监听,可以监听到View动画的开始、结束和重置。

如下所示:

AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
//设置时长为4000ms
alphaAnimation.setDuration(4000);
//设置动画结束后停在结束位置
alphaAnimation.setFillAfter(true);
textView.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        Log.i(TAG,"AnimationStart");
    }
    @Override
    public void onAnimationEnd(Animation animation) {
        Log.i(TAG,"AnimationEnd");
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        Log.i(TAG,"AnimationRepeat");
    }
});

可以看到日志打印结果:

四、LayoutAnimation

LayoutAnimation的作用对象是ViewGroup,为ViewGroup指定一个动画,使其子对象拥有这种动画效果。

1、LayoutAnimation的定义XML格式

在res/anim目录下,新建文件anim_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
	android:delay="0.5"
	android:animationOrder="random"
	android:animation="@anim/anim_item">
</layoutAnimation>

三个属性含义如下:

        android:delay表示动画延迟的比例,0.5指的是动画持续时间(animation duration)的比例。比如动画持续时间是300ms,0.5就代表每个子元素延迟150ms播放。

        android:animationOrder表示播放的顺序有三种效果(1、normal顺序播放;2、reverse倒序播放;3、random随机播放)。

        android:animation表示入场动画的效果。

其中res/anim/anim_item.xml如下,表示x为500的位置平移到0位置,透明度从0到1。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:duration="300"
	android:shareInterpolator="true">
	<alpha
		android:fromAlpha="0"
		android:toAlpha="1"/>
	<translate
		android:fromXDelta="500"
		android:toXDelta="0"/>
</set>

使用ListView来看下效果

布局文件activity_main.xml,设置ListView的android:layoutAnimation属性为我们写好的anim_layout效果。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context="com.example.viewanimation.MainActivity">
	<ListView
		android:id="@+id/list"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:layoutAnimation="@anim/anim_layout">
	</ListView>
</RelativeLayout>

代码中使用ListView就可以看到效果了,如下

 listView = findViewById(R.id.list);
 String[] strings = new String[30];
 for (int i = 0; i < 30; i++){
     strings[i] = "第" + i + "行";
 }
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.layout, strings);
 listView.setAdapter(adapter);

layout.layout,每个item的布局文件

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:textSize="25dp"
	android:textColor="@android:color/holo_red_light">
</TextView>

2、LayoutAnimation的定义代码格式

代码动态定义的形式其实就是我们不使用anim_layout.xml定义好的动画了,自然在布局文件中不设置ListView的android:layoutAnimation。我们将动画效果在代码中动态实现,如下,使用LayoutAnimationController来实现,思想和使用XML实现一样。我们前面讲解delay属性时提到 动画持续时间(animation duration)这个我们可以使用animation.getDuration()来得到。

listView = findViewById(R.id.list);
String[] strings = new String[30];
for (int i = 0; i < 30; i++){
    strings[i] = "第" + i + "行";
}
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_item);
LayoutAnimationController controller = new LayoutAnimationController(animation);
long d = animation.getDuration();
Log.d(TAG, "Druation : " + d);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(controller);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.layout, strings);
listView.setAdapter(adapter);

五、注意事项

        有时我们发现,我们使用View动画执行结束后,我们想隐藏一个控件,发现无法隐藏,这是因为View动画是对View的影像做动画,并不是真正改变View的状态,所以无法隐藏,这时可以先调用clearAnimation()来清楚动画。

mLin.clearAnimation();
mLin.setVisibility(View.GONE);

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值