拖放时调整ImageView的大小

时间:2020-05-04 14:36:15

标签: android drag-and-drop imageview resize

我目前正在尝试使用imagesView进行拖放。

移动正常,但是当图像接近RelativeLayout的右边缘和下边缘时,它会自动调整大小。

相对布局的左右边缘没有这种尺寸变化。

有人可以向我解释为什么调整图像大小以及如何将其保持在原始状态吗?

代码如下:

Java

        piece1 = (ImageView) findViewById(R.id.piece1);
        piece2 = (ImageView) findViewById(R.id.piece2);

        piece1.setOnTouchListener(pieceListener);
        piece2.setOnTouchListener(pieceListener);


        ....

   View.OnTouchListener pieceListener = new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        RelativeLayout.LayoutParams relativeLayoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                Log.d("TAG","Start Drag");
                // Where the user started the drag
                pressed_x = (int) event.getRawX();
                pressed_y = (int) event.getRawY();

                // On sauvegarde la position de départ
                dep_x = (int) event.getRawX();
                dep_y = (int) event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                Log.d("TAG","Drag en cours");
                // Where the user's finger is during the drag
                final int x = (int) event.getRawX();
                final int y = (int) event.getRawY();

                // Calculate change in x and change in y
                int dx = x - pressed_x;
                int dy = y - pressed_y;

                // Update the margins
                relativeLayoutParams.leftMargin += dx;
                relativeLayoutParams.topMargin += dy;
                view.setLayoutParams(relativeLayoutParams);

                // Save where the user's finger was for the next ACTION_MOVE
                pressed_x = x;
                pressed_y = y;
                break;

            case MotionEvent.ACTION_UP:
                Log.d("TAG","End Drag");

                //Si la position n'a pas changé par rapport au départ on applique la rotation
                if (pressed_x == dep_x && pressed_y == dep_y)
                {
                    view.setRotation(view.getRotation()+180);
                }
                break;
        }

        return true;
    }
};

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/fond_main">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/tablette">
        </ImageView>
        <ImageView
            android:id="@+id/piece1"
            android:layout_width="@dimen/size_paques_fleches_width"
            android:layout_height="@dimen/size_paques_fleches_height"
            android:src="@drawable/fleche">
        </ImageView>
        <ImageView
            android:id="@+id/piece2"
            android:layout_width="@dimen/size_paques_fleches_width"
            android:layout_height="@dimen/size_paques_fleches_height"
            android:src="@drawable/fleche_selec"
            android:layout_marginTop="100dp">
        </ImageView>
    </RelativeLayout>
    <LinearLayout
        android:orientation= "horizontal"
        android:layout_weight="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="@dimen/size_chronometer"
            android:layout_height="@dimen/size_chronometer"
            android:layout_gravity="center"
            android:clickable="true"
            android:focusable="true"
            android:src="@drawable/chrono" />
        <Chronometer
            android:id="@+id/simpleChronometervirus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="@dimen/size_chronometer"
            android:textColor="#f0f6f7"/>
    </LinearLayout>
</LinearLayout>
</RelativeLayout>

0 个答案:

没有答案