1. 程式人生 > >在螢幕上新增一個可移動,可點選的懸浮按鈕。

在螢幕上新增一個可移動,可點選的懸浮按鈕。

需求:
1.隨手指移動。
2.可觸發點選事件

實現步驟

在Android Studio的Gradle中匯入CircularFloatingActionMenu

dependencies {
    compile 'com.oguzdev:CircularFloatingActionMenu:1.0.2'
}

新增懸浮按鈕

-在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;
                    }