2011-12-29 7 views
11

Tôi có chế độ xem (customView) được thêm vào WindowManager.Chế độ xem hoạt ảnh được thêm trên WindowManager

WindowManager mWm = (WindowManager)activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); 
WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, 0, PixelFormat.TRANSPARENT); 
mWl.dimAmount = 0.0f; 
mWm.addView(customView, mWl); 

Bên trong chế độ xem tùy chỉnh, tôi sẽ gọi hoạt ảnh dịch khi nhấn nút đóng.

//// Đây là handler cho các hình ảnh động ////

final Handler translateHandler = new Handler(); 

final Runnable mtranslateUp = new Runnable() { 
    public void run() { 
     Log.v("TEST","mtranslateUp Runnable"); 
     startAnimation(translateUp); 
    } 
}; 

//// Đây là người biết lắng nghe cho các nút đóng ////

View.OnClickListener closeButtonListener = new View.OnClickListener() {   

    public void onClick(View v) { 
     translateHandler.post(mtranslateUp); 
    } 
}; 

//// Đây là bản dịch hoạt hình ////

translateUp = new TranslateAnimation(0,0,0,-200); 
translateUp.setFillAfter(true); 
translateUp.setDuration(1000); 
translateUp.setAnimationListener(new AnimationListener(){ 
     @Override 
     public void onAnimationEnd(Animation animation) { 
      Log.v("TEST","translateUp onAnimationEnd"); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 

     @Override 
     public void onAnimationStart(Animation animation) { 
      Log.v("TEST","translateUp onAnimationStart"); 
     }} 
    ); 

Nếu customView được thêm vào hoạt động, các mã này hoạt động tốt!

Khi customView được thêm vào WindowManager, Nhật ký bên trong onAnimationStart không hiển thị nhưng Nhật ký bên trong Runnable có thể được hiển thị.

Ai đó có thể cho biết cách thực hiện hoạt ảnh trên chế độ xem được thêm vào WindowManager?

+0

Bạn có giải pháp nào cho điều này không? Xin vui lòng chia sẻ, tôi cũng phải đối mặt với cùng một vấn đề – om252345

Trả lời

7

Tôi đang gặp sự cố tương tự với Chế độ xem được đính kèm với WindowManager. Thử thêm ViewGroup vào WindoManager so với Xem trực tiếp. Nó sẽ hoạt động.

+0

Tôi đã cố gắng này và nó đã không làm việc: ( – BVB

+0

Cảm ơn các mẹo. Làm việc cho tôi :) – alex

+0

này cũng làm việc cho tôi! mẹo tuyệt vời –

0

windowManager cần hình động bằng hệ thống Android. do đó, hoạt ảnh tùy chỉnh sẽ không hoạt động

10

Bạn nên tạo hoạt ảnh cho chế độ xem LayoutParameters. Ví dụ tôi sử dụng một phương pháp để cập nhật các bố trí xem:

public void updateViewLayout(View view, Integer x, Integer y, Integer w, Integer h){ 
    if (view!=null) { 
     WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams(); 

     if(x != null)lp.x=x; 
     if(y != null)lp.y=y; 
     if(w != null && w>0)lp.width=w; 
     if(h != null && h>0)lp.height=h; 

     mWindowService.updateViewLayout(view, lp); 
    } 
} 

Rõ ràng mWindowService là context.getSystemService (Context.WINDOW_SERVICE). Tôi kích hoạt phương pháp này trong hoạt ảnh:

public static void overlayAnimation(final View view2animate, int viewX, int endX) { 
    ValueAnimator translateLeft = ValueAnimator.ofInt(viewX, endX); 
    translateLeft.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 
      int val = (Integer) valueAnimator.getAnimatedValue(); 
      updateViewLayout(view2animate, val, null, null, null); 

     } 
    }); 
    translateLeft.setDuration(ANIMATION_DURATION); 
    translateLeft.start(); 
} 
+1

Cool, đây là cách để làm điều đó đúng mà không có gói xem vào ViewGroup – qbasso

+0

Đây là giải pháp duy nhất làm việc cho tôi, cảm ơn! Tôi đã thử sử dụng TranslateAnimation nhưng không có súc sắc. –