2013-07-19 34 views
12

Có cách nào để tăng lượt xem bằng WindowManager bằng Animation (ở dự án của android) không? Tôi chỉ không thể làm điều đó ngay cả khi sử dụng các ví dụ trong các trang web! Tôi đã sử dụng nhiều ví dụ nhưng không có tác dụng nào!WindowManager with Animation (có thể không?)

public BannerLayout(Activity activity, final Context context) { 
    super(context); 

    this.context = context; 

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams( 
      WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
      WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 
      WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
      PixelFormat.TRANSLUCENT); 

    wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    this.popupLayout = (RelativeLayout) inflater.inflate(R.layout.popup_activity, null); 
    this.popupLayout.setVisibility(GONE); 
    this.setActive(false); 

    wm.addView(this.popupLayout, params); 

    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 


private void show(){ 
    Animation in = AnimationUtils.loadAnimation(this.context, android.R.anim.fade_in); 
    this.popupLayout.setAnimation(in); 

    this.setActive(true); 
    this.popupLayout.setVisibility(VISIBLE); 
} 

Trả lời

28

Tôi không chắc chắn về các yêu cầu chính xác cho công việc của bạn, nhưng có hai cách để cung cấp hình ảnh động để cửa sổ:

  1. Sử dụng WindowManager.LayoutParams.windowAnimations như sau:

    params.windowAnimations = android.R.style.Animation_Translucent; 
    
  2. Thêm chế độ xem bổ sung 'vùng chứa', vì WindowManager không phải là ViewGroup thực và hoạt ảnh bình thường để thêm chế độ xem không hoạt động với chế độ xem đó. This question has been asked already, tuy nhiên thiếu mã. Tôi sẽ áp dụng nó theo cách sau:

    public class BannerLayout extends View { 
    
        private final Context mContext; 
    
        private final ViewGroup mPopupLayout; 
    
        private final ViewGroup mParentView; 
    
        public BannerLayout(Activity activity, final Context context) { 
         super(context); 
    
         mContext = context; 
    
         final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
           WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
           WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 
             WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
           PixelFormat.TRANSLUCENT); 
    
         final WindowManager mWinManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    
         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         mPopupLayout = (RelativeLayout) inflater.inflate(R.layout.popup_activity, null); 
         mPopupLayout.setVisibility(GONE); 
    
         params.width = ActionBar.LayoutParams.WRAP_CONTENT; 
         params.height = ActionBar.LayoutParams.WRAP_CONTENT; 
    
         // Default variant 
         // params.windowAnimations = android.R.style.Animation_Translucent; 
    
         mParentView = new FrameLayout(mContext); 
    
         mWinManager.addView(mParentView, params); 
    
         mParentView.addView(mPopupLayout); 
         mPopupLayout.setVisibility(GONE); 
        } 
    
        /** 
        * Shows view 
        */ 
        public void show(){ 
         final Animation in = AnimationUtils.loadAnimation(this.mContext, android.R.anim.fade_in); 
    
         in.setDuration(2000); 
    
         mPopupLayout.setVisibility(VISIBLE); 
         mPopupLayout.startAnimation(in); 
        } 
    
        /** 
        * Hides view 
        */ 
        public void hide() { 
         mPopupLayout.setVisibility(GONE); 
        } 
    } 
    
+0

sandrstar ... hoạt động hoàn hảo! Tuy nhiên ... Tôi tự hỏi nếu nó có thể sử dụng dịch hình ảnh động với các thành phần này. Tôi cần phải thực hiện một hiệu ứng lên xuống màn hình với các thành phần này ... – LeandroPortnoy

+0

private void show() { \t \t \t \t // Animation fadeIn = (Animation) AnimationUtils.loadAnimation (getContext(), android.R. anim.fade_in); \t \t //this.startAnimation(fadeIn); \t //this.bannerRelativeLayout.setVisibility(VISIBLE); \t \t \t \t this.setActive (true); mPopupLayout.setVisibility (VISIBLE); \t \t Hoạt ảnh cuối cùng trong = new TranslateAnimation (0, 0, -1000, 0); in.setDuration (700); AnimationSet animation = new AnimationSet (false); animation.addAnimation (in); \t mPopupLayout.startAnimation (hoạt ảnh); \t} – LeandroPortnoy

+0

Xin lỗi vì sự chậm trễ. Tôi đã thử nó và có vẻ nó hoạt động tốt. Có thể bạn cần sử dụng params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = ViewGroup.LayoutParams.MATCH_PARENT; cho FrameLayout. – sandrstar

0

Có nó là thực sự tốt. Miễn là chế độ xem bạn muốn tạo hiệu ứng động bên trong một vùng chứa, theo vùng chứa, nghĩa là LinearLayout hoặc bất kỳ bố cục nào khác sẽ thực hiện. Một cách chắc chắn chế độ xem là hoạt ảnh không phải là chế độ xem gốc của cửa sổ và vì vậy bạn có thể tạo hiệu ứng cho chế độ xem :) Hy vọng nó sẽ giúp