基于Android的太阳方位角计算工具

这个是我今年参加谷歌安卓大赛的作品,结果连我的名字都没有,伤心啊。不过我已经决定在Android上狠下功夫的,这可是个好东西。

先看我的截屏吧。

还有个城市选择界面:

下面是代码了,除了计算公式是找来的外,其他全部都是原创,那可是我的十一八天长假的劳动成果啊。

package lineter.works.sunangle;

import java.util.Calendar;

public class city {

	private double weidu, jingdu;
	private double pianjiao, yangjiao;
	public boolean weidu_cuowu = false;
	public boolean jingdu_cuowu = false;

	public city(double weidu, double jingdu) {

		if (weidu <= 90.0 && weidu >= -90.0 ){

			this.weidu = weidu;
		}
		else{
			weidu_cuowu=true;
		}
		if(jingdu <= 180.0&& jingdu > -180.0) {
			this.jingdu = jingdu;
		}
		else{
			jingdu_cuowu=true;
		}
		if(!weidu_cuowu && !jingdu_cuowu){
			this.jisuan();
		} else {
			this.pianjiao = 0.0;
			this.yangjiao = 0.0;
		}
	}

	public String getpianjiao() {
		String temp1, temp2, temp3;
		temp1 = String.valueOf((int) (this.pianjiao));
		temp2 = String
				.valueOf(Math
						.abs((int) ((this.pianjiao - (double) (int) (this.pianjiao)) * 60)));
		if (Integer.parseInt(temp2) < 10)
			temp2 = "0" + temp2;
		temp3 = String
				.valueOf(Math
						.abs((int) (((this.pianjiao - (double) (int) (this.pianjiao)) * 60 - (double) (int) ((this.pianjiao - (double) (int) (this.pianjiao)) * 60)) * 60)));
		if (Integer.parseInt(temp3) < 10)
			temp3 = "0" + temp3;
		return String.valueOf(temp1 + "°" + temp2 + "'" + temp3 + "\"");
	}

	public String getyangjiao() {
		String temp1, temp2, temp3;
		temp1 = String.valueOf((int) (this.yangjiao));
		temp2 = String
				.valueOf(Math
						.abs((int) ((this.yangjiao - (double) (int) (this.yangjiao)) * 60)));
		if (Integer.parseInt(temp2) < 10)
			temp2 = "0" + temp2;
		temp3 = String
				.valueOf(Math
						.abs((int) (((this.yangjiao - (double) (int) (this.yangjiao)) * 60 - (double) (int) ((this.yangjiao - (double) (int) (this.yangjiao)) * 60)) * 60)));
		if (Integer.parseInt(temp3) < 10)
			temp3 = "0" + temp3;
		return String.valueOf(temp1 + "°" + temp2 + "'" + temp3 + "\"");
	}

