【问题标题】:Moving a button randomly onClick() with ObjectAnimator使用 ObjectAnimator 随机移动按钮 onClick()
【发布时间】:2021-12-04 00:20:53
【问题描述】:

我有这个代码:

button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ObjectAnimator  animX = ObjectAnimator.ofFloat(button3, "X", 0f, 50f);
            ObjectAnimator animY = ObjectAnimator.ofFloat(button3, "Y", 0f, 50f);

            AnimatorSet animSet = new AnimatorSet();
            animSet.playSequentially(animX, animY);
            animSet.setDuration(500);


            animSet.start();
        }
    });

我正在尝试使用 ObjectAnimator 在屏幕上随机移动“button3”一段时间,但我不知道如何将 X 和 Y 值相乘以使按钮位置随机。

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    你可以这样做。在我的示例中,我展示了按钮仅在其容器内移动(id=parentView)并且不会超出它。我限制了宽度和高度,你可以改变你想要的值(或ma​​tch_parent

    MainActivity

    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.FrameLayout;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    import java.util.Random;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
            Button button = findViewById(R.id.button);
            FrameLayout parent = findViewById(R.id.parentView);
    
            Random random = new Random();
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    float randomX = (float) (random.nextInt(parent.getWidth() - button.getWidth()));
                    float randomY = (float) (random.nextInt(parent.getHeight() - button.getHeight()));
    
                    button.animate().x(randomX).y(randomY).setDuration(500).start();
                }
            });
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
            android:id="@+id/parentView"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:background="@android:color/darker_gray">
    
            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </FrameLayout>
    
    </FrameLayout>
    

    【讨论】:

      【解决方案2】:
       button3.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      int rx = new Random().nextInt(50);
                      int ry = new Random().nextInt(50);
                      ObjectAnimator animX = ObjectAnimator.ofFloat(button3, View.X, new Random().nextBoolean() ? rx : rx * -1);
                      ObjectAnimator animY = ObjectAnimator.ofFloat(button3, View.Y, new Random().nextBoolean() ? ry : ry * -1);
      
                      AnimatorSet animSet = new AnimatorSet();
                      animSet.playSequentially(animX, animY);
                      animSet.setDuration(500);
      
      
                      animSet.start();
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-22
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多