Android UI之基础动画——补间动画中偏移中的数值,百分数,百分数P的含义与区别

动画想必大家做开发的时候都有用到吧,本文将比教文题的3种偏移数值的区别,希望对不太清楚的童鞋有一定的帮助,也是为本人做个提醒。

1.平移固定数值:

java代码调用:

        mBtn_translate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation animation = AnimationUtils.loadAnimation(mContext,R.anim.animation_translation);
                mIv.startAnimation(animation);
            }
        });

anim文件中的animation_translation.xml:

<?xml version="1.0" encoding="utf-8"?>
    <!--android:fillAfter 结束后是否停在结束位置-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="false">
<translate
    android:fromXDelta="0"
    android:toXDelta="100"/>
<!--  平移动画中的数值:表示的是位置相对于 视图左上角的的相对位置:正为右,负为左 -->
</set>

运行效果:

        

运行结论:

            普通数值是相对于当前View的左上角为参考系进行平移的。

2. 平移为百分数

<?xml version="1.0" encoding="utf-8"?>
    <!--android:fillAfter 结束后是否停在结束位置-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="false">
<translate
    android:fromXDelta="0%"
    android:toXDelta="100%"/>
    <!-- x% 表示的是当前View的位置 + 当前View的宽*x%
         y% 表示的是当前View的位置 + 当前View的高*y%
    -->
</set>

结论:这里就先不演示了,单纯的百分数确实是相对于左上角加上View的宽高,这个其实用的不是太广吧

3. 平移百分数P实现从底部弹出的效果

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    >
<translate
    android:duration= "2000"
    android:fromYDelta="100%p"
    android:toYDelta="0"/>
    <!--
       表示从  当前View的左上角+View的父布局的高度,
              由于Android坐标系下为正,所以是从很下面
          到   当前View的位置
    -->
</set>

结论: x%p 或者 y%p 还是很有用的,对于活动的跳转乃至弹出框的平移动画等都是很有用途的

         在我们日常开发中这些都是比较常见的故而有必要进行掌握

总结:

        数值  百分数 百分数P 其实都是对View的当前位置,即左上角的偏移

        数值指代的是偏移多少个dp

        百分数指代的是偏移当前View的宽高百分比

         百分数P指代的是偏移当前View的父组件的宽高(注意这里是父组件)


猜你喜欢

转载自blog.csdn.net/crazyzhangxl/article/details/80875994
今日推荐