Advertisement
Guest User

Untitled

a guest
May 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. package com.example.myapplication;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.animation.Animation;
  7. import android.view.animation.RotateAnimation;
  8. import android.widget.ImageView;
  9.  
  10. import java.util.Timer;
  11. import java.util.Random;
  12. import java.util.TimerTask;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.     private ImageView bottle;
  16.     private Random random = new Random();
  17.     private int lastDir;
  18.     private boolean spinning;
  19.     private boolean shouldShowPopup = true;
  20.     Timer timer = new Timer();
  21.  
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.  
  28.         bottle = findViewById(R.id.bottle);
  29.  
  30.         timer.schedule(new TimerTask() {
  31.             @Override public void run() {
  32.                 showPopup();
  33.             }
  34.         }, 2);
  35.     }
  36.  
  37.     public void showPopup() {
  38.         if(shouldShowPopup) {
  39.             shouldShowPopup = false;
  40.             //TODO: afiseaza popupu aici
  41.             System.out.println("Alert shown!");
  42.         }
  43.     }
  44.  
  45.     public void spinBottle(View v) {
  46.         if (!spinning && !shouldShowPopup) {
  47.             int newDir = random.nextInt(1800);
  48.             float pivotX = bottle.getWidth() / 2;
  49.             float pivotY = bottle.getHeight() / 2;
  50.  
  51.             Animation rotate = new RotateAnimation(lastDir, newDir, pivotX, pivotY);
  52.             rotate.setDuration(2500);
  53.             rotate.setFillAfter(true);
  54.             rotate.setAnimationListener(new Animation.AnimationListener() {
  55.                 @Override
  56.                 public void onAnimationStart(Animation animation) {
  57.                     spinning = true;
  58.                 }
  59.  
  60.                 @Override
  61.                 public void onAnimationEnd(Animation animation) {
  62.                     spinning = false;
  63.                 }
  64.  
  65.                 @Override
  66.                 public void onAnimationRepeat(Animation animation) {
  67.  
  68.                 }
  69.             });
  70.  
  71.             lastDir = newDir;
  72.             bottle.startAnimation(rotate);
  73.         } else if (shouldShowPopup) {
  74.             showPopup();
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement