요건 1 ~ 50 까지 빠른 시간에 누르는 게임을..
간단하게 만들어 본겁니다..
저는 0 ~ 31 까지만.. 별 다른 이유는 없고 개발자 맘 이니까요..
이하 풀 소스...
package com.example.findnumber;
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class FindNumber extends Activity{
private Button startBtn;
private TextView tvTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onCreate(savedInstanceState);
LayoutParams lp;
lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
RelativeLayout gameView = new RelativeLayout(this);
gameView.setLayoutParams(lp);
setContentView(gameView);
LinearLayout cv = new LinearLayout(this);
cv.setLayoutParams(lp);
cv.setOrientation(LinearLayout.VERTICAL);
cv.setBackgroundColor(Color.rgb(255, 255, 255));
gameView.addView(cv);
lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView tvLabel = new TextView(this);
tvLabel.setLayoutParams(lp);
tvLabel.setBackgroundColor(Color.BLACK);
tvLabel.setTextColor(Color.WHITE);
tvLabel.setTextSize(18f);
tvLabel.setGravity(Gravity.CENTER);
tvLabel.setText("0 에서 31 번까지\n 버튼을 빠르게 누르는 게임입니다.");
cv.addView(tvLabel);
tvTime = new TextView(this);
tvTime.setLayoutParams(lp);
tvTime.setBackgroundColor(Color.BLACK);
tvTime.setTextColor(Color.RED);
tvTime.setHeight(100);
tvTime.setTextSize(24f);
tvTime.setGravity(Gravity.CENTER);
tvTime.setText("경과시간 : 00 초");
cv.addView(tvTime);
startBtn = new Button(this);
startBtn.setLayoutParams(lp);
startBtn.setTextSize(32f);
startBtn.setText("START GAME");
startBtn.setOnClickListener(BtnClick);
cv.addView(startBtn);
gv = new RelativeLayout(this);
lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
gv.setLayoutParams(lp);
cv.addView(gv);
int btnId = 16;
int nb = 4;
for(int ny=0; ny<nb; ny++){
for(int nx=0; nx<nb; nx++){
gv.addView( CreateButton(ny, nx, String.valueOf(btnId++)) );
}
}
btnId = 0;
for(int ny=0; ny<nb; ny++){
for(int nx=0; nx<nb; nx++){
gv.addView( CreateButton(ny, nx, String.valueOf(btnId++)) );
}
}
}
private RelativeLayout gv;
private Button CreateButton(int ny, int nx, String label){
int sw = getWindow().getWindowManager().getDefaultDisplay().getWidth();
int nb = 4;
int BtnWidth = sw / nb;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(BtnWidth-4, BtnWidth-4);
lp.topMargin = 2+ny * BtnWidth;
lp.leftMargin = 2+nx * BtnWidth;
Button btn = new Button(this);
btn.setLayoutParams(lp);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(Color.BLACK);
btn.setTextSize(52);
btn.setText(label);
btn.setOnClickListener(BtnClick);
return btn;
}
private int nextBtnId = 0;
private View.OnClickListener BtnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v == startBtn){
ResetButtons();
}
else {
int btnId = Integer.valueOf( ((Button)v).getText().toString() );
if(btnId==31){
startBtn.setVisibility(View.VISIBLE);
}
else if(btnId==nextBtnId){
nextBtnId ++;
((Button)v).setVisibility(View.GONE);
}
}
}
};
private void ResetButtons(){
nextBtnId = 0;
timeCount = 0;
startBtn.setVisibility(View.GONE);
Random rnd = new Random();
for(int i=gv.getChildCount()-1; i>-1; --i){
Button btn = (Button)gv.getChildAt(i);
btn.setVisibility(View.VISIBLE);
}
for(int i=0; i<14; i++){
int index = rnd.nextInt(i+1);
RelativeLayout.LayoutParams first = (RelativeLayout.LayoutParams)((Button)gv.getChildAt(index)).getLayoutParams();
RelativeLayout.LayoutParams next = (RelativeLayout.LayoutParams)((Button)gv.getChildAt(i)).getLayoutParams();
((Button)gv.getChildAt(index)).setLayoutParams(next);
((Button)gv.getChildAt(i)).setLayoutParams(first);
}
for(int i=16; i<31; i++){
int index = rnd.nextInt(i-15)+16;
RelativeLayout.LayoutParams first = (RelativeLayout.LayoutParams)((Button)gv.getChildAt(index)).getLayoutParams();
RelativeLayout.LayoutParams next = (RelativeLayout.LayoutParams)((Button)gv.getChildAt(i)).getLayoutParams();
((Button)gv.getChildAt(index)).setLayoutParams(next);
((Button)gv.getChildAt(i)).setLayoutParams(first);
}
if(handler==null){
handler=new Handler();
}
checkTime();
}
Handler handler;
private int timeCount=0;
private void checkTime(){
handler.postDelayed(new Runnable(){
public void run(){
if(startBtn.getVisibility() == View.GONE){
timeCount++;
checkTime();
if(timeCount<60){
tvTime.setText("경과시간 : "+String.valueOf(timeCount)+" 초");
}else{
int m = timeCount/60;
int s = timeCount-(m*60);
tvTime.setText("경과시간 : "+String.valueOf(m)+"분 "+String.valueOf(s)+"초");
}
}
}
}, 1000);
}
}