如何将一个按钮从cardview布局的中心拖动到android中的左,右,顶部和底部

问题描述 投票:-3回答:1

我想将一个按钮/视图从卡片视图布局的中心拖到它的相邻边。并且视图不应该超出卡片视图。

这就是我到目前为止所做的

 <android.support.v7.widget.CardView
    android:layout_width="300dp"
    android:layout_height="300dp"
    app:cardCornerRadius="150dp"
    app:cardElevation="0dp"
    android:gravity="center"
    app:cardBackgroundColor="#50001848"
    android:layout_centerInParent="true">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/root">
        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher_round"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>

main activity.Java

    public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
    ImageView _view;
    ViewGroup _root;
    private int _xDelta;
    private int _yDelta;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _root = findViewById(R.id.root);


        _view=findViewById(R.id.image);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(300, 300);
        layoutParams.leftMargin = 50;
        layoutParams.topMargin = 50;
        layoutParams.bottomMargin = 50;
        layoutParams.rightMargin = 50;
        _view.setLayoutParams(layoutParams);

        _view.setOnTouchListener(this);

    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) 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:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -50;
                layoutParams.bottomMargin = -50;
                view.setLayoutParams(layoutParams);
                break;
        }
        _root.invalidate();
        return true;
    }
}

当我拖动图像离开circle.i希望图像保持在圆圈内并获得圆的边界。请帮助实现这一点。

android drag-and-drop android-custom-view
1个回答
0
投票

在卡片视图中放置一个相对布局。然后在相对布局中,您可以这么简单。

© www.soinside.com 2019 - 2024. All rights reserved.