項目開發偶爾會用到動畫,每次都得找之前的代碼複製,太麻煩了,但又記不住,只好寫在這.java
ObjectAnimator.ofFloat(view, "translationX", 0, 50, -50, 0).setDuration(duration).start();//位移 ObjectAnimator.ofFloat(view, "translationY", 0, 50, -50, 0).setDuration(duration).start(); ObjectAnimator.ofFloat(view, "scaleX", 1, 2, 1).setDuration(duration).start();//縮放 ObjectAnimator.ofFloat(view, "scaleY", 1, 2, 1).setDuration(duration).start(); ObjectAnimator.ofFloat(view, "alpha", 1, 0, 1).setDuration(duration).start();//透明度 ObjectAnimator.ofFloat(view, "rotationX", 0, 180, 0).setDuration(duration).start();//翻轉 ObjectAnimator.ofFloat(view, "rotationY", 0, 180, 0).setDuration(duration).start(); ObjectAnimator.ofFloat(view, "rotation", 0, 180, 0).setDuration(duration).start();//旋轉 ViewHelper.setPivotX(view, view.getWidth() / 2f);//設置動畫基點 ViewHelper.setPivotY(view, view.getHeight() / 2f);
以上就是比較經常使用的nineold,可是偶爾遇到組合的,這樣就比較省事ide
AnimatorSet animator = new AnimatorSet(); animator.playTogether( ObjectAnimator.ofFloat(p_w_picpath, "rotation", 0, 1080), ObjectAnimator.ofFloat(p_w_picpath, "translationX", 0, 180), ObjectAnimator.ofFloat(p_w_picpath, "translationY", 0, -180), ObjectAnimator.ofFloat(p_w_picpath, "scaleX", 1, 0), ObjectAnimator.ofFloat(p_w_picpath, "scaleY", 1, 0) ObjectAnimator.ofFloat(p_w_picpath, "alpha", 1, 0.25f, 1) ); animator.setDuration(5 * 1000).start();
今天作一個添加商品到購物車時 商品圖片旋轉 縮小 位移 到指定的購物車圖標位置,用的屬性動畫,很麻煩,動畫執行順序若是弄亂了,位移軌跡會出問題的.大概記錄一下,以避免忘了
動畫
int[] endLocation = new int[2]; topSpcart.getLocationInWindow(endLocation);// shopCart是那個購物車 // 計算位移 int endX = endLocation[0] - startLocation[0] ;// 動畫位移的X座標 int endY = endLocation[1] - startLocation[1];// 動畫位移的y座標 RotateAnimation rotateAnimation = new RotateAnimation(0, 1080, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ScaleAnimation scaleAnimation =new ScaleAnimation(1, 0, 1, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0); TranslateAnimation translateAnimation = new TranslateAnimation(0,endX, 0, endY); // translateAnimation.setInterpolator(new LinearInterpolator());//動畫加速器 // translateAnimation.setInterpolator(new AccelerateInterpolator()); // translateAnimation.setRepeatCount(0);// 動畫重複執行的次數 // translateAnimation.setFillAfter(true);//動畫結束留在原位置 AnimationSet set = new AnimationSet(false); set.setFillAfter(true); set.addAnimation(rotateAnimation);//旋轉 set.addAnimation(scaleAnimation);//縮放 set.addAnimation(translateAnimation);//位移 set.setDuration(1000);// 動畫的執行時間 view.startAnimation(set); // 動畫監聽事件 set.setAnimationListener(new AnimationListener() { // 動畫的開始 @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } // 動畫的結束 @Override public void onAnimationEnd(Animation animation) { } });