具有动画的WindowManager(可以吗?)

有什么方法可以使用Animation在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);

}

回答:

我不确定您的任务的确切要求,但是有两种方法可以向窗口提供动画:

  1. 用法WindowManager.LayoutParams.windowAnimations如下:

    params.windowAnimations = android.R.style.Animation_Translucent;

  2. 添加附加的“容器”视图,因为WindowManager它不是真实的ViewGroup,因此添加视图的普通动画无法使用。已经问过这个问题,但是缺少代码。我将通过以下方式应用它:

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

    }

    }

以上是 具有动画的WindowManager(可以吗?) 的全部内容, 来源链接: utcz.com/qa/406484.html

回到顶部