简体   繁体   English

如何在Android中旋转图像并在按下按钮时随机停止图像

[英]How to rotate an image in Android and stop it randomly on button pressed

im trying to make a simple wheel of fortune in my android application. 我试图在我的android应用程序中做一个简单的命运之轮。 This will be a rounded image with person names. 这将是带有人名的四舍五入图像。 The image will start rotating(arround its center) when the button below it is pressed. 按下下方的按钮,图像将开始旋转(围绕其中心)。 The rotation needs to be stopped after a random time, so its not always the same person name ofcourse. 轮播需要在随机时间后停止,因此其课程名称不一定总是相同的人。 for now i just use an image with 1-2-3-4, se below. 现在,我只使用下面带有1-2-3-4的图像。

Output example 输出示例

Ive looked at nearly every topic regarding this, but cant figure it out how to make it random. 我已经看过几乎所有与此有关的话题,但无法弄清楚如何使其随机化。 At the moment it always stops at the same angle, for instance always at number 1. 此刻它总是以相同的角度停止,例如总是以1号停止。

The code i have sofar: 我有沙发的代码:

@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;
}

} }

So basicaly i give the RotateAnimation a random number, so its not always the same where it stops rotating. 所以基本上我给RotateAnimation一个随机数,所以它在停止旋转的地方并不总是相同的。 But this is not working becaus like i said it always stops on nr 1 for example. 但这是行不通的,因为像我说的那样,它总是在nr 1上停止。 i figured out that if i change the duration of the animation the output is not the same! 我发现如果我更改动画的持续时间,输出将不一样! Therefor i tryed put a random number in in the duration but thats not working aswell.btw i have checked other posts that say its random but its not. 因此,我尝试在持续时间内输入一个随机数,但那也不起作用。顺便说一句,我检查了其他帖子,说它是随机的,但不是。

In the ideal situation the animation starts fast and slows down and stops. 在理想情况下,动画开始快速,然后放慢并停止。

Thanx allot in advanced!!! 感谢高级分配!!!

You could try the following: 您可以尝试以下方法:

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());
    }
});

You could change the interpolator to something else as well to get a different effect. 您也可以将插值器更改为其他值以获得不同的效果。

Edit: Edited to fix the problem whereby only the first rotation was more than one revolution. 编辑:编辑以解决仅第一转超过一转的问题。 Note that this means the rotation value will get high pretty quickly! 请注意,这意味着旋转值将很快变高! Shouldn't pose an issue but worth keeping in mind. 不应该提出问题,但值得牢记。

Another Edit: Sample app/source code here if you wanted to take a look. 如果您想看一看, 在这里进行 另一个编辑: 示例应用程序/源代码 This runs fine on my emulator. 这在我的模拟器上运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM