以编程方式设置视图的位置

时间:2018-12-29 13:57:25

标签: android view

我有一个带ActionListener的浮动操作按钮,可在屏幕上移动它。

1432317817.1125843050

以及将其移动的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:color/holo_blue_bright"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

一切正常,但是我在设置onCreate中存储的X和Y值时面临着一个挑战,视图仍处于其原始位置:(这是我试图做的事情

floatingActionButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    positionX = view.getX() - event.getRawX();
                    positionY = view.getY() - event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    view.animate()
                            .x(event.getRawX() + positionX)
                            .y(event.getRawY() + positionY)
                            .setDuration(0)
                            .start();
                    break;
                case MotionEvent.ACTION_UP:
                    SharedPreferences sharedPreferences = getSharedPreferences("stored_position", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putFloat("pos_x", positionX).apply();
                    editor.putFloat("pos_y", positionY).apply();
                    break;
            }
            return true;
        }
    });

我如何达到预期的效果?

2 个答案:

答案 0 :(得分:0)

您可以使用相同的调用来定位View(FAB按钮)。

    SharedPreferences sharedPreferences = getSharedPreferences("stored_position", Context.MODE_PRIVATE);
    Float savedPositionX = sharedPreferences.getFloat("pos_x", 0);
    Float savedPositionY = sharedPreferences.getFloat("pos_y", 0);

    floatingActionButton = findViewById(R.id.floatingActionButton);
    if (savedPositionX != 0 && savedPositionY != 0) {
        //this is working i have tested 
        floatingActionButton.animate()
        .x(savedPositionX)
        .y(savedPositionY)
        .setDuration(0)
        .start()
   }

但是视图的位置将与纵向和横向的宽高比不同。

答案 1 :(得分:0)

似乎您在OnCreate中设置视图的位置为时过早。该解决方案应按照{medyas的建议,在屏幕onResume中进行,或者您可以在OnGlobalLayoutListener方法中使用像这样的所谓的onCreate

fun onCreate(savedInstanceState: Bundle?) {

    // obtain view
    val fab: FloatingActionButton = ...

    // setup observer that will "wait" till view is correctly laid out
    val observer = fab.viewTreeObserver
    observer.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {

        override fun onGlobalLayout() {
            observer.removeOnGlobalLayoutListener(this)

            // TODO here run your "setup"
        }
    })
}