本文实例为大家分享了Android悬浮窗菜单的具体代码,供大家参考,具体内容如下
MainActivity.java代码:
package siso.multilistview; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); hideBothNavigationBarAndStatusBar(); setContentView(R.layout.activity_main); FloatMenuManager.getInstance().startFloatView(this.getApplicationContext()); findViewById(R.id.hideStatuBarNaviBar).setOnClickListener(this); } private void hideBothNavigationBarAndStatusBar() { View decorView = getWindow().getDecorView(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); } } @Override protected void onResume() { super.onResume(); hideBothNavigationBarAndStatusBar(); FloatMenuManager.getInstance().showFloatingView(); } @Override protected void onPause() { super.onPause(); FloatMenuManager.getInstance().hideFloatingView(); } @Override protected void onDestroy() { super.onDestroy(); FloatMenuManager.getInstance().destroy(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.hideStatuBarNaviBar: hideBothNavigationBarAndStatusBar(); break; } } }
Const.java代码:
package siso.multilistview; public interface Const { String GAME_URL = "http://www.cnblogs.com/cate/html5/"; String HOME = "首页"; String FAVOUR = "收藏"; String FEEDBACK = "客服"; String MESSAGE = "消息"; String CLOSE = "关闭"; String[] MENU_ITEMS = {HOME, FAVOUR, FEEDBACK, MESSAGE, CLOSE}; }
FloatMenuManager.java代码:
package siso.multilistview; import android.content.ComponentName; import android.content.Context; import android.os.IBinder; import java.io.ObjectStreamException; public class FloatMenuManager implements ServiceConnectionManager.QdServiceConnection { private ServiceConnectionManager mServiceConnectionManager; private FloatMenuManager() { } //静态内部类实现单例 优于双重检查锁(DCL)单例 public static FloatMenuManager getInstance() { return FloatMenuHolder.single; } /** * 静态内部类能够解决DCL双重检查锁失效的问题 */ private static class FloatMenuHolder { private static final FloatMenuManager single = new FloatMenuManager(); } /** * 防止反序列获取新的单例 * * @return * @throws ObjectStreamException */ private Object readResolve() throws ObjectStreamException { return FloatMenuHolder.single; } private FloatMenuService mFloatViewService; public void startFloatView(Context context) { if (mFloatViewService != null) { mFloatViewService.showFloat(); return; } if (mServiceConnectionManager == null) { mServiceConnectionManager = new ServiceConnectionManager(context, FloatMenuService.class, this); mServiceConnectionManager.bindToService(); } } /** */ public void addFloatMenuItem() { if (mFloatViewService != null) { } } /** * */ public void removeMenuItem() { if (mFloatViewService != null) { } } /** * 显示悬浮图标 */ public void showFloatingView() { if (mFloatViewService != null) { mFloatViewService.showFloat(); } } /** * 隐藏悬浮图标 */ public void hideFloatingView() { if (mFloatViewService != null) { mFloatViewService.hideFloat(); } } /** * 释放QDSDK数据 */ public void destroy() { if (mFloatViewService != null) { mFloatViewService.hideFloat(); mFloatViewService.destroyFloat(); } if (mServiceConnectionManager != null) { mServiceConnectionManager.unbindFromService(); } mFloatViewService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { mFloatViewService = ((FloatMenuService.FloatMenuServiceBinder) service).getService(); if (mFloatViewService != null) { mFloatViewService.showFloat(); } } @Override public void onServiceDisconnected(ComponentName name) { mFloatViewService = null; } }
FloatMenuService.java代码:
package siso.multilistview; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Binder; import android.os.Handler; import android.os.IBinder; import android.view.View; import android.widget.Toast; import com.yw.game.sclib.Sc; import com.yw.game.sclib.ScCreateResultCallback; import java.util.ArrayList; import siso.floatmenu.FloatMenu; import siso.floatmenu.MenuItem; import siso.floatmenu.MenuItemView; public class FloatMenuService extends Service implements View.OnClickListener { private FloatMenu mFloatMenu; private final static String TAG = FloatMenuService.class.getSimpleName(); private Handler mHandler = new Handler(); private int[] menuIcons = new int[]{R.drawable.yw_menu_account, R.drawable.yw_menu_favour, R.drawable.yw_menu_fb, R.drawable.yw_menu_msg, R.drawable.yw_menu_close}; @Override public IBinder onBind(Intent intent) { return new FloatMenuServiceBinder(); } /** * On create. */ @Override public void onCreate() { super.onCreate(); ArrayList<MenuItem> mMenuItems = new ArrayList<>(); for (int i = 0; i < menuIcons.length; i++) { mMenuItems.add(new MenuItem(menuIcons[i], Const.MENU_ITEMS[i], android.R.color.black, this)); } mFloatMenu = new FloatMenu.Builder(this).menuItems(mMenuItems).build(); mFloatMenu.show(); } /** * On click. * * @param v the v */ @Override public void onClick(View v) { if (v instanceof MenuItemView) { MenuItemView menuItemView = (MenuItemView) v; String menuItemLabel = menuItemView.getMenuItem().getLabel(); Toast.makeText(this, menuItemLabel, Toast.LENGTH_SHORT).show(); switch (menuItemLabel) { case Const.HOME: // TODO WHAT U WANT 此处模拟联网操作 mFloatMenu.startLoaderAnim(); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } mHandler.post(new Runnable() { @Override public void run() { mFloatMenu.stopLoaderAnim(); goHomeIndex(FloatMenuService.this); } }); } }).start(); break; case Const.FAVOUR: createSc(); break; case Const.FEEDBACK: break; case Const.MESSAGE: if (hasNewMsg) { hasNewMsg = false; } else { hasNewMsg = true; } showRed(); break; case Const.CLOSE: hideFloat(); break; } } } private boolean hasNewMsg = false; private void showRed() { if (!hasNewMsg) { mFloatMenu.changeLogo(R.drawable.yw_image_float_logo, R.drawable.yw_menu_msg, 3); } else { mFloatMenu.changeLogo(R.drawable.yw_image_float_logo_red, R.drawable.yw_menu_msg_red, 3); } } private void createSc() { //在service中的使用场景 PackageManager pm = this.getPackageManager(); ApplicationInfo appInfo = FloatMenuService.this.getApplicationInfo(); Drawable drawable = appInfo.loadIcon(pm);//当前app的logo String name = appInfo.loadLabel(pm).toString();//当前app的名称 Intent intent = pm.getLaunchIntentForPackage(appInfo.packageName);//当前app的入口程序 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); new Sc.Builder(this, intent). setName(name). setAllowRepeat(true). setIcon(drawable). setCallBack(new ScCreateResultCallback() { @Override public void createSuccessed(String createdOrUpdate, Object tag) { Toast.makeText(FloatMenuService.this, createdOrUpdate, Toast.LENGTH_SHORT).show(); } @Override public void createError(String errorMsg, Object tag) { Toast.makeText(FloatMenuService.this, errorMsg, Toast.LENGTH_SHORT).show(); } }).build().createSc(); } /** * Show float. */ public void showFloat() { if (mFloatMenu != null) mFloatMenu.show(); } /** * Hide float. */ public void hideFloat() { if (mFloatMenu != null) { mFloatMenu.hide(); } } /** * Destroy float. */ public void destroyFloat() { hideFloat(); if (mFloatMenu != null) { mFloatMenu.destroy(); } mFloatMenu = null; } /** * On destroy. */ @Override public void onDestroy() { super.onDestroy(); destroyFloat(); } public class FloatMenuServiceBinder extends Binder { public FloatMenuService getService() { return FloatMenuService.this; } } private void goHomeIndex(Context context) { Uri uri = Uri.parse(Const.GAME_URL); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } }
ServiceConnectionManager.java代码:
package siso.multilistview; import android.app.Service; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; public class ServiceConnectionManager implements ServiceConnection { private final Context context; private final Class<? extends Service> service; private boolean attemptingToBind = false; private boolean bound = false; private QdServiceConnection mQdServiceConnection; public ServiceConnectionManager(Context context, Class<? extends Service> service, QdServiceConnection mQdServiceConnection) { this.context = context; this.service = service; this.mQdServiceConnection = mQdServiceConnection; } public void bindToService() { if (!attemptingToBind) { attemptingToBind = true; context.bindService(new Intent(context, service), this, Context.BIND_AUTO_CREATE); } } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { attemptingToBind = false; bound = true; mQdServiceConnection.onServiceConnected(componentName, iBinder); } @Override public void onServiceDisconnected(ComponentName componentName) { mQdServiceConnection.onServiceDisconnected(componentName); bound = false; } public void unbindFromService() { attemptingToBind = false; if (bound) { context.unbindService(this); bound = false; } } public interface QdServiceConnection { void onServiceConnected(ComponentName name, IBinder service); void onServiceDisconnected(ComponentName name); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" android:clipToPadding="false" tools:context=".MainActivity"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="通过服务启动浮动菜单" android:textAppearance="@style/TextAppearance.AppCompat.Body2"/> <Button android:id="@+id/hideStatuBarNaviBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/text" android:text="隐藏状态栏和导航栏"/> </RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="siso.multilistview"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <!-- android:configChanges="keyboardHidden|orientation|screenSize" 防止横竖屏切换时重新执行oncreate--> <activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".FloatMenuService"/> </application> </manifest>
Android Library Project(库项目)结构:
项目运行如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
本文向大家介绍Android学习教程之圆形Menu菜单制作方法(1),包括了Android学习教程之圆形Menu菜单制作方法(1)的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Android圆形菜单的使用方法,供大家参考,具体内容如下 MainActivity.java代码: activity_main.xml内容: CirclemenuActivity.java代码: Circl
本文向大家介绍javascript手工制作悬浮菜单,包括了javascript手工制作悬浮菜单的使用技巧和注意事项,需要的朋友参考一下 有选择性的重复造一些轮子,未必是件坏事。Aaron的博客上加了一个悬浮菜单,貌似显得很高大上了。虽然这类小把戏也不是头一次见了,但是从未自己写过。今天就选择性的拿这个功能写一写。下面是这个轮子的开发过程,也可以当作是一篇需求文档的分析和实现过程。 演示地址:htt
本文向大家介绍Python 学习教程之networkx,包括了Python 学习教程之networkx的使用技巧和注意事项,需要的朋友参考一下 networkx是Python的一个包,用于构建和操作复杂的图结构,提供分析图的算法。图是由顶点、边和可选的属性构成的数据结构,顶点表示数据,边是由两个顶点唯一确定的,表示两个顶点之间的关系。顶点和边也可以拥有更多的属性,以存储更多的信息。 对于netwo
本文向大家介绍android编程实现悬浮窗体的方法,包括了android编程实现悬浮窗体的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了android编程实现悬浮窗体的方法。分享给大家供大家参考,具体如下: 突然对悬浮窗体感兴趣,查资料做了个小Demo,效果是点击按钮后,关闭当前Activity,显示悬浮窗口,窗口可以拖动,双击后消失。效果图如下: 它的使用原理很简单,就是借用了Wi
本文向大家介绍socket.io学习教程之深入学习篇(三),包括了socket.io学习教程之深入学习篇(三)的使用技巧和注意事项,需要的朋友参考一下 前言 socket.io提供了基于事件的实时双向通讯,本文深入的介绍了socket.io,下面来看看详细的内容吧。 静态文件 socket.io默认情况下会通过socket.io-client包提供socket.io.min.js和socket.i
本文向大家介绍Perl学习教程之单行命令详解,包括了Perl学习教程之单行命令详解的使用技巧和注意事项,需要的朋友参考一下 前言 本文主要给大家介绍了关于Perl单行命令的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 与One-Liner相关的perl参数 -a 自动分隔模式,用空格分隔$并保存在@F中,也就是@F=split //, $ -F 指定-a的分隔符 -l