English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
How can you not play red packet entertainment during the Spring Festival? The dice rolling game and rock-paper-scissors in WeChat are good ways to play.
It is easy to implement with Java's Random function, taking the dice as an example:
1.The dice rolling animation uses animation-animation implementation with list
2.generate1~6of the random number,1 + new Random().nextInt(6)
The following is the code implementation, and the image resource download is provided at the end of the text:
package com.zms.dicedemo; import java.util.Random; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private ImageView imageDice; private Button btnGo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initialLayout(); } private void initialLayout() { imageDice = (ImageView) findViewById(R.id.imageDice); btnGo = (Button) findViewById(R.id.btnGo); btnGo.setOnClickListener(new MyOnClickListener()); } class MyOnClickListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnGo: imageDice.setImageDrawable(getResources().getDrawable( R.drawable.anim_dice)); new Thread(new DiceThread()).start(); break; default: break; } } } public class DiceThread implements Runnable { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } Message messageDice = new Message(); messageDice.what = 1; diceHandler.sendMessage(messageDice); } } private void resetDiceImage() { int randomInt = 1 + new Random().nextInt(6); // 1~6 Toast.makeText(MainActivity.this, getChineseByNumber(randomInt), Toast.LENGTH_SHORT).show(); imageDice.setImageDrawable(getResources().getDrawable( getResources().getIdentifier("dice_"} + randomInt, "drawable", getPackageName()))); } private String getChineseByNumber(int number) { switch (number) { case 1: return "Small one"; case 2: return "Small two"; case 3: return "Small three"; case 4: return "Big four"; case 5: return "Big five"; case 6: return "Big six"; default: return ""; } } final Handler diceHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: resetDiceImage(); break; default: break; } } }; }
The random numbers generated by Random are pseudo-random numbers, but they can still be used in entertainment where security requirements are not high.
The implementation method is relatively simple, and here is the link to download the project:Android WeChat dice rolling
That's all for this article. Hope it helps everyone's learning and also hope everyone will support the Yell Tutorial.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace # with @ when sending an email to report infringement. Provide relevant evidence, and once verified, the website will immediately delete the infringing content.