2013-07-19 1 views
12

Есть ли способ раздуть представление с помощью WindowManager с помощью анимации (в проекте android)? Я просто не могу этого сделать, даже используя примеры на сайтах! Я использовал много примеров, но никто не работал!WindowManager с анимацией (возможно ли это?)

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); 
} 

ответ

28

Я не уверен, что точные требования для вашей задачи, но есть два способа обеспечения анимации окна:

  1. Используйте WindowManager.LayoutParams.windowAnimations как следующее:

    params.windowAnimations = android.R.style.Animation_Translucent; 
    
  2. Добавить additonal 'container' view, потому что WindowManager не является реальным ViewGroup, и поэтому нормальная анимация для добавления просмотров не работает с ним. This question has been asked already, однако ему не хватает кода. Я бы применить его следующим образом:

    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

10 sandrstar ... работал отлично! однако ... Интересно, можно ли использовать анимационный перевод с этими компонентами. Мне нужно, чтобы сделать эффект вверх и вниз по экрану с этим компонентом ... – LeandroPortnoy

+0

частного пустого шоу() { \t \t \t \t // Анимация FadeIn = (анимация) 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 final Animation in = new TranslateAnimation (0, 0, -1000, 0); in.setDuration (700); AnimationSet animation = new AnimationSet (false); animation.addAnimation (in); \t mPopupLayout.startAnimation (анимация); \t} – LeandroPortnoy

+0

Извините за задержку. Я пробовал это и кажется, что все работает нормально. Может быть, вам нужно использовать params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = ViewGroup.LayoutParams.MATCH_PARENT; для FrameLayout. – sandrstar

0

Да, это действительно возможно. Пока представление, которое вы хотите оживить, находится внутри контейнера, в контейнере я подразумеваю, например, LinearLayout или любой другой макет. Конкретно вид, который нужно анимировать, не должен быть корневым видом окна, поэтому вы должны иметь возможность анимировать вид :) Надеюсь, он поможет

 Смежные вопросы

  • Нет связанных вопросов^_^