2013-07-19 8 views
12

Gibt es eine Möglichkeit, eine Ansicht mit WindowManager mit Animation (bei androids Projekt) aufzublasen? Ich kann es nicht einmal mit den Beispielen auf den Seiten machen! Ich habe viele Beispiele benutzt, aber keiner hat funktioniert!WindowManager mit Animation (ist das möglich?)

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

Antwort

28

Ich bin über die genaue Anforderungen für Ihre Aufgabe nicht sicher, aber es gibt zwei Möglichkeiten, Animation Fenster zur Verfügung zu stellen:

  1. Verwenden WindowManager.LayoutParams.windowAnimations wie folgt aus:

    params.windowAnimations = android.R.style.Animation_Translucent; 
    
  2. Fügen Sie eine zusätzliche 'Container'-Ansicht hinzu, da WindowManager keine echte ViewGroup ist und eine normale Animation zum Hinzufügen von Ansichten nicht funktioniert. This question has been asked already, jedoch fehlt der Code. Ich würde ihm die folgende Art und Weise anwenden:

    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 ... hat perfekt funktioniert! aber ... Ich frage mich, ob es möglich ist, Animationsübersetzung mit diesen Komponenten zu verwenden. Ich brauche einen Effekt auf und ab dem Bildschirm mit dieser Komponente ... – LeandroPortnoy

+0

private void show() { \t \t \t \t // Animation, fadeIn = (Animation) AnimationUtils.loadAnimation (getContext(), android.R zu machen. anim.fade_in); \t \t //this.startAnimation (fadeIn); \t //this.bannerRelativeLayout.setVisibility (VISIBLE); \t \t \t \t this.setActive (wahr); mPopupLayout.setVisibility (SICHTBAR); \t \t final Animation in = neu TranslateAnimation (0, 0, -1000, 0); in.setDuration (700); AnimationSet Animation = new AnimationSet (false); animation.addAnimation (in); \t mPopupLayout.startAnimation (Animation); \t} – LeandroPortnoy

+0

Sorry für die Verzögerung. Ich habe es versucht und scheint es funktioniert gut. Möglicherweise müssen Sie params.width = ViewGroup.LayoutParams.MATCH_PARENT verwenden; params.height = ViewGroup.LayoutParams.MATCH_PARENT; für FrameLayout. – sandrstar

0

Ja, es ist in der Tat möglich. Solange die Ansicht, die Sie animieren möchten, sich in einem Container befindet, meinen Sie mit container zum Beispiel ein LinearLayout oder irgendein anderes Layout. Abschließend sollte die zu animierende Ansicht nicht die Grundansicht eines Fensters sein und so sollte man in der Lage sein die Ansicht zu animieren :) Hoffe es hilft

Verwandte Themen