How To Create Spinier Game In Android - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
How To Create Spinier Game In Android

How To Create Spinier Game In Android

How To  Create Spinier Game In Android 



activity_main.xml

<?xml version="1.0"
 encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  xmlns:app="http://schemas.android.com/apk/res-auto"  
  xmlns:tools="http://schemas.android.com/tools"  
  android:layout_width="match_parent" 
   android:layout_height="match_parent"  
  android:background="#ffff" 
   tools:context=".MainActivity">

    <ImageView    
         android:onClick="spinBottel"  
         android:id="@+id/bottel_img"  
         android:padding="90dp"    
         android:layout_width="match_parent"     
         android:layout_height="match_parent"     
        android:src="@drawable/bottel" />
</RelativeLayout>

Out put


MainActivity.java

package com.dregan.bottelspinnergame;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
    private ImageView bottelImage;
    private Random random=new Random();
    private int lastDir;
    private boolean spinning;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottelImage=findViewById(R.id.bottel_img);

    }
      public  void spinBottel(View view)
      {
          if(!spinning )
          {
              int newDir = random.nextInt(1800);
              float pivotX=bottelImage.getWidth()/2;
              float pivotY=bottelImage.getHeight()/2;

              Animation animation=new RotateAnimation(lastDir, newDir,pivotX,pivotY);

              animation.setDuration(2500);
              animation.setFillAfter(true);
              animation.setAnimationListener(new Animation.AnimationListener()
                   {

                  @Override 
                    public void onAnimationStart(Animation animation) {
                      spinning=true; }

                  @Override   
                  public void onAnimationEnd(Animation animation) {
                          spinning=false;


                  }

                  @Override 
                 public void onAnimationRepeat(Animation animation) {

                  }
              });

              lastDir =newDir;
              bottelImage.startAnimation(animation);

              }

           }

      }


                 






About EasyToCode

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.