下载后可以在画布上绘制google静态地图吗? [英] Can you draw a google static map to a canvas after you download it?

查看:64
本文介绍了下载后可以在画布上绘制google静态地图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下类来下载特定的地图,我想将其与其他位图一起绘制到画布上,但是当我这样做时,仅地图不会显示. 我知道地图正在下载,我是否必须使其可变,我不确定这里发生了什么.

I am using the following class to download a specific map and i'd like to draw it to a canvas with other bitmaps but when I do it only the map doesn't show up. I know the map is downloading, do I have to make it mutable, i'm not completely sure what's going on here.

 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;
            }
        }

推荐答案

正如我在评论中提到的,您实际上可以重用位图.

As i mentioned in comments, You could actually reuse the bitmap.

您还可以直接在画布上绘制日期和时间,因为datetime位图将占用160000字节,即156kb.

You could also draw on canvas directly for date and time since the datetime bitmap will consume 160000 bytes i.e 156kb.

我尚未测试代码.这只是一个建议.

I have not tested the code. This is just a suggestion.

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;
        }
    }
}

这篇关于下载后可以在画布上绘制google静态地图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