	private void jisuan() {

		double temp1, temp2, temp3, temp4, temp5, temp6;
		double chiwei, shicha;
		Calendar d = Calendar.getInstance();

		temp1 = (double) d.get(Calendar.DAY_OF_YEAR);
		temp2 = 79.6764 + 0.2422 * ((double) d.get(Calendar.YEAR) - 1985)
				- (double) ((d.get(Calendar.YEAR) - 1985) / 4);
		temp3 = 2 * Math.PI * (temp1 - temp2) / (365.2422);
		chiwei = 0.3723 + 23.2567 * Math.sin(temp3) + 0.1149
				* Math.sin(2 * temp3) - 0.1712 * Math.sin(3 * temp3) - 0.758
				* Math.cos(temp3) + 0.3656 * Math.cos(2 * temp3) + 0.0201
				* Math.cos(3 * temp3);
		shicha = 0.0028 - 1.9857 * Math.sin(temp3) + 9.9059
				* Math.sin(2 * temp3) - 7.0924 * Math.cos(temp3) - 0.6882
				* Math.cos(2 * temp3);

		temp4 = (double) d.get(Calendar.HOUR_OF_DAY)
				+ ((double) d.get(Calendar.MINUTE)) / 60
				+ ((double) d.get(Calendar.SECOND)) / 3600;
		temp4 = temp4 + (this.jingdu - 120.0) * 4 / 60 + shicha / 60;
		temp5 = Math.sin(this.weidu * Math.PI / 180)
				* Math.sin(chiwei * Math.PI / 180)
				+ Math.cos(this.weidu * Math.PI / 180)
				* Math.cos(chiwei * Math.PI / 180)
				* Math.cos((temp4 - 12) * 15 * Math.PI / 180);
		yangjiao = Math.asin(temp5) * 180 / Math.PI;
		temp6 = (Math.sin(yangjiao * Math.PI / 180)
				* Math.sin(this.weidu * Math.PI / 180) - Math.sin(chiwei
				* Math.PI / 180))
				/ (Math.cos(yangjiao * Math.PI / 180) * Math.cos(this.weidu
						* Math.PI / 180));
		pianjiao = Math.acos(temp6) * 180 / Math.PI;
		if (temp4 < 12.0) {
			pianjiao = -pianjiao;
		}
	}

}


这个是主程序了,

sunangle.java:

