lp.windowAnimations = R.style.anim_view;//动画
设置系统动画可能还有效果,设置自己的动画似乎没有什么效果,
底部别人提供了自定义的动画,是OK的
转载地址是 :
http://www.2cto.com/kf/201408/328978.htmlWindowManager上像在Activity中使用动画效果无效,比如下面的代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
ImageView iv =
new
ImageView(
this
);
iv.setImageResource(R.drawable.ic_launcher);
TranslateAnimation animation =
new
TranslateAnimation(
Animation.ABSOLUTE,
20
, Animation.ABSOLUTE,
300
,
Animation.ABSOLUTE,
100
, Animation.ABSOLUTE,
400
);
animation.setDuration(
1000
);
animation.setFillAfter(
false
);
iv.setAnimation(animation);
WindowManager mWindowManager = (WindowManager)
this
.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams mLayoutParams =
new
WindowManager.LayoutParams();
mLayoutParams.x =
20
;
mLayoutParams.y =
100
;
mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mLayoutParams.gravity = Gravity.TOP;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
mWindowManager.addView(iv, mLayoutParams);
animation.start();
|
|
1
2
3
4
5
6
7
8
9
10
11
|
lp =
new
WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.RGBA_8888);
lp.gravity = Gravity.LEFT |Gravity.TOP;
lp.windowAnimations = R.style.anim_view;
//动画
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.addView(view, lp);
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/**
*
*/
package
com.kince.apus.widget;
import
com.kince.apus.R;
import
android.animation.Animator;
import
android.animation.Animator.AnimatorListener;
import
android.animation.AnimatorSet;
import
android.animation.ObjectAnimator;
import
android.content.Context;
import
android.graphics.PixelFormat;
import
android.os.Handler;
import
android.util.AttributeSet;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.WindowManager;
import
android.widget.FrameLayout;
import
android.widget.ImageView;
import
android.widget.TextView;
/**
* @author kince
* @category Windowmanager在Layout中的应用
*
*
*/
public
class
GuideLayout
extends
FrameLayout {
private
WindowManager wManager;
private
WindowManager.LayoutParams wmParams;
private
View addView;
private
TextView mTextView;
private
ImageView mImageView;
private
boolean
isAddView;
private
AnimatorSet mShowAnimatorSet, mHideAnimatorSet;
private
Handler mHandler =
new
Handler() {
public
void
handleMessage(android.os.Message msg) {
super
.handleMessage(msg);
switch
(msg.what) {
case
1
:
showAnimator();
break
;
default
:
break
;
}
};
};
/**
* @param context
*/
public
GuideLayout(Context context) {
this
(context,
null
);
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param attrs
*/
public
GuideLayout(Context context, AttributeSet attrs) {
this
(context, attrs,
0
);
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public
GuideLayout(Context context, AttributeSet attrs,
int
defStyle) {
super
(context, attrs, defStyle);
addView = LayoutInflater.from(context).inflate(R.layout.guide_layout,
this
);
mTextView = (TextView) addView.findViewById(R.id.tv);
mImageView = (ImageView) addView.findViewById(R.id.iv);
mTextView.setVisibility(View.GONE);
mImageView.setVisibility(View.GONE);
setAnimator();
getWindowManager(context);
}
/**
* @category 实例化WindowManager 初次模拟位置时候使用
* @param context
*/
private
void
getWindowManager(
final
Context context) {
wManager = (WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
wmParams =
new
WindowManager.LayoutParams();
wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
wmParams.format = PixelFormat.TRANSPARENT;
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
wmParams.gravity =
17
;
wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;
}
private
void
setAnimator() {
mShowAnimatorSet =
new
AnimatorSet();
Animator[] showAnimator =
new
Animator[
2
];
showAnimator[
0
] = ObjectAnimator.ofFloat(mTextView,
"alpha"
,
new
float
[] {
0
.0F,
1
.0F });
showAnimator[
1
] = ObjectAnimator.ofFloat(mImageView,
"alpha"
,
new
float
[] {
0
.0F,
1
.0F });
mShowAnimatorSet.playTogether(showAnimator);
mShowAnimatorSet.setDuration(1500l);
mHideAnimatorSet =
new
AnimatorSet();
Animator[] hideAnimator =
new
Animator[
2
];
hideAnimator[
0
] = ObjectAnimator.ofFloat(mTextView,
"alpha"
,
new
float
[] {
1
.0F,
0
.0F });
hideAnimator[
1
] = ObjectAnimator.ofFloat(mImageView,
"alpha"
,
new
float
[] {
1
.0F,
0
.0F });
mHideAnimatorSet.playTogether(hideAnimator);
mHideAnimatorSet.setDuration(1500l);
}
public
void
showAnimator() {
mTextView.setVisibility(View.VISIBLE);
mImageView.setVisibility(View.VISIBLE);
mShowAnimatorSet.start();
isAddView=
true
;
}
public
void
hideAnimator() {
mHideAnimatorSet.start();
mHideAnimatorSet.addListener(
new
AnimatorListener() {
@Override
public
void
onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
}
@Override
public
void
onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
@Override
public
void
onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
mTextView.setVisibility(View.INVISIBLE);
mImageView.setVisibility(View.INVISIBLE);
}
@Override
public
void
onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
}
public
void
sendMessage() {
if
(isAddView) {
wManager.removeView(
this
);
mHandler.removeMessages(
1
);
isAddView=
false
;
}
mHandler.sendEmptyMessage(
1
);
wManager.addView(
this
, wmParams);
}
}
|
本文介绍如何在 WindowManager 上实现动画效果。通过创建自定义布局 GuideLayout,并利用 ObjectAnimator 和 AnimatorSet 控制视图的透明度变化,实现了显示和隐藏动画。此外,文章还探讨了直接在 WindowManager 添加视图进行动画操作的限制及其解决方法。
303

被折叠的 条评论
为什么被折叠?



