国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android仿360懸浮小球自定義view實現示例

2019-10-23 18:33:20
字體:
來源:轉載
供稿:網友

Android仿360懸浮小球自定義view實現示例

效果圖如下:

android,懸浮小球,360懸浮球,仿360懸浮窗

android,懸浮小球,360懸浮球,仿360懸浮窗

實現當前這種類似的效果 和360小球 懸浮桌面差不錯類似。這種效果是如何實現的呢。廢話不多說 ,直接上代碼。

1.新建工程,添加懸浮窗權限。

<uses-permission android/130564.html">android/208530.html">android:name="android.permission.SYSTEM_ALERT_WINDOW" />

2.自定義一個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); }}

調用方法:

FloatMessagerMainWindow.getFloatMessagerMainWindow(context, null);

實現到此 ,點擊按鈕就可以實現 懸浮窗。(此處可能會出現相應的崩潰,崩潰原因是懸浮窗的 懸浮權限開啟問題。)

4.我以官方模擬器為例開啟懸浮權限:

android,懸浮小球,360懸浮球,仿360懸浮窗

打開允許在其他應用上的管理權限

android,懸浮小球,360懸浮球,仿360懸浮窗

此時再次打開工程,點擊按鈕,就可以實現懸浮效果。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石台县| 河间市| 宜川县| 个旧市| 临漳县| 通化县| 梨树县| 长治市| 镇安县| 井冈山市| 那坡县| 敖汉旗| 化州市| 张家港市| 英德市| 兰坪| 南宫市| 道真| 莎车县| 大城县| 和硕县| 色达县| 淮滨县| 湖南省| 仁化县| 九龙城区| 乌鲁木齐县| 澄迈县| 宁城县| 独山县| 永城市| 年辖:市辖区| 开江县| 沐川县| 登封市| 文登市| 连江县| 朝阳市| 浪卡子县| 绵阳市| 藁城市|