20172305 暑假做業 之 TimeCalculate & Save Iron Man

20172305 暑假做業 之 TimeCalculate & Save Iron Man

TimeCalculate

項目介紹

  • 項目名稱:
    • TimeCalculate
  • 項目簡介:
    • 本項目基於Java語言和Anroid Studio軟件,便於計算與年齡相關的內容,解決有些人羣在計算他人年齡或是生肖、星座等問題的尷尬。
  • 使用說明:
    • 本款APP經過輸入人的年、月、日或是年齡來計算相關的出生日期、生肖、星座、以及生辰八字等相關年齡的問題。

需求分析

  • 本款產品是基於特定人羣的開發,當遇到有關年齡的相關計算,存在粗心算錯或是或是忽略了時間計算的特殊而致使的尷尬的問題。譬如,在問過對方的生肖以後就應該會判斷出他的年齡,而若是多算了一輪生肖恐怕氣氛會有些尷尬。因此,本款產品就是爲此而設計的。git

  • 產品圖標

相關過程分析

  • 本項目分爲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個界面,有登陸界面、選擇界面、三個填寫信息的界面api

  • 登錄界面 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,用來肯定是填寫「年月日」來計算,仍是填寫「今年年齡」來計算。利用彈出的對話框來進行選擇,不一樣的選擇將會開啓不一樣的活動界面。與登錄界面相似,一樣都是利用跳轉和對話框兩部分。dom

    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是在此基礎上再用一個出生時間來計算用戶的生辰八字(此部分是用網上的算法,因此準確性有待真正的會算的人檢驗)ide

    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的經典片斷中進行截取,創意設想將鋼鐵俠融入獲得打地鼠角色當中,爲鋼鐵俠迷提供一款懷念的小遊戲。學習

  • 產品圖標

相關過程分析

  • 本項目共5個活動界面,登錄界面、情節界面、遊戲界面、情節界面、付費界面動畫

  • 登錄界面 MainActivity,該界面放了一張我PS過的圖片,用來提升遊戲感受,用到的是跳轉和對話框兩部分。ui

    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 這兩個部分一個是玩遊戲以前的故事情節和玩遊戲以後的故事情節,兩部分用到了幀動畫的形式進行一幀一幀的播放動畫。此外,在點擊結束動畫彈出的對話框中將退出和不退出都將會跳轉到付費界面進行付費。this

    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程序圖片,想體驗的同窗能夠找我。

相關文章
相關標籤/搜索