悬浮窗

activity:复制代码

initAudioView(LayoutInflater.from(context), R.layout.teana_audio_hc);
public void initAudioView(LayoutInflater inflater, int layout) {
    if (mAudioFloadView == null) {
        mAudioFloadView = new FloatView(mContext);
        mAudioFloadView.inflateLayout(layout);
        mAudioFloadView.setLayoutParams(getSoundUiLayoutParams());
    } else {
        android.util.Log.e(TAG, "initAudioView called more than once!!!!");
    }
}

protected WindowManager.LayoutParams getSoundUiLayoutParams() {
    Context context = mCanbusManager.getContext();

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
    lp.format = PixelFormat.RGBA_8888;
    lp.gravity = Gravity.CENTER;
    lp.packageName = context.getPackageName();
    lp.windowAnimations = R.style.AirAnimation;
    lp.setTitle("CarSound");

    return lp;
}复制代码

package android.car.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class FloatView {
    protected Context mContext;
    protected WindowManager mWindowManager;
    protected LayoutParams mLayoutParams = new LayoutParams();
    private View mRootView;
    private boolean mIsAttach;
    protected FloatView.OnAttachListener mOnAttachListener;

    public FloatView(Context context) {
        this.mContext = context;
        this.mWindowManager = (WindowManager)this.mContext.getSystemService("window");
    }

    public LayoutParams getLayoutParams() {
        return this.mLayoutParams;
    }

    public void setLayoutParams(LayoutParams lp) {
        this.mLayoutParams.copyFrom(lp);
    }

    public View inflateLayout(int layout) {
        if(this.mRootView != null) {
            throw new IllegalStateException("mRootView has been created!");
        } else {
            LayoutInflater li = LayoutInflater.from(this.mContext);
            this.mRootView = li.inflate(layout, (ViewGroup)null);
            this.onInflateFinish();
            return this.mRootView;
        }
    }

    protected void onInflateFinish() {
    }

    public View setView(View view) {
        if(this.mRootView != null) {
            throw new IllegalStateException("mRootView has been created!");
        } else {
            this.mRootView = view;
            this.onInflateFinish();
            return this.mRootView;
        }
    }

    public View getView() {
        return this.mRootView;
    }

    private void setIsAttach(boolean isAttach) {
        if(this.mIsAttach != isAttach) {
            this.mIsAttach = isAttach;
            if(this.mOnAttachListener != null) {
                this.mOnAttachListener.onAttach(this, this.mIsAttach);
            }
        }

    }

    public boolean isAttach() {
        return this.mIsAttach;
    }

    public void attach() {
        if(this.mWindowManager != null && this.mRootView != null && this.mLayoutParams != null && !this.mIsAttach) {
            this.mWindowManager.addView(this.mRootView, this.mLayoutParams);
            this.setIsAttach(true);
        }

    }

    public void detach() {
        this.detach(false);
    }

    public void detach(boolean immediate) {
        if(this.mWindowManager != null && this.mRootView != null && this.mIsAttach) {
            if(immediate) {
                this.mWindowManager.removeViewImmediate(this.mRootView);
            } else {
                this.mWindowManager.removeView(this.mRootView);
            }

            this.setIsAttach(false);
        }

    }

    public void setShow(boolean show) {
        if(show) {
            this.attach();
        } else {
            this.detach();
        }

    }

    public void updateLayout() {
        if(this.mWindowManager != null && this.mRootView != null && this.mIsAttach) {
            this.mWindowManager.updateViewLayout(this.mRootView, this.mLayoutParams);
        }

    }

    public View findViewById(int id) {
        return this.mRootView != null?this.mRootView.findViewById(id):null;
    }

    public void setBackgroundColor(int color) {
        if(this.mRootView != null) {
            this.mRootView.setBackgroundColor(color);
        }

    }

    public void setOnAttachListener(FloatView.OnAttachListener l) {
        this.mOnAttachListener = l;
    }

    public interface OnAttachListener {
        void onAttach(FloatView var1, boolean var2);
    }
}
复制代码


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值