大家想测试的话,APK文件放到压缩包里了。下载压缩文件安装到手机测试
初次写了一个Android 猜拳游戏的小Demo 测试一下,代码如下:
MainActivity文件
import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
private ImageButton jiandao, shitou, bu;
private ImageView computerpic, computerpic1, iv1;
private int[] pic = new int[] { R.drawable.jiandao, R.drawable.shitou,
R.drawable.bu };
private TextView result;
private Button reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
reset = (Button) findViewById(R.id.reset);
result = (TextView) findViewById(R.id.textview2);
jiandao = (ImageButton) findViewById(R.id.imagejiandao);
shitou = (ImageButton) findViewById(R.id.imageshitou);
bu = (ImageButton) findViewById(R.id.imagebu);
computerpic = (ImageView) findViewById(R.id.imagecomputer);
computerpic1 = (ImageView) findViewById(R.id.imagecomputer1);
iv1 = (ImageView) findViewById(R.id.iv1);
final AnimationDrawable ad = (AnimationDrawable) computerpic
.getBackground();
final AnimationDrawable ad1 = (AnimationDrawable) iv1.getBackground();
ad1.start();
ad.start();
reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ad.start();
computerpic.setVisibility(View.VISIBLE);
computerpic1.setVisibility(View.INVISIBLE);
}
});
jiandao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ad.stop();
computerpic.setVisibility(View.INVISIBLE);
int x = (int) (Math.random() * 3);
if (x == 0) {
result.setText("平了");
computerpic1.setImageResource(pic[x]);
} else if (x == 1) {
result.setText("输了");
computerpic1.setImageResource(pic[x]);
} else if (x == 2) {
result.setText("赢了");
computerpic1.setImageResource(pic[x]);
}
computerpic1.setVisibility(View.VISIBLE);
}
});
shitou.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ad.stop();
computerpic.setVisibility(View.INVISIBLE);
int x = (int) (Math.random() * 3);
if (x == 0) {
result.setText("赢了");
computerpic1.setImageResource(pic[x]);
} else if (x == 1) {
result.setText("平了");
computerpic1.setImageResource(pic[x]);
} else if (x == 2) {
result.setText("输了");
computerpic1.setImageResource(pic[x]);
}
computerpic1.setVisibility(View.VISIBLE);
}
});
bu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ad.stop();
computerpic.setVisibility(View.INVISIBLE);
int x = (int) (Math.random() * 3);
if (x == 0) {
result.setText("输了");
computerpic1.setImageResource(pic[x]);
} else if (x == 1) {
result.setText("赢了");
computerpic1.setImageResource(pic[x]);
} else if (x == 2) {
result.setText("平了");
computerpic1.setImageResource(pic[x]);
}
computerpic1.setVisibility(View.VISIBLE);
}
});
}
}
布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dh"
/>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="猜拳游戏"
android:textSize="24sp" />
<TextView
android:id="@+id/computertextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview1"
android:layout_marginLeft="40dp"
android:text="电脑出拳:"
android:textSize="20sp" />
<TextView
android:id="@+id/wanjiatextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview1"
android:layout_marginLeft="70dp"
android:layout_toRightOf="@id/computertextview"
android:text="玩家出拳:"
android:textSize="20sp" />
<ImageButton
android:id="@+id/imagejiandao"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignLeft="@id/wanjiatextview"
android:layout_below="@id/wanjiatextview"
android:layout_marginTop="20dp"
android:background="@drawable/jiandao" />
<ImageButton
android:id="@+id/imageshitou"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignLeft="@id/wanjiatextview"
android:layout_below="@id/imagejiandao"
android:layout_marginTop="15dp"
android:background="@drawable/shitou" />
<ImageButton
android:id="@+id/imagebu"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/wanjiatextview"
android:layout_below="@id/imageshitou"
android:layout_marginTop="15dp"
android:background="@drawable/bu" />
<ImageView
android:id="@+id/imagecomputer"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/imagejiandao"
android:layout_marginLeft="40dp"
android:layout_marginTop="15dp"
android:background="@drawable/donghua" />
<ImageView
android:id="@+id/imagecomputer1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/imagejiandao"
android:layout_marginLeft="40dp"
android:layout_marginTop="15dp" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imagebu"
android:layout_centerHorizontal="true"
android:text=""
android:textSize="24sp" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview2"
android:layout_centerHorizontal="true"
android:text="重新开始"
android:textSize="22sp" />
</RelativeLayout>
在 drawable目录下创建 donghua.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/jiandao"
android:duration="100">
</item>
<item
android:drawable="@drawable/shitou"
android:duration="100">
</item>
<item
android:drawable="@drawable/bu"
android:duration="100">
</item>
</animation-list>
在 drawable目录下创建 donghua.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/a1"
android:duration="200">
</item>
<item
android:drawable="@drawable/a2"
android:duration="200">
</item>
<item
android:drawable="@drawable/a3"
android:duration="200">
</item>
<item
android:drawable="@drawable/a4"
android:duration="200">
</item>
<item
android:drawable="@drawable/a5"
android:duration="200">
</item>
<item
android:drawable="@drawable/a6"
android:duration="200">
</item>
</animation-list>
|
|