我有轮盘赌(ImageView),带有滚动数。当我点击按钮时,轮盘开始以270度取整。当我再次点击该按钮时,轮盘必须加快并进行更多舍入。如何在开始播放第一个动画后加快滚动速度,并在动画结束后滚动时减少滚动计数?
答案 0 :(得分:0)
由于您没有提供任何引用来显示错误的代码,因此我无法准确地理解要执行的操作,但是这样做应该可以使您在不断单击图像时更快地旋转图像,从而达到您的要求。 “旋转”按钮。
我使用了 ObjectAnimator
activity_main.xml:
<ImageView
android:id="@+id/imageView_roulette"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Rotation"
app:layout_constraintTop_toBottomOf="@+id/imageView_roulette"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/buttonStop"
app:layout_constraintRight_toRightOf="parent"
/>
<Button
android:id="@+id/buttonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
app:layout_constraintTop_toBottomOf="@+id/imageView_roulette"
app:layout_constraintLeft_toRightOf="@+id/button"
app:layout_constraintRight_toRightOf="parent"
/>
</android.support.constraint.ConstraintLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
Boolean rotating = false;
Integer durationOfSpin = 3000; //default 3000
ImageView image;
ObjectAnimator animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStart = (Button) findViewById(R.id.button);
Button buttonStop = (Button) findViewById(R.id.buttonStop);
image = (ImageView) findViewById(R.id.imageView_roulette);
buttonStart.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if(!rotating) {
rotating = true;
animation = ObjectAnimator.ofFloat(
image, "rotation", 0, 360);
animation.setDuration(durationOfSpin);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE);
animation.start();
}else{
if(durationOfSpin - 300 > 0) {
durationOfSpin -= 300; //make slightly faster each subsequent click
}
animation.setDuration(durationOfSpin);
}
}
});
buttonStop.setOnClickListener(new Button.OnClickListener() {
//just here so you can cancel your rotation
public void onClick(View v) {
if(rotating) {
rotating = false;
durationOfSpin = 3000; //set back to default
animation.cancel();
}
}
});
}
}