簡體   English   中英

使用Android在設備的屏幕尺寸中拖放圖像

[英]Drag & Drop image in Screen size of device using android

我正在做一個在整個屏幕上移動浮動按鈕的項目。 我已經為浮動按鈕的拖放圖像編寫了一個簡單的代碼。 但它並未在設備的特定屏幕中移動。 它隱藏在頁眉中,並且從各個角度切入。 我附上了一些截圖。 如何從各個方面添加一些填充。 甚至拖到角落 在此處輸入圖片說明

這是我的拖放事件代碼:

 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>

最終經過大量研究,我找到了這個很棒的倉庫的解決方案。 使用這個庫:) github中的浮動按鈕

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM