问题描述
有什么方法可以使用动画(在 android 的项目中)使用 WindowManager 来扩充视图?即使使用站点中的示例,我也无法做到!我用了很多例子,但没有一个奏效!
Is there any way to inflate a view with WindowManager using Animation (at android's project)? I just can't do it even using the examples in sites! I used many examples but none worked!
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);
}
推荐答案
我不确定您的任务的确切要求,但有两种方法可以为窗口提供动画:
I'm not sure about exact requirements for Your task, but there's two ways to provide animation to window:
使用
WindowManager.LayoutParams.windowAnimations
如下:
params.windowAnimations = android.R.style.Animation_Translucent;
添加附加的容器"视图,因为 WindowManager
不是真正的 ViewGroup
,因此用于添加视图的普通动画无法使用它.这个问题已经有人问过了,但是缺少代码.我会通过以下方式应用它:
Add additonal 'container' view, because WindowManager
is not a real ViewGroup
and so normal animation for adding views is not working with it. This question has been asked already, however it lacks the code. I would apply it the following way:
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(有可能吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!