本帖最后由 etracer 于 2013-06-13 15:08:42 编辑

解决方案 »

  1.   

    恩,好多图,图不大,但是好多图,反复交替时候,除问题,还有就是我要根据gps来不停的画,所以也会溢出给您看代码哦
    public class Map1 extends Activity { int testx = 1;
    int testy = 2;
    double YDNUM = 0.9144;
    private SensorManager sensorManager;
    private Bitmap bitmap1 = null;
    private ImageView imageView; private SensorEventListener listener = new SensorListener(); private LocationManager mLocationManager;
    protected static final int GUIUPDATEIDENTIFIER = 0x101;
    private Location location = null;
    @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

    } Handler mHandler = new Handler() { @Override
    public void handleMessage(Message msg) { switch (msg.what) {
    case Map1.GUIUPDATEIDENTIFIER:
    gpspicshow(location);
    // Toast.makeText(getApplicationContext(),
    // "image更新>>>>>>>>>>>>>>>>>>>:", Toast.LENGTH_SHORT)
    // .show();
    break;
    } super.handleMessage(msg);
    }
    }; // -----------GPS距離開始------------
    private static final double EARTH_RADIUS = 6378137;
    private LocationListener locationListener = null;
    private int bitmapHeight;
    private int bitmapWith;
    private GpsData gpsData;
    private int w;
    private int h;
    private int w_2;
    private int h_2; private List objectList; private static double rad(double d) {
    return d * Math.PI / 180.0;
    }
    // -----------GPS距離終了------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map1); objectList = (List) getIntent().getSerializableExtra("ListMapArr"); String maptypename = (String) (this.getIntent().getExtras()
    .getString("maptypeindex"));
    // 地図objectリスト
    gpsData = (GpsData) objectList.get(Integer.parseInt(maptypename)); // Toast.makeText(
    // getApplicationContext(),
    // "FFFFFFFFFFFFFFF" + maptypename + "<><><>"
    // + gpsData.getBaseGpsStar()[0], Toast.LENGTH_SHORT)
    // .show(); imageView = (ImageView) this.findViewById(R.id.imageView);
    imageView.setKeepScreenOn(true);// sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);//
    locationListener = new LocationListener() { @Override
    public void onLocationChanged(Location location) {
    // ------GPSを表示----
    // gpspicshow(location);
    getGps(); }
    }; openGPSSettings();
    getGps(); } // GPSを読み込む
    private void getGps() { mLocationManager = (LocationManager) this
    .getSystemService(Context.LOCATION_SERVICE); // 查找服务信息
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE); // 精度最高
    criteria.setAltitudeRequired(false); // 海拔信息:不需要
    criteria.setBearingRequired(false); // 方法ミセッジ:いらん
    criteria.setCostAllowed(true); // 是否允许付费
    // criteria.setPowerRequirement(Criteria.POWER_LOW); //耗电量: 低功耗 String provider = mLocationManager.getBestProvider(criteria, true); // 获取GPS信息 location = mLocationManager.getLastKnownLocation(provider); Message message = new Message();
    message.what = Map1.GUIUPDATEIDENTIFIER;
    Map1.this.mHandler.sendMessage(message); mLocationManager.requestLocationUpdates(provider, 2000, 5,
    locationListener);
    } // gpsを表示します
    public void gpspicshow(Location location) { // -------GPS--の処理 if (location != null) {
    try { bitmap1 = BitmapFactory.decodeResource(
    getResources(),
    R.drawable.class.getField(gpsData.getMapname()).getInt(
    new R.drawable())).copy(
    Bitmap.Config.ARGB_8888, true); for (int j = 0; j < objectList.size(); j++) {
    GpsData tmpgpsData = (GpsData) objectList.get(j);
    int a = 1;
    if (((location.getLatitude() <= tmpgpsData.getBaseGpsStar()[0]) && (location
    .getLatitude() >= tmpgpsData.getBaseGpsEnd()[0]))
    && ((location.getLongitude() >= tmpgpsData
    .getBaseGpsStar()[1]) && (location
    .getLongitude() <= tmpgpsData
    .getBaseGpsEnd()[1]))) {
    gpsData = (GpsData) objectList.get(j);
    continue;
    } } } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (NoSuchFieldException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } else {// GPSを取れない場合 bitmap1 = BitmapFactory.decodeResource(getResources(),
    R.drawable.defult).copy(Bitmap.Config.ARGB_8888, true);
    }
    bitmapHeight = bitmap1.getHeight();
    bitmapWith = bitmap1.getWidth();
    // -----図に描きます---- Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
    R.drawable.people)).getBitmap();
    Bitmap bitmapbgimg = ((BitmapDrawable) getResources().getDrawable(
    R.drawable.bgimg)).getBitmap();
    Bitmap newBitmap = null; newBitmap = Bitmap.createBitmap(bitmap1);
    Canvas canvas = new Canvas(newBitmap);
    Paint paint = new Paint(); w = bitmap1.getWidth();
    h = bitmap1.getHeight(); w_2 = bitmap2.getWidth();
    h_2 = bitmap2.getHeight();
    double[] golfxy = gpsData.getGolfEnd();
    double bt = gpsData.getBT();
    double rt = gpsData.getRT();
    double ft = gpsData.getFT();
    double lt = gpsData.getLT();
    String showfar;
    // -----------YD計算-------------------
    if (location != null) {
    double latx = location.getLatitude();// y
    double lonx = location.getLongitude();// x
    double nowdis = DistanceOfTwoPoints(golfxy[0], golfxy[1], latx,
    lonx);// 実際GPS距離差 NumberFormat ddf1 = NumberFormat.getNumberInstance(); ddf1.setMaximumFractionDigits(2);
    showfar = ddf1.format(nowdis / YDNUM); // showfar=String.valueOf(nowdis/YDNUM); } else { showfar = "計算不可";
    } // paint.setColor(Color.BLACK);
    paint.setAlpha(99);
    // 情報表示
    canvas.drawBitmap(bitmapbgimg, 0, 15, paint);// test
    // canvas.drawRect(0, 0, 250, 150,paint);
    paint.setTextSize(30); paint.setARGB(255, 250, 0, 0);
    canvas.drawText("カップまで" + showfar + "YD", 5, 55, paint);
    paint.setTextSize(25);
    paint.setARGB(255, 0, 0, 0);
    canvas.drawText("BT" + bt + "yd", 5, 85, paint);
    paint.setARGB(255, 0, 0, 0);
    canvas.drawText("RT" + rt + "yd", 5, 115, paint);
    paint.setARGB(255, 0, 0, 0);
    canvas.drawText("FT" + ft + "yd", 5, 145, paint);
    paint.setARGB(255, 0, 0, 0);
    canvas.drawText("LT" + lt + "yd", 5, 175, paint); // paint = new Paint();
    // canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2,
    // Math.abs(h - h_2) / 2, paint);
    if (location != null) {
    double[] tmpXY = gps2xy(location); double lattmp = tmpXY[0];
    double lontmp = tmpXY[1];
    canvas.drawBitmap(bitmap2, (int) lattmp, (int) lontmp, paint); } else { canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2,
    Math.abs(h - h_2) / 2, paint);
    } canvas.save(Canvas.ALL_SAVE_FLAG);
    canvas.restore();
    imageView.setBackgroundColor(Color.BLUE);
    imageView.setImageBitmap(newBitmap); } private void openGPSSettings() {
    LocationManager alm = (LocationManager) this
    .getSystemService(Context.LOCATION_SERVICE);
    if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { // Toast.makeText(this, "GPSが正常", Toast.LENGTH_SHORT).show(); return;
    } Toast.makeText(this, "GPSを開けてください!", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
    startActivityForResult(intent, 0); // 画面に戻る } @Override
    protected void onResume() {
    Sensor sensors = sensorManager
    .getDefaultSensor(Sensor.TYPE_ORIENTATION); sensorManager.registerListener(listener, sensors,
    sensorManager.SENSOR_DELAY_GAME); super.onResume();
    } public class SensorListener implements SensorEventListener {
    private float preDegress = 0;// @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub } @Override
    public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    float degree = event.values[0];// 方法数値 Animation animation = new RotateAnimation(preDegress, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(200);// 回転時間
    // imageView.startAnimation(animation);
    preDegress = -degree;// 角度を保存する }
    }}