#define PI 3.1415926
double LantitudeLongitudeDist(double lon1,double lat1,
double lon2,double lat2)
{
double er = 6378137; // 6378700.0f;
//ave. radius = 6371.315 (someone said more accurate is 6366.707)
//equatorial radius = 6378.388
//nautical mile = 1.15078
double radlat1 = PI*lat1/180.0f;
double radlat2 = PI*lat2/180.0f;
//now long.
double radlong1 = PI*lon1/180.0f;
double radlong2 = PI*lon2/180.0f;
if( radlat1 < 0 ) radlat1 = PI/2 + fabs(radlat1);// south
if( radlat1 > 0 ) radlat1 = PI/2 - fabs(radlat1);// north
if( radlong1 < 0 ) radlong1 = PI*2 - fabs(radlong1);//west
if( radlat2 < 0 ) radlat2 = PI/2 + fabs(radlat2);// south
if( radlat2 > 0 ) radlat2 = PI/2 - fabs(radlat2);// north
if( radlong2 < 0 ) radlong2 = PI*2 - fabs(radlong2);// west
//spherical coordinates x=r*cos(ag)sin(at), y=r*sin(ag)*sin(at), z=r*cos(at)
//zero ag is up so reverse lat
double x1 = er * cos(radlong1) * sin(radlat1);
double y1 = er * sin(radlong1) * sin(radlat1);
double z1 = er * cos(radlat1);
double x2 = er * cos(radlong2) * sin(radlat2);
double y2 = er * sin(radlong2) * sin(radlat2);
double z2 = er * cos(radlat2);
double d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
//side, side, side, law of cosines and arccos
double theta = acos((er*er+er*er-d*d)/(2*er*er));
double dist = theta*er;
return dist;
}
我们都知道创建线程有两个方法: 一是通过继承Thread类;二是向Thread类传递一个Runnable对象.
比如说一个售票站有四个窗口卖票,我们要设计四个线程
第一种方法:传递一个Runnable对象.
public class MyThread { public static void main(String[] args) { TestThread tt = new TestThread(); new Thread(tt).start(); new Thread(tt).start(); new Thread(tt).start(); new Thread(tt).start(); } } class TestThread implements Runnable { public int ticket = 100; public void run() { while(ticket>0) { System.out.println(Thread.currentThread().getName()+"is saling ticket"+ticket--); } } }
二.通过继承Thread类
public class MyThread { public static void main(String[] args) { TestThread tt1 = new TestThread().start(); TestThread tt2= new TestThread().start(); TestThread tt3 = new TestThread().start(); TestThread tt4 = new TestThread().start(); } } class TestThread implements Runnable { public int ticket = 100; public void run() { while(ticket>0) { System.out.println(Thread.currentThread().getName()+"is saling ticket"+ticket--); } } }
结论:
方法一中四个线程是合作卖100张,而方法二中是四个线程分别各卖100张,就是说四个线程都会卖同一张票四次。
也就是说Runnalbe 是多个对象同时跑一个run,Thread是各自跑各自的
TestThread tt = new TestThread();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
这里只有一个线程实例,所以ticket数据共享;
而
TestThread tt1 = new TestThread().start();
TestThread tt2= new TestThread().start();
TestThread tt3 = new TestThread().start();
TestThread tt4 = new TestThread().start();
各有各自的ticket,各自属于各自的实例,不存在数据共享问题。
这就是说,上边那个是线程间数据共享的例子,有如火车售票。
我也是来总结下,沾沾楼主的光。
楼主万福!
刚开始学习android,以前学习J2me的时候也碰到过类似的问题,现在看到有同学总结过了,地址为:http://www.iteye.com/topic/642128
这里全文摘抄,以备学习:)
1、Drawable → Bitmap
Java代码
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
2、从资源中获取Bitmap
Java代码
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap → byte[]
Java代码
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
4、 byte[] → Bitmap
Java代码
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}