package lineter.works.sunangle;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class sunangle extends Activity {

	public static final String ALL_DATE="POSITION";
	
	private EditText etweidu, etjingdu;
	private Button bcalc, bhelp, bquit;
	private TextView tvpianjiao, tvyangjiao;

	final Handler handler = new Handler();
	
    private List<String> list=new ArrayList<String>();
    private ArrayAdapter<String> adapter;
    private Spinner sp;
    private Boolean firstchoice=true;
    private int citychoic;
    
    private String[] city_name={"北京","天津","石家庄","保定","唐山","秦皇岛","张家口","承德","太原","大同","临汾","长治",	//12
    		"呼和浩特","包头","海拉尔","沈阳","大连","鞍山","锦州","长春","吉林","哈尔滨","齐齐哈尔","牡丹江","上海",	//13
    		"南京","无锡","苏州","徐州","杭州","宁波","温州","金华","合肥","芜湖","安庆","福州","厦门",	"泉州","南昌",	//15
    		"九江","赣州","济南","烟台","青岛","郑州","开封","洛阳","许昌","新乡","武汉","宜昌","沙市","长沙","衡阳",	//15
    		"湘潭","常德","广州","汕头","韶关","海口","南宁","桂林","柳州","悟州","成都","重庆","内江","泸州","万县",	//15
    		"贵阳","遵义","昆明","拉萨","日喀则","西安","宝鸡","延安","兰州","天水","酒泉","西宁","银川","乌鲁木齐",	//14
    		"哈密","喀什","和田","台北"};																			//4
    private double[] city_longitude={116.467,117.167,114.600,115.467,118.150,119.617,114.917,117.867,112.550,113.217,111.217,113.217,	//12
		   111.633,110.000,119.717,123.383,121.633,123.000,121.150,125.300,126.600,126.633,123.917,129.600,121.433,						//13
		   118.767,120.300,120.650,117.200,120.167,121.567,120.633,119.817,117.267,118.333,117.033,119.317,118.067,118.617,115.883,		//15
		   115.983,114.933,117.033,121.333,120.317,113.700,114.383,112.433,113.800,113.900,114.333,111.250,112.283,112.917,112.567,		//15
		   112.850,111.650,113.300,116.667,113.550,110.167,108.350,110.167,109.317,111.300,104.067,106.550,105.050,105.450,108.367,		//15
		   106.717,106.883,102.700,91.033,88.817,108.917,107.150,109.433,103.833,105.550,98.500,101.817,106.216,87.600,					//14
		   93.450,75.983,79.917,121.517};																								//4
    private double[] city_dimensionality={39.900,39.167,38.050,38.883,39.617,39.900,40.850,40.983,37.850,40.117,36.083,36.083,		//12
    		40.800,40.583,49.233,41.800,38.900,41.067,41.150,43.917,43.800,45.750,47.367,44.583,31.200,								//13
    		32.050,31.583,31.333,34.267,30.250,29.883,28.000,29.167,31.850,31.350,30.533,26.033,24.433,24.900,28.683,				//15
    		29.717,25.850,36.667,37.550,36.067,34.733,34.867,34.717,34.000,35.300,30.617,30.700,30.267,28.200,26.917,				//15
    		27.900,29.000,23.167,23.350,24.800,20.050,22.783,25.300,24.333,23.467,30.650,29.550,29.583,28.900,30.800,				//15
    		26.567,27.750,25.050,29.650,29.267,34.250,34.350,36.583,36.050,34.583,39.733,36.617,38.467,43.767,						//14
    		42.833,39.450,37.117,25.033};																							//4
    
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("太阳角度计算工具");
		setContentView(R.layout.main);

		etweidu = (EditText) findViewById(R.id.weidu);
		etjingdu = (EditText) findViewById(R.id.jingdu);
		bcalc = (Button) findViewById(R.id.bcalc);
		bcalc.setOnClickListener(new ClickListener());
		bhelp = (Button) findViewById(R.id.bhelp);
		bhelp.setOnClickListener(new ClickListener());
		bquit = (Button) findViewById(R.id.bquit);
		bquit.setOnClickListener(new ClickListener());
		tvpianjiao = (TextView) findViewById(R.id.pianjiao);
		tvyangjiao = (TextView) findViewById(R.id.yangjiao);

		handler.postDelayed(runnable, 1000);
		
		
		int tmp;
		for (tmp=0;tmp<city_name.length;tmp++){
			list.add(city_name[tmp].toString());
		}
		
		sp = (Spinner) findViewById(R.layout.myspinner);
		adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp.setAdapter(adapter);
		sp.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(firstchoice){
					firstchoice=false;
				}
				else{
					etjingdu.setText(String.valueOf( city_longitude[arg2]));
					etweidu.setText(String.valueOf( city_dimensionality[arg2]));
					citychoic=arg2;
				}
				arg0.setVisibility(View.VISIBLE);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		SharedPreferences position1=getSharedPreferences(ALL_DATE,0);
		String lastWeidu=position1.getString("LAST_WEIDU", "38.497");
		String lastJingdu=position1.getString("LAST_JINGDU", "106.133");
		citychoic=position1.getInt("CITYCHOIC", 0);
		etweidu.setText(lastWeidu.toString());
		etjingdu.setText(lastJingdu.toString());
		sp.setSelection(citychoic);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		SharedPreferences.Editor sharedata = getSharedPreferences(ALL_DATE, 0).edit(); 
		sharedata.putString("LAST_WEIDU",etweidu.getText().toString());  
		sharedata.putString("LAST_JINGDU",etjingdu.getText().toString()); 
		sharedata.putInt("CITYCHOIC", citychoic); 
		sharedata.commit();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		handler.removeCallbacks(runnable);
	}

	class ClickListener implements View.OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if (arg0 == bcalc) {
				city ct = new city(Double.parseDouble(etweidu.getText()
						.toString()), Double.parseDouble(etjingdu.getText()
						.toString()));
				tvpianjiao.setText(ct.getpianjiao());
				tvyangjiao.setText(ct.getyangjiao());
			} else if (arg0 == bhelp) {
				new AlertDialog.Builder(sunangle.this)
						.setTitle("使用帮助")
						.setMessage(
								"请选择城市或输入当地经纬度计算当前时刻太阳角度。\n"
										+ "说明:\n  “偏角”以正南方向为0度,东负西正。\n"
										+ "  “仰角”以地平面为0度,上正下负。".toString()).show();
			} else if (arg0 == bquit) {
				finish();
			}
		}
	}

	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				double tmp1, tmp2;
				if (etweidu.getText().toString() != ""
						&& etjingdu.getText().toString() != "") {
					tmp1 = Double.parseDouble(etweidu.getText().toString());
					tmp2 = Double.parseDouble(etjingdu.getText().toString());
					
					city ct = new city(tmp1, tmp2);
					tvpianjiao.setText(ct.getpianjiao());
					tvyangjiao.setText(ct.getyangjiao());
					if (ct.weidu_cuowu) {
						tvpianjiao.setText("维度输入错误");
					}
					if(ct.jingdu_cuowu){
						tvyangjiao.setText("经度输入错误");
					}

					handler.postDelayed(this, 1000);
				}
			} catch (Exception e) {
				handler.postDelayed(runnable, 1000);
			}
		}
	};
}

