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

Android 動畫總結-屬性動畫

編輯:關於Android編程

特點:

改變的是對象的實際屬性 不僅可以應用於View, 有getter和setter方法的都可以

在xml中定義

放在res\animator
如:
animator_alpha.xml
這裡寫圖片描述


animator_backgroud.xml


animator_set.xml


// 動畫順序
    

作用於控件

Animator animator = AnimatorInflater.loadAnimator(this, R.animator.animator_alpha);
animator.setTarget(mImage);
animator.start();

PropertyAnimXMLActivity

public class PropertyAnimXMLActivity extends AppCompatActivity {
    private ImageView mImage;

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

    public void onClick(View view) {
        Animator animator;
        switch (view.getId()) {
            case R.id.btn_alpha:
                animator = AnimatorInflater.loadAnimator(this, R.animator.animator_alpha);
                break;
            case R.id.btn_background:
                animator = AnimatorInflater.loadAnimator(this, R.animator.animator_backgroud);
                break;
            case R.id.btn_set:
                animator = AnimatorInflater.loadAnimator(this, R.animator.animator_set);
                break;
            default:
                return;
        }
        animator.setTarget(mImage);
        animator.start();
    }
}

在Java代碼中定義

這裡寫圖片描述
PropertyAnimCodeActivity

public class PropertyAnimCodeActivity extends AppCompatActivity {
    private ImageView mImage;

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

    public void onClick(View view) {
        ObjectAnimator objectAnimator;
        switch (view.getId()) {
            case R.id.btn_alpha:
                objectAnimator = ObjectAnimator.ofFloat(mImage, "alpha", 1, 0, 0.5f);
                break;
            case R.id.btn_translation:
                objectAnimator = ObjectAnimator.ofFloat(mImage, "translationX", 0, 50, 20, 60,
                        30);
                break;
            case R.id.btn_rotate:
                objectAnimator = ObjectAnimator.ofFloat(mImage, "rotation", 0, 360, 180, -180);
                break;
            case R.id.btn_scale:
                objectAnimator = ObjectAnimator.ofFloat(mImage, "scaleX", 0, 1, 0, 2);
                break;
            case R.id.btn_background:
                objectAnimator = ObjectAnimator.ofObject(mImage, "BackgroundColor",
                        new ArgbEvaluator(), Color.RED, Color.GRAY, Color.BLUE);
                break;
            case R.id.btn_set:
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator oaAlpha = ObjectAnimator.ofFloat(mImage, "scaleX", 0, 2);
                ObjectAnimator oaScale = ObjectAnimator.ofFloat(mImage, "ScaleY", 0, 4);
                //這裡propertyName 首字母大小寫都可以
                animatorSet.setTarget(mImage);
                animatorSet.setDuration(2000);

                //同時執行:set.playTogether(animator1,animator2,animator3)
                //順序執行:set.playSequentially(animator1,animator2,animator3)
                //分布執行:play().with(); play().after();
                animatorSet.playTogether(oaAlpha, oaScale);

//                after(Animator anim)   將現有動畫插入到傳入的動畫之後執行
//                after(long delay)   將現有動畫延遲指定毫秒後執行
//                before(Animator anim)   將現有動畫插入到傳入的動畫之前執行
//                with(Animator anim)   將現有動畫和傳入的動畫同時執行
//                animatorSet.play(oaAlpha).after(oaScale);
                animatorSet.start();
                //監聽動畫變化時四個狀態
                animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {

                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                return;
            default:
                return;
        }
        objectAnimator.setRepeatCount(1);
        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        objectAnimator.setDuration(2000);
        objectAnimator.start();
        //監聽動畫變化時某個狀態
        //可以只復寫部分方法, 這裡傳Animator.AnimatorListener的實現類AnimatorListenerAdapter
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                Toast.makeText(PropertyAnimCodeActivity.this, "動畫結束", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

特別的地方

這裡寫圖片描述

屬性動畫可以作用於任何有getter和setter方法的對象 propertyName就是set開頭的方法的方法名不包含”set” 第一個字母大小寫可以隨意,但後面的部分必須與set方法後的大小寫保持一致。 可以使用Keyframe 時間/值 對定義屬性動畫, 可以理解為關鍵幀

PropertyAnimSpecialActivity

public class PropertyAnimSpecialActivity extends AppCompatActivity {
    private TextView mTvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_anim_special);
        mTvTitle = (TextView) findViewById(R.id.tv_title);

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_colorValue:
                ObjectAnimator animator = ObjectAnimator.ofArgb(mTvTitle, "TextColor", Color.RED,
                        Color.YELLOW, Color.GREEN, Color.BLUE, Color.BLACK);
                animator.setDuration(5000).start();
                //監聽動畫變化時的實時值
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int currentTextColor = mTvTitle.getCurrentTextColor();
                        mTvTitle.setText("#" + Integer.toHexString(currentTextColor));
                    }
                });
                break;
            case R.id.btn_value:
                final Test target = new Test();
                //propertyName就是set開頭的方法的方法名不包含"set" 第一個字母大小寫可以隨意,但後面的部分必須與set方法後的大小寫保持一致。
                ObjectAnimator animator1 = ObjectAnimator.ofInt(target, "value", 0, 100000);
                animator1.setDuration(5000).start();
                animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        //參數animation可以得到值
                        mTvTitle.setText(animation.getAnimatedValue() + "------" + target.getValue());
                    }
                });
                break;
            case R.id.btn_keyFrames:
                //The time, expressed as a value between 0 and 1
                Keyframe keyframe1 = Keyframe.ofInt(0, R.color.colorAccent);
                Keyframe keyframe2 = Keyframe.ofInt(0.25f, R.color.colorPrimary);
                Keyframe keyframe3 = Keyframe.ofInt(0.5f, R.color.colorAccent);
                Keyframe keyframe4 = Keyframe.ofInt(0.75f, R.color.colorPrimary);
                Keyframe keyframe5 = Keyframe.ofInt(1.0f, R.color.colorAccent);
                PropertyValuesHolder valuesHolder = PropertyValuesHolder.ofKeyframe("BackgroundResource", keyframe1, keyframe2, keyframe3,
                        keyframe4, keyframe5);
                ObjectAnimator animator2 = ObjectAnimator.ofPropertyValuesHolder(mTvTitle, valuesHolder);
                animator2.setDuration(3000).start();
                break;
            default:
                break;
        }
    }

    public class Test {
        private int value;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            //可以得知, ObjectAnimator調用了set方法
//            this.value = 666;
            this.value = value;
        }
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved