如何制作像ludo游戏一样的骰子滚动画?

时间:2018-06-10 03:51:55

标签: android animation dice

我正在制作一个游戏,因为骰子有重要的推动力。因此我想在它滚动时给出一些3D滚动效果 我成功地摇动了骰子的动画,但这并不吸引人。我想要骰子滚动然后在流行的ludo游戏中显示3D数字。任何帮助。提前谢谢。

这是我的摇骰动画文件

shake.xml

 <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="20"
        android:fromDegrees="0"
        android:interpolator="@android:anim/cycle_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="5"
        android:repeatMode="reverse"
        android:toDegrees="360" />

    <translate
        android:duration="20"
        android:fromXDelta="-10"
        android:interpolator="@android:anim/linear_interpolator"
        android:repeatCount="5"
        android:repeatMode="reverse"
        android:toXDelta="10" />

</set>

这是我的java代码

MainActivity.java
package com.example.sumit.dice;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    public static final Random RANDOM = new Random();
    private Button roll;
    private ImageView leftdice,rightdice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        roll=(Button) findViewById(R.id.button);
          leftdice=findViewById(R.id.li);
         rightdice=findViewById(R.id.ri);

         int[] diceArray={
                R.drawable.dice1,
                R.drawable.dice2,
                R.drawable.dice3,
                R.drawable.dice4,
                R.drawable.dice5,
                R.drawable.dice6};
        roll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Animation anim1= AnimationUtils.loadAnimation(MainActivity.this, R.anim.shake);
                final Animation anim2 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.shake);
                final Animation.AnimationListener animationListener=new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                   int value=randomDiceValue();
                    int res=getResources().getIdentifier("dice" + value, "drawable", "com.example.sumit.dice");

                        if (animation == anim1) {
                            leftdice.setImageResource(res);
                        } else if (animation == anim2) {
                            rightdice.setImageResource(res);
                        }
                    }


                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                };
                anim1.setAnimationListener(animationListener);
                anim2.setAnimationListener(animationListener);

                leftdice.startAnimation(anim1);
                rightdice.startAnimation(anim2);

            }
        });
    }
     public static int randomDiceValue(){
     return RANDOM.nextInt(6)+1;
    }
}

这是我的MainActivity.xml文件代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:background="@color/colorAccent"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/img"
        android:src="@drawable/d"
        android:layout_marginTop="10dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2"
        >

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:src="@drawable/dice1"
            android:id="@+id/li"/>

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:layout_marginRight="40dp"
            android:src="@drawable/dice2"
            android:id="@+id/ri"/>
    </LinearLayout>
   <Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="10dp"
       android:layout_gravity="center"
       android:background="@color/colorPrimaryDark"
       android:text="roll" />
</LinearLayout>

0 个答案:

没有答案