看看代码就知道我是怎么费了九牛二虎之力才做出来的了吧,唉,重在参与吧。


不好意思,少上传个布局文件。

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:background="@drawable/taiyang" 
	>
	<TableLayout android:layout_width="fill_parent"
		android:stretchColumns="0,1"
		android:layout_height="fill_parent"
		android:padding="10dp">
		<TableRow>
			<TextView
				android:text="请选择城市"
				android:textSize="18dp" 
				/>
			<Spinner 
				android:id="@+layout/myspinner" 
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				/>
		</TableRow>
		<TableRow>
			<TextView
				android:text="当地维度(°)"
				android:textSize="18dp" 
				/>
			<EditText 
				android:id="@+id/weidu"
				android:text="38.16" 
				android:numeric="decimal"
				/>
		</TableRow>
		<TableRow>
			<TextView 
				android:text="当地经度(°)"
				android:textSize="18dp" 
				/>
			<EditText 
				android:id="@+id/jingdu"
				android:text="106.1" 
				android:numeric="decimal"
				/>
		</TableRow>
		<TableRow>
			<TextView
				android:text="太阳偏角"
				android:textSize="18dp"
				/>
			<TextView
				android:id="@+id/pianjiao"
				android:text="0.0"
				android:textSize="18dp"
				android:textColor="#4754f8"
				/>
		</TableRow>
		<TableRow>
			<TextView
				android:text="太阳仰角"
				android:textSize="18dp"
				/>
			<TextView
				android:id="@+id/yangjiao"
				android:text="0.0"
				android:textSize="18dp"
				android:textColor="#4754f8"
				/>
		</TableRow>
		<TableRow>
			<TextView
				android:text="当前时间"
				android:textSize="22dp"
				/>
			<DigitalClock 
				android:text="DigitalClock" 
				android:id="@+id/digitalClock1" 
				android:layout_width="wrap_content" 
				android:layout_height="wrap_content"
				android:textSize="22dp"
				android:textColor="#0ff"
				/>
		</TableRow>
		<TableRow>
			<Button
				android:id="@+id/bhelp"
			 	android:text="帮助"
			 	android:layout_width="wrap_content" 
			 	/>
			<Button
				android:id="@+id/bquit"
			 	android:text="退出系统"
			 	android:layout_width="wrap_content" 
			 	/>
		</TableRow>
		<Button 
			android:id="@+id/bcalc"
			android:text="计算角度"
			android:layout_width="wrap_content" 
			android:visibility="invisible"
			/>
	</TableLayout>
</LinearLayout>

字符串文件

string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, sunangle!</string>
    <string name="app_name">sunangle</string>
    <string name="helpmsg">使用帮助。</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="lineter.works.sunangle" android:versionCode="1"
	android:versionName="1.0">
	<uses-sdk android:minSdkVersion="4" />

	<application android:icon="@drawable/sunangle"
		android:label="@string/app_name" android:launchMode="singleInstance">
		<activity android:name=".sunangle" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>

	</application>
</manifest>
其实也没什么权限,它也不需要什么权限,只是有个单例运行设置,在第八行,注意下就行了。




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值