2013-04-20 2 views
-2

다음 클래스를 사용하여 특정 맵을 다운로드하고 있으며 다른 비트 맵을 사용하여 캔버스에 그려 넣고 싶습니다. 그러나지도를 표시 할 때만지도가 표시되지 않습니다. 지도가 다운로드 중임을 알고 있습니다. 변경 가능하도록해야하나요, 여기서 무슨 일이 일어나는지 완전히 확신 할 수 없습니다.Google 정적지도를 다운로드 한 후 캔버스에 그릴 수 있습니까?

class TheTask extends AsyncTask<Void,Void,Void> 
    { 
    File mediaFile = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + 
      "IMG_"+ MenuScreen.timeStamp + ".png"); 
    File mediaFile1 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + 
      "IMG_"+ MenuScreen.timeStamp + "1" + ".png"); 
    File mediaFile2 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + 
      "IMG_"+ MenuScreen.timeStamp + "2" + ".png"); 
    Bitmap bitmap; 
      protected void onPreExecute() 
      {   super.onPreExecute(); 
        //display progressdialog. 
      } 

      protected Void doInBackground(Void ...params) 
      { 





      bitmap = getGoogleMapThumbnail(MainActivity.latlng.latitude,MainActivity.latlng.longitude); 

      FileOutputStream outStream; 
      try { 
       outStream = new FileOutputStream(mediaFile2); 
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
       outStream.flush(); 
       outStream.close(); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


      //mapView.setDrawingCacheEnabled(enabled); 
      //mediaFile = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + 
        // "IMG_"+ MenuScreen.timeStamp + ".jpg"); 

       return null; 
      } 

      protected void onPostExecute(Void result) 
      {  

        super.onPostExecute(result); 
        Bitmap finished; 
         String path = mediaFile1.getAbsolutePath(); 
         String path1 = mediaFile2.getAbsolutePath(); 
         Bitmap map = BitmapFactory.decodeFile(path1); 
         Bitmap photo = BitmapFactory.decodeFile(path); 
         Paint into = new Paint(); 
         into.setFilterBitmap(true); 

          into.setColor(Color.BLACK); 
          into.setTextSize(25); 
          into.setUnderlineText(true); 
          into.setTypeface(Typeface.DEFAULT_BOLD); 
          Bitmap datetime = Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888); 
          Canvas can = new Canvas(datetime); 
          can.drawColor(Color.WHITE); 
          can.drawText(MainActivity.curDate, 0, 35, into); 
          can.drawText(MainActivity.curTime, 0, 70, into); 
          if(photo.getWidth() > photo.getHeight()){ 

          finished = Bitmap.createBitmap(480,720,Bitmap.Config.ARGB_8888); 

          Canvas ca = new Canvas(finished); 
          ca.drawColor(Color.WHITE); 
          ca.drawBitmap(map,0, 0, null); 
          ca.drawBitmap(datetime, 0, 0, null); 

          //ca.drawBitmap(bm, new Rect(0,0, bm.getWidth(), bm.getHeight()),new Rect(280,0,480,200) , null); 

          ca.drawBitmap(photo, 0, 400, null); 
         }else{ 

          finished = Bitmap.createBitmap(720,480,Bitmap.Config.ARGB_8888); 
          Canvas ca = new Canvas(finished); 
          ca.drawColor(Color.WHITE); 
          ca.drawBitmap(map, 320, 0, null); 
          ca.drawBitmap(datetime, 320, 0, null); 

          //ca.drawBitmap(bm, new Rect(0,0, bm.getWidth(), bm.getHeight()),new Rect(320,280,520,480) , null); 

          ca.drawBitmap(photo, 0, 0, null); 
         } 
          mediaFile1.delete(); 
          mediaFile2.delete(); 
         FileOutputStream outStream; 
         try { 
          outStream = new FileOutputStream(mediaFile); 
          finished.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
          outStream.flush(); 
          outStream.close(); 
         } catch (FileNotFoundException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

         //sendBroadcast(new Intent(
          // Intent.ACTION_MEDIA_MOUNTED, 
           // Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 


        //dismiss progressdialog. 
        //update ui 
      } 
      public static Bitmap getGoogleMapThumbnail(double lati, double longi){ 
       String URL = "http://maps.google.com/maps/api/staticmap?center=" +lati + "," + longi + "&zoom=15&size=200x200&sensor=false"; 
       Bitmap bmp = null; 
       bmp = getBitmapFromURL(URL); 

       /*HttpClient httpclient = new DefaultHttpClient(); 
       HttpGet request = new HttpGet(URL); 

       InputStream in = null; 
       try { 
        in = httpclient.execute(request).getEntity().getContent(); 
        bmp = BitmapFactory.decodeStream(in); 
        in.close(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }*/ 

       return bmp; 
      } 
      public static Bitmap getBitmapFromURL(String src) { 
      try { 
       URL url = new URL(src); 
       HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoInput(true); 
       connection.connect(); 
       InputStream input = connection.getInputStream(); 
       Bitmap myBitmap = BitmapFactory.decodeStream(input); 
       return myBitmap; 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 
+0

왜 doBackground에 비트 맵을 저장합니까? onPostExecute에서 해당 비트 맵을 직접 사용할 수 있습니다. 그리고지도는 캔버스에 그려지지만 사진과 겹쳐져 있다고 생각합니다. 지도를 제외한 다른 모든 drawBitmap에 주석을 달아보십시오. –

+0

좋아, 방금 돌아 왔어, 고마워. 지금 당장 시도하려고 했어. – JRowan

+0

지도가 날짜와 시간 비트 맵 뒤에 있었는데, 내가 v1을 사용했을 때 디버그 키가 있고 3 월 13 일 이후에 릴리스를 알지 못했다. 키가 중단되었습니다. v2로 변환하는 중입니다. 400X400을 가졌습니다. 고마워도 게시 할 수 있습니다. 나는 잠시 동안 올라와서 어제 석방 할 예정이었고, 변환해야한다는 것을 알았습니다. v2에, 시간 내 주셔서 감사 드리며, 진심으로 – JRowan

답변

1

의견에서 언급했듯이 실제로 비트 맵을 재사용 할 수 있습니다.

datetime 비트 맵은 160000 바이트, 즉 156kb를 소비하므로 캔버스에 날짜와 시간을 직접 그릴 수도 있습니다.

코드를 테스트하지 않았습니다. 이것은 단지 제안 일뿐입니다.

class TheTask extends AsyncTask<Void, Void, Bitmap> { 
    File mediaFile = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "IMG_" + MenuScreen.timeStamp + ".png"); 
    File mediaFile1 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "IMG_" + MenuScreen.timeStamp + "1" + ".png"); 
    //File mediaFile2 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "IMG_" + MenuScreen.timeStamp + "2" + ".png"); 
    Bitmap mapBitmap; 

    protected void onPreExecute() { 
     super.onPreExecute(); 
     // display progressdialog. 
    } 

    protected Bitmap doInBackground(Void... params) { 
     mapBitmap = getGoogleMapThumbnail(MainActivity.latlng.latitude, MainActivity.latlng.longitude); 
     // You could reuse this bitmap. Rather than saving and loading again 
     return mapBitmap; 
    } 

    protected void onPostExecute(Bitmap map) { 
     super.onPostExecute(map); 

     Bitmap finished; 

     String path = mediaFile1.getAbsolutePath(); 

     Bitmap photo = BitmapFactory.decodeFile(path); 

     Paint into = new Paint(); 

     Paint background = new Paint(); 
     background.setColor(Color.WHITE); 
     background.setStyle(Style.FILL); 

     into.setFilterBitmap(true); 

     into.setColor(Color.BLACK); 
     into.setTextSize(25); 
     into.setUnderlineText(true); 
     into.setTypeface(Typeface.DEFAULT_BOLD); 

     // Bitmap datetime = Bitmap.createBitmap(200, 200, 
     // Bitmap.Config.ARGB_8888); 
     // 
     // Canvas can = new Canvas(datetime); 
     // can.drawColor(Color.WHITE); 
     // can.drawText(MainActivity.curDate, 0, 35, into); 
     // can.drawText(MainActivity.curTime, 0, 70, into); 

     Rect rect = new Rect(); 

     if (photo.getWidth() > photo.getHeight()) { 

      finished = Bitmap.createBitmap(480, 720, Bitmap.Config.ARGB_8888); 

      Canvas ca = new Canvas(finished); 
      ca.drawColor(Color.WHITE); 

      ca.drawBitmap(map, 0, 0, null); 

      rect.set(0, 0, 200, 200); 
      ca.drawRect(rect, background); 
      ca.drawText(MainActivity.curDate, 0, 35, into); 
      ca.drawText(MainActivity.curTime, 0, 70, into); 

      ca.drawBitmap(photo, 0, 400, null); 
     } 
     else { 

      finished = Bitmap.createBitmap(720, 480, Bitmap.Config.ARGB_8888); 

      Canvas ca = new Canvas(finished); 
      ca.drawColor(Color.WHITE); 

      ca.drawBitmap(map, 320, 0, null); 

      ca.save(); 

      ca.translate(320, 0); 
      rect.set(0, 0, 200, 200); 
      ca.drawRect(rect, background); 
      ca.drawText(MainActivity.curDate, 0, 35, into); 
      ca.drawText(MainActivity.curTime, 0, 70, into); 

      ca.restore(); 

      ca.drawBitmap(photo, 0, 0, null); 
     } 

     mediaFile1.delete(); 

     FileOutputStream outStream; 
     try { 
      outStream = new FileOutputStream(mediaFile); 
      finished.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
      outStream.flush(); 
      outStream.close(); 
     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static Bitmap getGoogleMapThumbnail(double lati, double longi) { 
     String URL = "http://maps.google.com/maps/api/staticmap?center=" + lati + "," + longi + "&zoom=15&size=200x200&sensor=false"; 
     Bitmap bmp = null; 
     bmp = getBitmapFromURL(URL); 
     return bmp; 
    } 

    public static Bitmap getBitmapFromURL(String src) { 
     try { 
      URL url = new URL(src); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 
} 
+0

다시 한번 감사드립니다. – JRowan