20172305 暑假作业 之 TimeCalculate & Save Iron Man
TimeCalculate
项目介绍
- 项目名称:
- TimeCalculate
- 项目简介:
- 本项目基于Java语言和Anroid Studio软件,便于计算与年龄相关的内容,解决有些人群在计算他人年龄或是生肖、星座等问题的尴尬。
- 使用说明:
- 本款APP通过输入人的年、月、日或是年龄来计算相关的出生日期、生肖、星座、以及生辰八字等相关年龄的问题。
需求分析
本款产品是基于特定人群的开发,当遇到有关年龄的相关计算,存在粗心算错或是或是忽略了时间计算的特殊而导致的尴尬的问题。譬如,在问过对方的生肖之后就应该会判断出他的年龄,而如果多算了一轮生肖恐怕气氛会有些尴尬。所以,本款产品就是为此而设计的。
产品图标
相关过程分析
本项目分为Java代码部分和Android 界面部分
Java代码部分:该部分编写了相关年龄的计算,有利用年、月、日算出的出生日期、生肖、星座、生辰八字和年龄算出的出生年份。
- 计算出生日期(阴历)和生肖
public class Bazi {
private int year;
private int month;
private int day;
private boolean leap;
Date baseDate = null;
final static String chineseNumber[] = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊"};
public final static String[] Gan = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
public final static String[] Zhi = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy-MM-dd");
final static long[] lunarInfo = new long[]{0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0
};
public String getMonth() {
return chineseNumber[month- 1] ;
}
public String getYear() {
return getYearStr(year);
}
public int getnumberYear() {
return year;
}
public int getnumbermonth() {
return month;
}
public int getYearindex() {
return (year - 4) % 12;
}
public String getDay() {
return getChinaDayString(day);
}
public static final String[] jiazhi = {
"甲子", "乙丑", "丙寅", "丁卯", "戊辰", "己巳", "庚午", "辛未", "壬申", "癸酉",
"甲戌", "乙亥", "丙子", "丁丑", "戊寅", "己卯", "庚辰", "辛巳", "壬午", "癸未",
"甲申", "乙酉", "丙戌", "丁亥", "戊子", "己丑", "庚寅", "辛卯", "壬辰", "癸巳",
"甲午", "乙未", "丙申", "丁酉", "戊戌", "己亥", "庚子", "辛丑", "壬寅", "癸卯",
"甲辰", "乙巳", "丙午", "丁未", "戊申", "己酉", "庚戌", "辛亥", "壬子", "癸丑",
"甲寅", "乙卯", "丙辰", "丁巳", "戊午", "己未", "庚申", "辛酉", "壬戌", "癸亥"
};
private Calendar cal;
public String getYearGanZhi(int hour) {
int idx = (year - 1864) % 60;
String y = jiazhi[idx];
String m="";
String d="";
String h="";
idx = idx % 5;
int idxm=0;
idxm=(idx+1)*2;
if(idxm==10) idxm=0;
m=Gan[(idxm+month-1)%10]+Zhi[(month+2-1)%12];
int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);
offset=(offset+40)%60;
d=jiazhi[offset];
offset=(offset % 5 )*2;
h=Gan[(offset+hour)%10]+Zhi[hour];
return y+"\n"+m+"\n"+d+"\n"+h;
}
public String getShichenFromDay(int offset){
return null;
}
//====== 传回农历 y年的总天数
final private static int yearDays(int y) {
int i, sum = 348;
for (i = 0x8000; i > 0x8; i >>= 1) {
if ((lunarInfo[y - 1900] & i) != 0) {
sum += 1;
}
}
return (sum + leapDays(y));
}
//====== 传回农历 y年闰月的天数
final private static int leapDays(int y) {
if (leapMonth(y) != 0) {
if ((lunarInfo[y - 1900] & 0x10000) != 0) {
return 30;
} else {
return 29;
}
} else {
return 0;
}
}
//====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
final private static int leapMonth(int y) {
return (int) (lunarInfo[y - 1900] & 0xf);
}
//====== 传回农历 y年m月的总天数
final private static int monthDays(int y, int m) {
if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0) {
return 29;
} else {
return 30;
}
}
/***
* @return 传回农历 y年的生肖
*/
final public String animalsYear() {
final String[] Animals = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
return Animals[(year - 4) % 12];
}
//====== 传入 月日的offset 传回干支, 0=甲子
final private static String cyclicalm(int num) {
return (Gan[num % 10] + Zhi[num % 12]);
}
//====== 传入 offset 传回干支, 0=甲子
final public String cyclical() {
int num = year - 1900 + 36;
return (cyclicalm(num));
}
/**
* 传出y年m月d日对应的农历.
* yearCyl3:农历年与1864的相差数 ?
* monCyl4:从1900年1月31日以来,闰月数
* dayCyl5:与1900年1月31日相差的天数,再加40 ?
*
* @param cal
* @return
*/
public Bazi(Calendar cal) {
this.cal=cal;
int yearCyl, monCyl, dayCyl;
int leapMonth = 0;
try {
baseDate = chineseDateFormat.parse("1900-1-31");
} catch (ParseException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
//求出和1900年1月31日相差的天数
int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);
dayCyl = offset + 40;
monCyl = 14;
//用offset减去每农历年的天数
// 计算当天是农历第几天
//i最终结果是农历的年份
//offset是当年的第几天
int iYear, daysOfYear = 0;
for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) {
daysOfYear = yearDays(iYear);
offset -= daysOfYear;
monCyl += 12;
}
if (offset < 0) {
offset += daysOfYear;
iYear--;
monCyl -= 12;
}
//农历年份
year = iYear;
yearCyl = iYear - 1864;
leapMonth = leapMonth(iYear); //闰哪个月,1-12
leap = false;
//用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
int iMonth, daysOfMonth = 0;
for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
//闰月
if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {
--iMonth;
leap = true;
daysOfMonth = leapDays(year);
} else {
daysOfMonth = monthDays(year, iMonth);
}
offset -= daysOfMonth;
//解除闰月
if (leap && iMonth == (leapMonth + 1)) {
leap = false;
}
if (!leap) {
monCyl++;
}
}
//offset为0时,并且刚才计算的月份是闰月,要校正
if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
if (leap) {
leap = false;
} else {
leap = true;
--iMonth;
--monCyl;
}
}
//offset小于0时,也要校正
if (offset < 0) {
offset += daysOfMonth;
--iMonth;
--monCyl;
}
month = iMonth;
day = offset + 1;
}
public static String getChinaDayString(int day) {
String chineseTen[] = {"初", "十", "廿", "卅"};
int n = day % 10 == 0 ? 9 : day % 10 - 1;
if (day > 30) {
return "";
}
if (day == 10) {
return "初十";
} else {
return chineseTen[day / 10] + chineseNumber[n];
}
}
@Override
public String toString() {
return getYearStr(year) + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] + "月" + getChinaDayString(day);
}
public String getYearStr(int year) {
String[] chineseword = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
String ys = "";
int index = year / 1000;
ys += chineseword[index];
year = year % 1000;
index = year / 100;
ys += chineseword[index];
year = year % 100;
index = year / 10;
ys += chineseword[index];
year = year % 10;
index = year;
ys += chineseword[index];
return ys;
}
public static String getSixtyDay() {
String temp = "";
for (int i = 0; i < 60; i++) {
temp += ",/" + cyclicalm(i) + "/";
}
return temp;
}
}
- 计算星座
public class Age {
public String years(int num1, int num2, int num3){
LocalDate dateA = LocalDate.of(num1, num2, num3);
LocalDate dateB = LocalDate.now();
Period period = Period.between(dateA,dateB);
return "" + period.getYears();
}
public String constellation(int num1, int num2, int num3){
LocalDate date = LocalDate.of(num1, num2, num3);
int day, months = 0;
//平年
if(leap(date.getYear())){
switch(date.getMonthValue()){
case 1:
months = 0;
break;
case 2:
months = 31;
break;
case 3:
months = 60;
break;
case 4:
months = 91;
break;
case 5:
months = 121;
break;
case 6:
months = 152;
break;
case 7:
months = 182;
break;
case 8:
months = 213;
break;
case 9:
months = 244;
break;
case 10:
months = 274;
break;
case 11:
months = 305;
break;
case 12:
months = 335;
break;
}
}
else{
switch(date.getMonthValue()){
case 1:
months = 0;
break;
case 2:
months = 31;
break;
case 3:
months = 59;
break;
case 4:
months = 90;
break;
case 5:
months = 120;
break;
case 6:
months = 151;
break;
case 7:
months = 181;
break;
case 8:
months = 212;
break;
case 9:
months = 243;
break;
case 10:
months = 273;
break;
case 11:
months = 304;
break;
case 12:
months = 334;
break;
}
}
day = months + date.getDayOfMonth();
if(leap(date.getYear())){
if((day >= 20) && (day <= 49))
return "星座: Aquarius \n水瓶座 \n01月20日--02月18日 \n构成元素:空气 \n颜色:黑\n 泼墨吹烛,\n我自赏遍红尘蒹葭, \n点笔寒桠,\n为你温纸入画。";
else if((day >= 50) && (day <= 80))
return "星座: Pisces \n双鱼座 \n02月19日--03月20日 \n构成元素:\n水 \n颜色:蓝\n浮生幻化,\n犹记当年月下,\n红线千匝眉笔朱砂,\n如今,别后她嫁。";
else if((day >= 81) && (day <= 111))
return "星座: Aries \n白羊座 \n03月21日--04月20日 \n构成元素:火 \n颜色:红\n我且逍遥山水人家,\n 闲话桑麻。\n听一曲琵琶,\n曲终后,再无牵挂。";
else if((day >= 112) && (day <= 141))
return "星座: Taurus \n金牛座 \n04月21日--05月20日 \n构成元素:土 \n颜色:绿\n花前饮茶,\n镜前遗留的是谁的白发?\n梦里却再无她,\n入土为安也罢,\n弹指不过一霎。";
else if((day >= 142) && (day <= 173))
return "星座: Gemini \n双子座 \n05月21日--06月21日 \n构成元素:空气 \n颜色:黄\n金堂玉马,玉人风华,\n不于我浪迹天涯,\n遍看朝夕烟霞。";
else if((day >= 174) && (day <= 204))
return "星座: Cancer \n巨蟹座 \n06月22日--07月22日 \n构成元素:水 \n颜色:白\n江山如画,山河戎马。\n我逐鹿天下,\n守你盛世容华。";
else if((day >= 205) && (day <= 235))
return "星座: Leo \n狮子座 \n07月23日--08月22日 \n构成元素:火 \n颜色:橙\n策马飞沙,风云叱咤。\n纵这江山崩塌,寄人篱下,\n也护你一步一莲华。";
else if((day >= 236) && (day <= 266))
return "星座: Virgo \n处女座 \n08月23日--09月22日 \n构成元素:土 \n颜色:灰\n我拱手天下,许你两袖烟花,\n那,\n若我化土为沙,\n你可会为我祭酒扫茶?";
else if((day >= 267) && (day <= 296))
return "星座: Libra \n天秤座 \n09月23日--10月22日 \n构成元素:空气 \n颜色:淡红\n雪落窗纱,佳人韶华。\n谁能覆我秒音高寡,叶桥佛刹?";
else if((day >= 297) && (day <= 326))
return "星座: Scorpio \n天蝎座 \n10月23日--11月21日 \n构成元素:水 \n颜色:深红\n明月无暇,城池暗哑。\n你说君临天下,\n维我一方繁华,\n再见后却是风流飒沓,生死无话。";
else if((day >= 327) && (day <= 356))
return "星座: Sagittarius \n射手座 \n11月22日--12月21日 \n构成元素:火 \n颜色:紫红\n当时醉卧桃花,\n见你琴瑟饮茶,\n梦我一世相思入画,\n明眸刹那。";
else
return "星座: Capricom \n摩羯座 \n12月22日--01月19日 \n构成元素:土 \n颜色:黑\n草庐为家,我愿半生换作浮华,\n一缕炊烟披霞,一件清酒词话。";
}
else{
if((day >= 20) && (day <= 49))
return "星座: Aquarius \n水瓶座 \n01月20日--02月18日 \n构成元素:空气 \n颜色:黑\n 泼墨吹烛,\n我自赏遍红尘蒹葭, \n点笔寒桠,\n为你温纸入画。";
else if((day >= 50) && (day <= 79))
return "星座: Pisces \n双鱼座 \n02月19日--03月20日 \n构成元素:\n水 \n颜色:蓝\n浮生幻化,\n犹记当年月下,\n红线千匝眉笔朱砂,\n如今,别后她嫁。";
else if((day >= 80) && (day <= 110))
return "星座: Aries \n白羊座 \n03月21日--04月20日 \n构成元素:火 \n颜色:红\n我且逍遥山水人家,\n 闲话桑麻。\n听一曲琵琶,\n曲终后,再无牵挂。";
else if((day >= 111) && (day <= 140))
return "星座: Taurus 金牛座 \n04月21日--05月20日 \n构成元素:土 \n颜色:绿\n花前饮茶,镜前遗留的是谁的白发?梦里却再无她,入土为安也罢,弹指不过一霎。";
else if((day >= 141) && (day <= 172))
return "星座: Gemini \n双子座 \n05月21日--06月21日 \n构成元素:空气 \n颜色:黄\n金堂玉马,玉人风华,\n不于我浪迹天涯,\n遍看朝夕烟霞。";
else if((day >= 173) && (day <= 203))
return "星座: Cancer \n巨蟹座 \n06月22日--07月22日 \n构成元素:水 \n颜色:白\n江山如画,山河戎马。\n我逐鹿天下,\n守你盛世容华。";
else if((day >= 204) && (day <= 234))
return "星座: Leo \n狮子座 \n07月23日--08月22日 \n构成元素:火 \n颜色:橙\n策马飞沙,风云叱咤。\n纵这江山崩塌,寄人篱下,\n也护你一步一莲华。";
else if((day >= 235) && (day <= 265))
return "星座: Virgo \n处女座 \n08月23日--09月22日 \n构成元素:土 \n颜色:灰\n我拱手天下,许你两袖烟花,\n那,\n若我化土为沙,\n你可会为我祭酒扫茶?";
else if((day >= 266) && (day <= 295))
return "星座: Libra \n天秤座 \n09月23日--10月22日 \n构成元素:空气 \n颜色:淡红\n雪落窗纱,佳人韶华。\n谁能覆我秒音高寡,叶桥佛刹?";
else if((day >= 296) && (day <= 325))
return "星座: Scorpio \n天蝎座 \n10月23日--11月21日 \n构成元素:水 \n颜色:深红\n明月无暇,城池暗哑。\n你说君临天下,\n维我一方繁华,\n再见后却是风流飒沓,\n生死无话。";
else if((day >= 326) && (day <= 355))
return "星座: Sagittarius \n射手座 \n11月22日--12月21日 \n构成元素:火 \n颜色:紫红\n当时醉卧桃花,\n见你琴瑟饮茶,\n梦我一世相思入画,\n明眸刹那。";
else
return "星座: Capricom \n摩羯座 \n12月22日--01月19日 \n构成元素:土 \n颜色:黑\n草庐为家,我愿半生换作浮华,\n一缕炊烟披霞,一件清酒词话。";
}
}
private boolean leap(int years){
if(((years%100 != 0) && (years%4 == 0)) || (years%400 == 0)) {
return true;
}
else {
return false;
}
}
}
- **Android 界面部分**:该部分共分为5个界面,有登录界面、选择界面、三个填写信息的界面
- 登陆界面 MainActivity, 界面的跳转和弹出对话框两个部分
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast toast = Toast.makeText(this,"触屏进入",Toast.LENGTH_SHORT);
toast.show();
ImageView imageview = (ImageView)findViewById(R.id.imageview);
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setTitle("程序提示")
.setMessage("计算有风险,仅供参考与娱乐。")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}).setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
}).show();
}
});
}
}
- 选择界面 SecondActivity,用来确定是填写“年月日”来计算,还是填写“今年年龄”来计算。利用弹出的对话框来进行选择,不同的选择将会开启不同的活动界面。与登陆界面类似,同样都是利用跳转和对话框两部分。
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
MediaPlayer player = MediaPlayer.create(this,R.raw.sevenyears);
player.start();
Toast toast = Toast.makeText(this, "欢迎使用时间计算器,\n 请点击右下方按钮。", LENGTH_LONG);
toast.show();
// 进行选择是已知年龄还是出生日期
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new myButtonListener());
}
public class myButtonListener implements View.OnClickListener{
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
new AlertDialog.Builder(SecondActivity.this).setTitle("程序提示")
.setMessage("根据需要进行选择")
.setPositiveButton("已知出生日期", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent1 = new Intent(SecondActivity.this, ThridActivity.class);
startActivity(intent1);
}
}).setNegativeButton("已知现在年龄", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent2 = new Intent(SecondActivity.this, ForthActivity.class);
startActivity(intent2);
}
}).show();
}
}
}
- 填写信息界面 ThridActivity ForthActivity FifthActivity,三个界面利用到之前编写的Java代码,将根据用户填写的信息,进行确定相关信息宾显示在界面上。其中ThridActivity是根据“年月日”来确定相关内容的,显示出的内容有用户的出生日期(阴历)和生肖以及今年是否过生日了;ForthActivity是根据“今年年龄”来确定相关内容,比如他的出生年份,因为只知道他的岁数,所以其他的算不了;FifthActivity是在此基础上再用一个出生时间来计算用户的生辰八字(此部分是用网上的算法,所以准确性有待真正的会算的人检验)
public class ThridActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thrid);
ImageButton button1 = (ImageButton)findViewById(R.id.imageButton);
button1.setOnClickListener(new myButtonListener1());
Button button2 = (Button)findViewById(R.id.button);
button2.setOnClickListener(new myButtonListener2());
}
public class myButtonListener1 implements View.OnClickListener{
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
EditText editText1 =(EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText)findViewById(R.id.editText2);
EditText editText3 = (EditText)findViewById(R.id.editText3);
int nian = Integer.parseInt(editText1.getText().toString());
int yue = Integer.parseInt(editText2.getText().toString());
int ri = Integer.parseInt(editText3.getText().toString());
Age age = new Age();
String nianling = age.years(nian,yue,ri);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String baizi = nian + "-" + yue + "-" + ri;
try {
cal.setTime(sdf.parse(baizi));
} catch (ParseException e) {
e.printStackTrace();
}
Bazi lunar = new Bazi(cal);
String yinli = lunar.toString();
//判断是否今年生日已经过去
LocalDate localdate1 = LocalDate.of(LocalDate.now().getYear(),yue,ri);
LocalDate localdate2 = LocalDate.now();
Period period = Period.between(localdate1,localdate2);
if(localdate1.isAfter(localdate2)) {
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("年龄:" + nianling + "岁\n阴历日期:" + yinli +"\n今年生日未过,距离过生日还有" + period.getMonths() + "月" + period.getDays() + "天");
} else{
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("年龄:" + nianling + "岁\n阴历日期:" + yinli + "\n今年生日已过" + period.getMonths() + "月" + period.getDays() + "天");
Toast toast = Toast.makeText(ThridActivity.this, "想算你的星座和生辰八字么?\n 点击右上方的按钮,快来吧!", LENGTH_LONG);
toast.show();
}
}
}
public class myButtonListener2 implements View.OnClickListener{
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v){
Intent intent = new Intent(ThridActivity.this,FifthActivity.class);
startActivity(intent);
}
}
}
public class ForthActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forth);
ImageButton imagebutton1 = (ImageButton)findViewById(R.id.imageButton1);
imagebutton1.setOnClickListener(new myButtonListener1());
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new myButtonListener2());
}
public class myButtonListener1 implements View.OnClickListener{
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v){
EditText editText = (EditText)findViewById(R.id.editText);
LocalDate csnian = LocalDate.now().minusYears(Integer.valueOf(editText.getText().toString()));
EditText editText4 = (EditText)findViewById(R.id.editText4);
editText4.setText(String.valueOf(csnian.getYear()));
Toast toast = Toast.makeText(ForthActivity.this, "想算你的星座和生辰八字么?\n 点击右上方的按钮,快来吧!", LENGTH_LONG);
toast.show();
}
}
public class myButtonListener2 implements View.OnClickListener{
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v){
Intent intent = new Intent(ForthActivity.this,FifthActivity.class);
startActivity(intent);
}
}
}
public class FifthActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
Toast toast = Toast.makeText(this,"点击奇异博士,让他给你算算!",Toast.LENGTH_LONG);
toast.show();
ImageButton imagebutton = (ImageButton)findViewById(R.id.imageButton2);
imagebutton.setOnClickListener(new myButtonListener());
}
public class myButtonListener implements View.OnClickListener{
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
final ImageView imageView = (ImageView)findViewById(R.id.imageView4);
imageView.setVisibility(View.VISIBLE);
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
imageView.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
imageView.startAnimation(animation);
EditText editText5 =(EditText) findViewById(R.id.editText5);
EditText editText6 = (EditText)findViewById(R.id.editText6);
EditText editText7 = (EditText)findViewById(R.id.editText7);
EditText editText8 = (EditText)findViewById(R.id.editText8);
int nian = Integer.parseInt(editText5.getText().toString());
int yue = Integer.parseInt(editText6.getText().toString());
int ri = Integer.parseInt(editText7.getText().toString());
int tian = Integer.parseInt(editText8.getText().toString());
ShiChen shichen = new ShiChen();
int hours = shichen.shichen(tian);
//星座
Age age = new Age();
String xingzuo = age.constellation(nian,yue,ri);
//生辰八字
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String baizi = nian + "-" + yue + "-" + ri;
try {
cal.setTime(sdf.parse(baizi));
} catch (ParseException e) {
e.printStackTrace();
}
Bazi lunar = new Bazi(cal);
String bazi = lunar.getYearGanZhi(hours);
TextView textView2 = (TextView)findViewById(R.id.textView2);
TextView textView3 = (TextView)findViewById(R.id.textView3);
textView2.setText(xingzuo);
textView3.setText(bazi);
}
}
}
---
## Save Iron Man
### 项目介绍
- 项目名称:
- Save Iron Man
- 项目简介:
- 本项目基于Java语言和Anroid Studio软件,一款复仇者联盟版的打地鼠游戏。
- 使用说明:
- 本款APP通过建立相关故事情节,模拟打地鼠游戏机制,将复仇者联盟与打地鼠游戏相关结合,创造出一款“Save Iron Man”的单机游戏。
### 需求分析
- 本款产品是一款基于打地鼠开发出的致敬钢铁侠经典桥段的小游戏,由于开发者是一个钢铁侠迷,所以整个打地鼠游戏全部是钢铁侠风格。本款游戏从复仇者联盟1和复仇者联盟3的经典片段中进行截取,创意设想将钢铁侠融入得到打地鼠角色当中,为钢铁侠迷提供一款怀念的小游戏。
- 产品图标
- ![](https://img2018.cnblogs.com/blog/1333004/201812/1333004-20181223150309734-673880840.png)
### 相关过程分析
- 本项目共5个活动界面,登陆界面、情节界面、游戏界面、情节界面、付费界面
- 登陆界面 MainActivity,该界面放了一张我PS过的图片,用来提高游戏感觉,用到的是跳转和对话框两部分。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast toast = Toast.makeText(MainActivity.this,"触屏开始",Toast.LENGTH_SHORT);
toast.show();
ImageView imageview1 = (ImageView)findViewById(R.id.imageView1);
imageview1.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setTitle("程序提示")
.setMessage("本产品为休闲类游戏,仅供娱乐。")
.setPositiveButton("开始", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}).setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
}).show();
}
});
}
}
- 情节界面 SecondActivity ForthActivity 这两个部分一个是玩游戏之前的故事情节和玩游戏之后的故事情节,两部分用到了帧动画的形式进行一帧一帧的播放动画。此外,在点击结束动画弹出的对话框中将退出和不退出都将会跳转到付费界面进行付费。
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
final AnimationDrawable anim = (AnimationDrawable)ll.getBackground();
anim.setOneShot(true);
anim.start();
ll.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
}
});
}
}
public class ForthActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forth);
LinearLayout l = (LinearLayout)findViewById(R.id.ff);
final AnimationDrawable animtion = (AnimationDrawable)l.getBackground();
animtion.setOneShot(true);
animtion.start();
l.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(ForthActivity.this).setTitle("程序提示")
.setMessage("退出程序")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(ForthActivity.this,FifthActivity.class);
startActivity(intent);
}
}).setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(ForthActivity.this,FifthActivity.class);
startActivity(intent);
}
}).show();
}
});
}
}
- 游戏界面 ThirdActivity,这部分是最为主要的部分,我通过其他程序确定了图片上每个洞口的位置,并存入一个数组内进行随机出现,这就可以随机产生“出洞”的效果,我们还添加了一个背景音乐的,和提示动画,此外我们在内部机制中进行预判当打到20只的时候就会自动跳转到结束界面,无需用户手动跳转就可实现。但是,在跳转的过程中一直 不是很流畅,所以显示的很僵硬的感觉。
public class ThirdActivity extends AppCompatActivity {
private int i = 0;
private ImageView gangtiexia;
private Handler handler;
public int[][] position = new int[][]{{963,365},{1308,337},{1544,413},{1009,445},{1456,541},{971,621},{793,518},{445,474}};
private boolean isPause = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
final ImageView imageView = (ImageView)findViewById(R.id.imageView4);
imageView.setVisibility(View.VISIBLE);
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(5000);
animation.setFillAfter(false);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
imageView.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
imageView.startAnimation(animation);
gangtiexia = (ImageView)findViewById(R.id.imageView3);
gangtiexia.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event){
v.setVisibility(View.INVISIBLE);
i++;
Toast toast=Toast.makeText(ThirdActivity.this,"捕捉到" + i + "只钢铁侠!", Toast.LENGTH_LONG);
showMyToast(toast,10*10);
if (i == 20){
Intent intent = new Intent(ThirdActivity.this,ForthActivity.class);
startActivity(intent);
}
return false;
}
});
handler = new Handler(){
@Override
public void handleMessage(Message msg){
int index = 0;
if(msg.what == 0x101){
index = msg.arg1;
gangtiexia.setX(position[index][0]);
gangtiexia.setY(position[index][1]);
gangtiexia.setVisibility(View.VISIBLE);
}
super.handleMessage(msg);
}
};
Thread t = new Thread(new Runnable() {
@Override
public void run() {
int index = 0;
while (!Thread.currentThread().isInterrupted()){
index = new Random().nextInt(position.length);
Message m = handler.obtainMessage();
m.arg1 = index;
m.what = 0x101;
handler.sendMessage(m);
try{
Thread.sleep(new Random().nextInt(500) + 500);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
final MediaPlayer player = MediaPlayer.create(this,R.raw.something);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(player.isPlaying()&&!isPause){
player.pause();
isPause = true;
((Button)v).setText("背景音乐播放");
}else{
player.start();
((Button)v).setText("背景音乐暂停");
isPause = false;
}
}
});
}
public void showMyToast(final Toast toast, final int cnt) {
final Timer timer =new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toast.show();
}
},0,3000);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
toast.cancel();
timer.cancel();
}
}, cnt );
}
protected void onResume() {
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
super.onResume();
}
}
- 付费界面 FifthActivity,该界面只是添加了一张二维码的图片供玩家进行付费支持的。
## 总结
- 学习到的相关内容:
- 帧动画 属性动画 时间类的学习 对话框 音乐 按钮和背景图 PS图片
- 在尝试做暑假作业的时候,我并没有准备做游戏的APP,但是在做完时间计时器之后发现做的有些过于LOW,所以尝试这做了一款游戏。其实这款游戏的制作过程,不仅是在完成作业,更是满足我作为一个钢铁侠迷的热爱和致敬。两款APP的界面设计图片都是我用PS进行修改或是再加工的,在学习Android开发的同时,我也学习了一定的PS的知识。在整个编写的过程,做一款游戏的难度要远远高于应用程序的难度,尤其是调试的阶段,快要炸了也找不到错误所在。总体来说,满是心酸泪。。。
-因版权所限,未附相关APP程序图片,想体验的同学可以找我。