在屏幕上添加一个可移动,可点击的悬浮按钮。

需求:
1.随手指移动。
2.可触发点击事件

实现步骤

在Android Studio的Gradle中导入CircularFloatingActionMenu

dependencies {
    compile 'com.oguzdev:CircularFloatingActionMenu:1.0.2'
}
  • git地址:

添加悬浮按钮

-在oncreate方法里调用下面的FloatingActionButton方法.触发事件自己随意添加

private void FloatingActionButton() {
        // Create a button to attach the menu:
        ImageView icon = new ImageView(this); // Create an icon
        icon.setImageResource(R.mipmap.ic_launcher);
        final FloatingActionButton actionButton = new FloatingActionButton.Builder(this)
                .setContentView(icon)
                .build();

        SubActionButton button1 = buildSubActionButton(MainActivity.this, R.drawable.icon_dark_edit);
        SubActionButton button2 = buildSubActionButton(MainActivity.this, R.drawable.icon_dark_edit);
        SubActionButton button3 = buildSubActionButton(MainActivity.this, R.drawable.icon_dark_edit);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SetUpActivity.actionStart(MainActivity.this);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.actionStart(MainActivity.this);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ServerTestActivity.actionStart(MainActivity.this);
            }
        });
        //Create the menu with the items:
        FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(button1)
                .addSubActionView(button2)
                .addSubActionView(button3)
                .attachTo(actionButton)
                .build();

    }

    private SubActionButton buildSubActionButton(Activity activity, int iconResourceId) {
        SubActionButton.Builder itemBuilder = new SubActionButton.Builder(activity);
        ImageView itemIcon = new ImageView(activity);
        itemIcon.setImageResource(iconResourceId);
        return itemBuilder.setContentView(itemIcon).build();
    }

设置悬浮按钮随手指移动

    private int mLastX;
    private int mLastY;
    private int mDownX;
    private int mDownY;
    private final int ADAPTER_VALUE = 25;
actionButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getRawX();
                int y = (int) event.getRawY();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mDownX = (int) event.getRawX();
                        mDownY = (int) event.getRawY();
                        break;
                    }
                    case MotionEvent.ACTION_MOVE: {
                        int deltaX = x - mLastX;
                        int deltaY = y - mLastY;
                        Log.d(TAG, "move, deltaX:" + deltaX + " deltaY:" + deltaY);

                        int translationX = (int) actionButton.getTranslationX() + deltaX;
                        int translationY = (int) actionButton.getTranslationY() + deltaY;
                        actionButton.setTranslationX(translationX);
                        actionButton.setTranslationY(translationY);
                        break;
                    }
                    case MotionEvent.ACTION_UP: {

                        break;
                    }
                    default:
                        break;
                }

                mLastX = x;
                mLastY = y;
                return true;
            }
        });

触发点击事件

因为setOnTouchListener,所以actionButton的点击事件不被触发了,这时候可以在MotionEvent.ACTION_UP中通过callOnClick()触发他的点击事件

                    case MotionEvent.ACTION_UP: {
                        if (rangeInDefined(mDownX, (int) event.getRawX() - ADAPTER_VALUE, (int) event.getRawX() + ADAPTER_VALUE)) {
                            if (rangeInDefined(mDownY, (int) event.getRawY() - ADAPTER_VALUE, (int) event.getRawY() + ADAPTER_VALUE)) {
                                actionButton.callOnClick();
                            }
                        }
                        break;
                    }

你可能感兴趣的:(安卓开发,控件)