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);
} } }
0 comments:
Post a Comment