悬浮窗能实现自定Animation动画效果吗

如题所述

目前找到的方法只能调用内建的动画效果
params = new WindowManager.LayoutParams();
params.windowAnimations = android.R.style.Animation_Translucent;
若改成R.style.custon_anim的话则是怎麼样都没有动画

可用的方法是直接搭Handler、Runnable来updateViewLayout
但是在改物件大小时会看起来不顺畅
仅管已经把呼叫时间改为1ms,一样没辨法每毫秒呼叫一次更新

现在是先用Runnable的方法,只做了淡出效果、物件直接放大到固定值
代码大致上如下

private void createFloatView(){
Log.d("HuybnTag","S.createFloatView");
btn_exif = new Button(getApplicationContext());
btn_exif.setBackgroundResource(R.drawable.exif);
wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

exif = new WindowManager.LayoutParams();
exif.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
exif.format = PixelFormat.RGBA_8888;
exif.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
exif.height = 124;
exif.width = 124;
exif.gravity = Gravity.CENTER;
exif.windowAnimations = android.R.style.Animation_Translucent;
.......

要离开时运行这个
fadeOut(btn_exif,exif);
//下面这段改自网络上的代码
public void fadeOut(final View notificationView, final LayoutParams params){
animTime=300;
exif.width=310;
exif.height=310;
final long startTime = System.currentTimeMillis();
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
fadeOutHandler(notificationView, params, startTime);
}
}, 25);
}
public void fadeOutHandler(final View notificationView, final LayoutParams params, final long startTime){
long timeNow = System.currentTimeMillis();
float alpha = (1- ((float)(timeNow - startTime)/animTime) ) * 1.0f;
if(alpha<=0) alpha = 0;
params.alpha = alpha;
Log.d("HuybnTag","alpha= "+alpha);
wm.updateViewLayout(notificationView, params);
if (timeNow-startTime<animTime){
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
fadeOutHandler(notificationView, params, startTime);
}
}, 25);
温馨提示:答案为网友推荐,仅供参考
相似回答