月齢計算アプリ、とりあえず完成
月齢表示のAndroidアプリの改良 - Throw or Die and ChouChou
前回に画像表示機能を追加。(だいぶ粗いが。)
※2010/7/5 計算式修正
package jp.throwordie.moonphase; import java.util.Date; import java.util.GregorianCalendar; import jp.throwordie.moonphase.R; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dispPhase(); Button button = (Button)findViewById(R.id.Button01); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { dispPhase(); } }); } private void dispPhase(){ TextView textView = (TextView)findViewById(R.id.TextView01); Date date = new GregorianCalendar().getTime(); int year = date.getYear() + 1900; int month = date.getMonth() + 1; int day = date.getDate(); double phase = (((year - 2009) % 19) * 11 + month + day) % 30; if((month == 1) || (month == 2)) { phase += 2; } textView.setText("DATE: " + year + "/" + month + "/" + day + "\nPHASE: " + phase + "days"); View view = (View)findViewById(R.id.SurfaceView01); int id = 0; if(phase < 3) { id = R.drawable.moon3; } else if(phase <= 5) { id = R.drawable.moon5; } else if(phase <= 7) { id = R.drawable.moon7; } else if(phase <= 10) { id = R.drawable.moon10; } else if(phase <= 15) { id = R.drawable.moon15; } else if(phase <= 18) { id = R.drawable.moon18; } else if(phase <= 21) { id = R.drawable.moon21; } else if(phase <= 23) { id = R.drawable.moon23; } else if(phase <= 25) { id = R.drawable.moon25; } else { id = R.drawable.moon30; } Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), id); BitmapDrawable bd = new BitmapDrawable(this.getResources(), bitmap); view.setBackgroundDrawable(bd); } }