android 动画-补间动画

补间动画
包含渐变Alpha、旋转Rotate、缩放Scale、平移Translate

布局代码

<?xml version="1.0" encoding="utf-8"?>
<!-- duration 执行动画的时间  fillafter  执行完动画后,保持最后的效果-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5555"
    android:fillAfter="true">

    <!-- 透明度  从0 到1  -->
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1" />
    <!-- 旋转  从0度旋转720 pivot  以基点(中心点)  -->
    <rotate
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="720"

        />
    <!-- 缩放  从1到0.5  从原始状态,缩放一半,以父布局中心为基点  -->
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"

        android:toXScale="0.5"
        android:toYScale="0.5" />

    <!-- 平移  从屏幕外 平移到屏幕内,注意以布局的左上角(坐标0,0)为基点  -->
    <translate

        android:fromXDelta="-100%"
        android:fromYDelta="-100%"
        android:toXDelta="0"
        android:toYDelta="0"></translate>
</set>

rotate、scale动画的android:pivotX和android:pivotY属性、translate动画的android:toXDelta和android:toYDelta属性的取值都可以是都可以数值、百分数、百分数p,比如:50、50%、50%p,他们取值的代表的意义各不相同:
50表示以View左上角为原点沿坐标轴正方向(x轴向右,y轴向下)偏移50px的位置;
50%表示以View左上角为原点沿坐标轴正方向(x轴向右,y轴向下)偏移View宽度或高度的50%处的位置;
50%p表示以View左上角为原点沿坐标轴正方向(x轴向右,y轴向下)偏移父控件宽度或高度的50%处的位置(p表示相对于ParentView的位置)。
ggg.gif

Java代码

public void clickToSet(View view) {
    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
    alphaAnimation.setDuration(2000);

    RotateAnimation rotateAnimation = new RotateAnimation(
            0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setDuration(2000);

    ScaleAnimation scaleAnimation = new ScaleAnimation(
            1, 0.5f,
            1, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    scaleAnimation.setDuration(2000);

    TranslateAnimation translateAnimation = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0,
            Animation.RELATIVE_TO_SELF, 1,
            Animation.RELATIVE_TO_SELF, 0,
            Animation.RELATIVE_TO_SELF, 1);
    translateAnimation.setDuration(2000);

    AnimationSet animationSet = new AnimationSet(true);
    animationSet.addAnimation(alphaAnimation);
    animationSet.addAnimation(rotateAnimation);
    animationSet.addAnimation(scaleAnimation);
    animationSet.addAnimation(translateAnimation);

    mTargetView.startAnimation(animationSet);
}
发布了118 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/chentaishan/article/details/104915192