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);
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。