Android仿360懸浮小球自定義view實(shí)現(xiàn)示例
成都創(chuàng)新互聯(lián)公司專(zhuān)業(yè)為企業(yè)提供下城網(wǎng)站建設(shè)、下城做網(wǎng)站、下城網(wǎng)站設(shè)計(jì)、下城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、下城企業(yè)網(wǎng)站模板建站服務(wù),10多年下城做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
效果圖如下:
實(shí)現(xiàn)當(dāng)前這種類(lèi)似的效果 和360小球 懸浮桌面差不錯(cuò)類(lèi)似。這種效果是如何實(shí)現(xiàn)的呢。廢話(huà)不多說(shuō) ,直接上代碼。
1.新建工程,添加懸浮窗權(quán)限。
2.自定義一個(gè)FloatMessagerMainWindow
import android.content.Context; import android.graphics.PixelFormat; import android.graphics.Point; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; import android.widget.Toast; import com.android.view.FloatMessagePopleDialog; /** * Created by liupanpan on 2017/3/16. */ public class FloatMessagerMainWindow { private Context context; private View view; private WindowManager.LayoutParams mParams = null; private WindowManager windowManager = null; private static FloatMessagerMainWindow floatMessagerMainWindow; public FloatMessagerMainWindow(Context context, View view) { this.context = context; this.view = view; showWindow(context); } public static FloatMessagerMainWindow getFloatMessagerMainWindow(Context context, View view) { if (floatMessagerMainWindow == null) { synchronized (FloatMessagerMainWindow.class) { if (floatMessagerMainWindow == null) { floatMessagerMainWindow = new FloatMessagerMainWindow(context, view); } } } return floatMessagerMainWindow; } private void showWindow(final Context context) { // if (!isWindowDismiss) { // Log.e(TAG, "view is already added here"); // return; // } // isWindowDismiss = false; if (windowManager == null) { windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); } Point size = new Point(); windowManager.getDefaultDisplay().getSize(size); int screenWidth = size.x; int screenHeight = size.y; mParams = new WindowManager.LayoutParams(); mParams.packageName = context.getPackageName(); mParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; // mParams.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | // WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN; mParams.format = PixelFormat.RGBA_8888; mParams.gravity = Gravity.LEFT | Gravity.TOP; mParams.x = screenWidth - dp2px(context, 450); mParams.y = screenHeight - dp2px(context, 550); ImageView imageView = new ImageView(context); imageView.setImageResource(R.mipmap.icon_tab_item_message_pressed); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "image=========", Toast.LENGTH_SHORT).show(); View view = LayoutInflater.from(context).inflate(R.layout.float_pople_room_layout, null); FloatMessagePopleDialog.getInstance(context, R.style.webviewTheme).setContextView(view); } }); // floatView = new AVCallFloatView(context); // floatView.setParams(mParams); // floatView.setIsShowing(true); windowManager.addView(imageView, mParams); } private int dp2px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.1f); } }
調(diào)用方法:
FloatMessagerMainWindow.getFloatMessagerMainWindow(context, null);
實(shí)現(xiàn)到此 ,點(diǎn)擊按鈕就可以實(shí)現(xiàn) 懸浮窗。(此處可能會(huì)出現(xiàn)相應(yīng)的崩潰,崩潰原因是懸浮窗的 懸浮權(quán)限開(kāi)啟問(wèn)題。)
4.我以官方模擬器為例開(kāi)啟懸浮權(quán)限:
打開(kāi)允許在其他應(yīng)用上的管理權(quán)限
此時(shí)再次打開(kāi)工程,點(diǎn)擊按鈕,就可以實(shí)現(xiàn)懸浮效果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。