我想创建一个“Flash”类来像吐司一样轻松地显示白色闪光屏幕(200ms),如下所示:
Flash.create(context).show();
我通过WindowManager寻找解决方案,但我遇到了一些困难。 你推荐什么解决方案?
答案 0 :(得分:1)
存在一些问题,因为您必须将活动而不仅仅是一个上下文传递到" Flash"屏幕。因此,处理屏幕旋转会很棘手。
我已创建并开源small library called Crouton,可以帮助您。 目前,在展示Crouton的同时,它仍然在解决方向变化问题。
答案 1 :(得分:0)
所以我给自己的解决方案,如果它可以帮助任何人... (自我第一篇文章以来,我将调用改为flash方法)
public class Flasher {
private final static int DURATION_OFFSET = 300;
private final static int DURATION_FADEIN = 50;
private final static int DURATION_FADEOUT = 200;
private Animation fadein;
private Animation fadeout;
private RelativeLayout flash;
private RelativeLayout view;
private Toast toast;
private int count;
public Flasher(Context context) {
fadein = new AlphaAnimation(0, 1);
fadein.setStartOffset(DURATION_OFFSET);
fadein.setDuration(DURATION_FADEIN);
fadein.setAnimationListener(new AnimationListener() {
@Override public void onAnimationStart(Animation anim) {}
@Override public void onAnimationRepeat(Animation anim) {}
@Override public void onAnimationEnd(Animation anim) {
flash.startAnimation(fadeout);
}
});
fadeout = new AlphaAnimation(1, 0);
fadeout.setDuration(DURATION_FADEOUT);
fadeout.setAnimationListener(new AnimationListener() {
@Override public void onAnimationStart(Animation anim) {}
@Override public void onAnimationRepeat(Animation anim) {}
@Override public void onAnimationEnd(Animation anim) {
if(count > 1) {
flash(count - 1);
} else {
cancel();
}
}
});
LayoutParams params = new LayoutParams(-1, -1);
flash = new RelativeLayout(context);
flash.setLayoutParams(params);
flash.setBackgroundColor(0xffffffff);
flash.setVisibility(View.INVISIBLE);
view = new RelativeLayout(context);
view.setLayoutParams(params);
view.addView(flash);
toast = new Toast(context);
toast.setView(view);
toast.setGravity(Gravity.FILL, 0, 0);
}
public final void flash(int count) {
toast.show();
this.count = count;
flash.startAnimation(fadein);
}
public final void cancel() {
toast.cancel();
}
}
MainActivity用于测试它:
public class MainActivity extends Activity {
private static Flasher flasher;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
View layout = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(layout);
if(flasher == null) {
flasher = new Flasher(this);
}
layout.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
flasher.flash(1);
}
});
}
@Override protected void onDestroy() {
if(isFinishing()) {
flasher = null;
}
super.onDestroy();
}
}
感谢Keyboardsurfer的帮助!
编辑:不满足约束: Toast不会发送触摸事件:
flasher.flash(10);
在闪光效果期间,MainActivity不可触摸 如果有人有解决问题的解决方案......
答案 2 :(得分:0)
搜索了几个小时后,似乎无法将事件从一个窗口委托给另一个窗口。所以我发现的最终功能解决方案是扩展一个活动,以便它实现这种闪光效果。对于那些对它感兴趣的人,这是我的子类:
public class FlmActivity extends Activity {
private final static int DURATION_FADEIN = 50;
private final static int DURATION_FADEOUT = 200;
private final static int DURATION_INTERVAL = 200;
private View view;
private AnimationSet anim;
private int count;
public FlmActivity() {
super();
}
@Override protected void onPostCreate(Bundle state) {
super.onPostCreate(state);
view = new View(this) {{
setBackgroundColor(0xffffffff);
setVisibility(View.INVISIBLE);
}};
anim = new AnimationSet(false) {{
addAnimation(new AlphaAnimation(0, 1) {{
setDuration(DURATION_FADEIN);
}});
addAnimation(new AlphaAnimation(1, 0) {{
setStartOffset(DURATION_FADEIN);
setDuration(DURATION_FADEOUT);
}});
addAnimation(new Animation() {{
setStartOffset(DURATION_FADEIN + DURATION_FADEOUT);
setDuration(DURATION_INTERVAL);
setAnimationListener(new AnimationListener() {
@Override public void onAnimationStart(Animation anim) {}
@Override public void onAnimationRepeat(Animation anim) {}
@Override public void onAnimationEnd(Animation anim) {
flash(count - 1);
}
});
}});
}};
addContentView(view, getWindow().getAttributes());
}
@Override protected void onDestroy() {
((ViewGroup) view.getParent()).removeView(view);
super.onDestroy();
}
public final void flash(int count) {
if((this.count = count) > 0) {
view.startAnimation(anim);
}
}
}
在扩展活动中调用闪光效果的代码:
flash(10);
通过此实现,事件将通过图层