【问题标题】:Drag & Drop image in Screen size of device using android使用 android 在设备的屏幕大小中拖放图像
【发布时间】:2016-01-25 10:50:27
【问题描述】:

我正在做一个在整个屏幕上移动浮动按钮的项目。我为浮动按钮的拖放图像编写了一个简单的代码。但它没有在设备的特定屏幕中移动。它隐藏在标题中,也从各个角落切开。我附上了一些截图。如何从各个方面添加一些填充。

这是我的拖放事件代码:

 mFloatingActionButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    Intent mIntent = new Intent(mActivity, Cart.class);
                    startActivity(mIntent);
                } else {

                    int X = (int) event.getRawX();
                    int Y = (int) event.getRawY();

                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();

                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_DOWN:
                            view.getLayoutParams();

                            _xDelta = X - lParams.leftMargin;
                            _yDelta = Y - lParams.topMargin;
                            break;
                        case MotionEvent.ACTION_UP:
                            break;
                        case MotionEvent.ACTION_POINTER_DOWN:
                            break;
                        case MotionEvent.ACTION_POINTER_UP:
                            break;
                        case MotionEvent.ACTION_MOVE:

                            if (X > windowwidth) {
                                X = windowwidth;
                            }
                            if (Y > windowheight) {
                                Y = windowheight;
                            }
                            lParams.leftMargin = X - _xDelta;
                            lParams.topMargin = Y - _yDelta;
                            lParams.rightMargin = 100;
                            lParams.bottomMargin = 100;
                            view.setLayoutParams(lParams);
                            break;
                    }
                    mRrootLayout.invalidate();
                    return true;
                }
                return true;
            }
        });

xml 文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_color">

    <in.srain.cube.views.GridViewWithHeaderAndFooter
        android:id="@+id/gridProductList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fadingEdge="none"
        android:focusable="false"
        android:listSelector="@android:color/transparent"
        android:numColumns="2"
        android:overScrollMode="never"
        android:paddingBottom="4dp"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="none"
        android:stretchMode="columnWidth" />

    <RelativeLayout
        android:id="@+id/rl_parent_floating"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
            android:id="@+id/rl_floating_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.melnykov.fab.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="3dp"
                android:src="@drawable/ic_floating_cart"
                fab:fab_colorNormal="@color/header_color_red"
                fab:fab_colorPressed="@color/colorPrimaryDark"
                fab:fab_colorRipple="@color/gold_color" />

            <TextView
                android:id="@+id/txtCartCount"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignRight="@+id/fab"
                android:layout_alignTop="@+id/fab"
                android:background="@drawable/cart_gold_circle"
                android:gravity="center"
                android:singleLine="true"
                android:text="2"
                android:textColor="@android:color/white"
                android:textSize="8sp" />

        </RelativeLayout>
    </RelativeLayout>
</FrameLayout>

【问题讨论】:

    标签: java android android-studio drag-and-drop


    【解决方案1】:

    终于经过大量研究,我找到了这个很棒的回购的解决方案。使用这个库:) Floating button in github

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      相关资源
      最近更新 更多