1. 程式人生 > >屬性動畫對角移動

屬性動畫對角移動

具體就是回獲取螢幕的寬高和幀率

 //屬性動畫
        private void initAnimator() {
            /*translationY*/
            WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;         // 螢幕寬度(畫素)
            int height = dm.heightPixels;       // 螢幕高度(畫素)
            float density = dm.density;         // 螢幕密度(0.75 / 1.0 / 1.5)
            int densityDpi = dm.densityDpi;     // 螢幕密度dpi(120 / 160 / 240)
            // 螢幕寬度演算法:螢幕寬度(畫素)/螢幕密度
            float screenWidth =  (width / density);  // 螢幕寬度(dp)
            float screenHeight =  (height / density);// 螢幕高度(dp)
            //Y軸普平移
            ObjectAnimator translationY = ObjectAnimator.ofFloat(image, "translationY", new float[]{0f, screenHeight});
            translationY.setDuration(5000);
            translationY.start();
            //X軸普平移
            ObjectAnimator translationX = ObjectAnimator.ofFloat(image, "translationX", new float[]{0f, -screenWidth});
            translationX.setDuration(5000);
            translationX.start();
            //漸變
            ObjectAnimator alpha = ObjectAnimator.ofFloat(image, "alpha", new float[]{1.0f, 0.9f, 0.8f, 0.7f, 0.6f, 0.5f, 0.4f, 0.3f, 0.2f, 0.1f, 0.0f});
            alpha.setDuration(5000);
            alpha.start();
            translationY.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
    
                }
    
                @Override
                public void onAnimationEnd(Animator animation) {
                    image2.setVisibility(View.VISIBLE);
                }
    
                @Override
                public void onAnimationCancel(Animator animation) {
    
                }
    
                @Override
                public void onAnimationRepeat(Animator animation) {
    
                }
            });
    
        }