首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用onClick()随机移动按钮ObjectAnimator

使用onClick()随机移动按钮ObjectAnimator
EN

Stack Overflow用户
提问于 2021-12-04 00:20:53
回答 2查看 77关注 0票数 1

我有这样的代码:

代码语言:javascript
复制
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();
        }
    });

我试图在屏幕上随机移动"button3“和ObjectAnimator,但我不知道如何将X值和Y值相乘,使按钮位置变得随机。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-04 00:55:35

你可以这样做。在我的示例中,我展示了该按钮仅在其container(id=parentView)中移动,并且没有超出它。我限制宽度和高度,您可以更改所需的值(或match_parent)。

MainActivity

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>

票数 1
EN

Stack Overflow用户

发布于 2021-12-04 04:57:20

代码语言:javascript
复制
 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();
            }
        });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70222047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档