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); }}复制代码