Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 動畫總結-補間動畫

Android 動畫總結-補間動畫

編輯:關於Android編程

AlphaAnimation:透明度(alpha)漸變效果,對應< alpha/>”標簽。

TranslateAnimation:位移漸變,需要指定移動點的開始和結束坐標,對應標簽。

ScaleAnimation:縮放漸變,可以指定縮放的參考點,對應標簽。

RotateAnimation:旋轉漸變,可以指定旋轉的參考點,對應標簽。

AnimationSet:組合漸變,支持組合多種漸變效果,對應標簽。

這裡寫圖片描述

在xml中定義

放在res\anim

如:
anim_alpha.xml


anim_translation.xml




    

anim_rotate.xml



    

anim_scale.xml



    

anim_set.xml



    

    

    

    

作用於控件

Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
mImage.startAnimation(animation);

TweenAnimXMLActivity

public class TweenAnimXMLActivity extends AppCompatActivity {
    private ImageView mImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween_anim_xml);
        mImage = (ImageView) findViewById(R.id.image);
    }

    public void onClick(View view) {
        Animation animation = null;
        switch (view.getId()) {
            case R.id.btn_alpha:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
                break;
            case R.id.btn_translation:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_translation);
                break;
            case R.id.btn_rotate:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
                break;
            case R.id.btn_scale:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
                break;
            case R.id.btn_set:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_set);
                break;
            default:
                break;
        }
        mImage.startAnimation(animation);
    }
}

直接在Java代碼中定義

這裡寫圖片描述

TweenAnimCodeActivity

public class TweenAnimCodeActivity extends AppCompatActivity {
    private static final String TAG = "TweenAnimCodeActivity";
    private ImageView mImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween_anim_code);
        mImage = (ImageView) findViewById(R.id.image);
    }

    public void onClick(View view) {
        Animation animation;
        switch (view.getId()) {
            case R.id.btn_alpha:
                animation = new AlphaAnimation(1.0f, 0.2f);
                break;
            case R.id.btn_translation:
//                animation = new TranslateAnimation(0, 200, 0, 200);
//                參考坐標系
//                Animation.ABSOLUTE  默認
//                Animation.RELATIVE_TO_SELF  相對自己  左上角 (0, 0)
//                Animation.RELATIVE_TO_PARENT  相對父View
                animation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 200,
                        Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 200);
                break;
            case R.id.btn_rotate:
//                animation = new RotateAnimation(0, 720, 0.5f, 0.5f);
                // pivotX、pivotY  縮放, 旋轉中心
                animation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation
                        .RELATIVE_TO_SELF, 0.5f);
//                        設置插值器
//                        LinearInterpolator(勻速)
//                        AccelerateInterpolator(先慢後快)
//                        AccelerateDecelerateInterpolator(先慢中快後慢)
//                        DecelerateInterpolator(先快後慢)
//                        CycleInterpolator(循環播放,速度為正弦曲線)
//                        AnticipateInterpolator(先回撤,再勻速向前)
//                        OvershootInterpolator(超過,拉回)
//                        BounceInterpolator(回彈)
                animation.setInterpolator(new AccelerateDecelerateInterpolator());
                // 相對父View
                // 父View width(100dp) height(200dp)
                //旋轉中心位於 相對控件(0, 0) X50, Y100的地方
//                animation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_PARENT, 0.5f,
//                        Animation.RELATIVE_TO_PARENT, 0.5f);
                break;
            case R.id.btn_scale:
//                animation = new ScaleAnimation(0.2f, 2, 0.2f, 2, 0.5f, 0.5f);
                animation = new ScaleAnimation(0.2f, 2, 0.2f, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation
                        .RELATIVE_TO_SELF, 0.5f);
                break;
            case R.id.btn_set:
                ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2, Animation
                        .RELATIVE_TO_SELF, 0.5f, Animation
                        .RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(1000);
                scaleAnimation.setRepeatCount(Animation.INFINITE);
                scaleAnimation.setRepeatMode(Animation.REVERSE);
                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.2f);
                alphaAnimation.setDuration(1000);
                alphaAnimation.setRepeatCount(Animation.INFINITE);
                alphaAnimation.setRepeatMode(Animation.REVERSE);
                //是否公用插值器
                AnimationSet animationSet = new AnimationSet(false);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(alphaAnimation);
                //設置動畫延時時間(多久後開始)
//                animationSet.setStartOffset(2000);
                //設置動畫結束之後是否保持動畫的目標狀態
//                animationSet.setFillAfter(true);
                //設置動畫結束之後是否保持動畫開始時的狀態
//                animationSet.setFillBefore(false);
                mImage.startAnimation(animationSet);
                //取消動畫
//                animationSet.cancel();
                //釋放資源
//                animationSet.reset();
                return;
            default:
                return;
        }
        animation.setDuration(1000);//間隔
        animation.setFillAfter(true);//結束後應用
        animation.setRepeatCount(3);//重復次數
        animation.setRepeatMode(Animation.REVERSE);//重復模式
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.d(TAG, "onAnimationStart() called with: animation = [" + animation + "]");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d(TAG, "onAnimationEnd() called with: animation = [" + animation + "]");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.d(TAG, "onAnimationRepeat() called with: animation = [" + animation + "]");
            }
        });
        mImage.startAnimation(animation);
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved