如何在 Android 中旋转图像并在按下按钮时随机停止
How to rotate an image in Android and stop it randomly on button pressed
我正在尝试在我的 android 应用程序中制作一个简单的命运之轮。这将是一个带有人名的圆形图像。当按下下方的按钮时,图像将开始旋转(围绕其中心)。轮换需要在一个随机时间后停止,所以它当然不总是同一个人的名字。现在我只使用带有 1-2-3-4 的图像,如下所示。
我几乎查看了与此相关的所有主题,但无法弄清楚如何使其随机化。目前它总是停在相同的角度,例如总是在数字 1。
我目前拥有的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_who, container, false);
final ImageView alberto = (ImageView)view.findViewById(R.id.alberto);
Button spin =(Button)view.findViewById(R.id.spin);
final RotateAnimation anim = new RotateAnimation(0f,generateRandomNumber(), Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
anim.setFillAfter(true);
anim.setDuration(1000);
spin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alberto.startAnimation(anim);
}
});
return view;
}
public float generateRandomNumber() {
Random rand = new Random();
int randomNum = rand.nextInt((360 - 0) + 1);
return (float)randomNum;
}
}
所以基本上我给 RotateAnimation 一个随机数,所以它在停止旋转的地方并不总是相同的。但这不起作用,因为就像我说的那样,它总是停在 nr 1 上。我发现如果我改变动画的持续时间,输出就不一样了!因此,我尝试在持续时间内输入一个随机数,但那不起作用 aswell.btw 我检查了其他帖子,说它是随机的,但不是。
在理想情况下动画开始快,然后慢下来然后停止。
感谢提前分配!!!
您可以尝试以下方法:
private static final float BASE_ROTATION_DEGREES = 3600;
private static final int DURATION = 1000;
//...
spin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float deg = alberto.getRotation() + BASE_ROTATION_DEGREES + ((float)Math.random() * 360F);
alberto.animate().rotation(deg).setDuration(DURATION)
.setInterpolator(new AccelerateDecelerateInterpolator());
}
});
您也可以将插值器更改为其他东西以获得不同的效果。
编辑: 编辑以解决只有第一次旋转超过一圈的问题。请注意,这意味着旋转值会很快变高!不应构成问题,但值得牢记。
另一个编辑: Sample app/source code here 如果您想看一看。这在我的模拟器上运行良好。
我正在尝试在我的 android 应用程序中制作一个简单的命运之轮。这将是一个带有人名的圆形图像。当按下下方的按钮时,图像将开始旋转(围绕其中心)。轮换需要在一个随机时间后停止,所以它当然不总是同一个人的名字。现在我只使用带有 1-2-3-4 的图像,如下所示。
我几乎查看了与此相关的所有主题,但无法弄清楚如何使其随机化。目前它总是停在相同的角度,例如总是在数字 1。
我目前拥有的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_who, container, false);
final ImageView alberto = (ImageView)view.findViewById(R.id.alberto);
Button spin =(Button)view.findViewById(R.id.spin);
final RotateAnimation anim = new RotateAnimation(0f,generateRandomNumber(), Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
anim.setFillAfter(true);
anim.setDuration(1000);
spin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alberto.startAnimation(anim);
}
});
return view;
}
public float generateRandomNumber() {
Random rand = new Random();
int randomNum = rand.nextInt((360 - 0) + 1);
return (float)randomNum;
}
}
所以基本上我给 RotateAnimation 一个随机数,所以它在停止旋转的地方并不总是相同的。但这不起作用,因为就像我说的那样,它总是停在 nr 1 上。我发现如果我改变动画的持续时间,输出就不一样了!因此,我尝试在持续时间内输入一个随机数,但那不起作用 aswell.btw 我检查了其他帖子,说它是随机的,但不是。
在理想情况下动画开始快,然后慢下来然后停止。
感谢提前分配!!!
您可以尝试以下方法:
private static final float BASE_ROTATION_DEGREES = 3600;
private static final int DURATION = 1000;
//...
spin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float deg = alberto.getRotation() + BASE_ROTATION_DEGREES + ((float)Math.random() * 360F);
alberto.animate().rotation(deg).setDuration(DURATION)
.setInterpolator(new AccelerateDecelerateInterpolator());
}
});
您也可以将插值器更改为其他东西以获得不同的效果。
编辑: 编辑以解决只有第一次旋转超过一圈的问题。请注意,这意味着旋转值会很快变高!不应构成问题,但值得牢记。
另一个编辑: Sample app/source code here 如果您想看一看。这在我的模拟器上运行良好。