如何打开FloatingView(库)并在其中显示自定义布局?

时间:2016-06-18 19:23:48

标签: android android-layout android-service floating

我在我的应用中使用FloatingView库。

我已经成功启动了它,现在我想要的是我希望在点击它时会在其中显示自定义布局,就像在Facebook信使聊天头中发生的那样。

这是ChatHeadService.java文件的代码:

public class ChatHeadService extends Service implements FloatingViewListener {

    private static final String TAG = "ChatHeadService";

    private IBinder mChatHeadServiceBinder;

    private FloatingViewManager mFloatingViewManager;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mFloatingViewManager != null) {
            return START_STICKY;
        }

        final DisplayMetrics metrics = new DisplayMetrics();
        final WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        mChatHeadServiceBinder = new ChatHeadServiceBinder(this);
        final LayoutInflater inflater = LayoutInflater.from(this);
        final ImageView iconView = (ImageView) inflater.inflate(R.layout.widget_chathead, null, false);
        iconView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Charhead clicked");

            }
        });

        mFloatingViewManager = new FloatingViewManager(this, this);
        mFloatingViewManager.setFixedTrashIconImage(R.drawable.ic_trash_fixed);
        mFloatingViewManager.setActionTrashIconImage(R.drawable.ic_trash_action);
        final FloatingViewManager.Options options = new FloatingViewManager.Options();
        options.shape = FloatingViewManager.SHAPE_CIRCLE;
        options.overMargin = (int) (16 * metrics.density);
        mFloatingViewManager.addViewToWindow(iconView, options);

//        startForeground(NOTIFICATION_ID, createNotification());

        return START_REDELIVER_INTENT;

    }

    @Override
    public void onDestroy() {
        destroy();
        super.onDestroy();
    }

    private void destroy() {
        if (mFloatingViewManager != null) {
            mFloatingViewManager.removeAllViewToWindow();
            mFloatingViewManager = null;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onFinishFloatingView() {
        stopSelf();
    }


    public static class ChatHeadServiceBinder extends Binder {

        /**
         * FloatingRateService
         */
        private final WeakReference<ChatHeadService> mService;


        ChatHeadServiceBinder(ChatHeadService service) {
            mService = new WeakReference<>(service);
        }


        public ChatHeadService getService() {
            return mService.get();
        }
    }

}

以下是我从MainActivity开始的方式:

startService(new Intent(MainActivity.this, ChatHeadService.class));

请让我知道如何做我想做的事。

抱歉问题格式不正确。我还是个初学者。

0 个答案:

没有答案