在从url获取的资源drawable中显示标记

时间:2012-07-12 07:41:04

标签: android google-maps android-canvas

map showing uers

这是我的地图页面,显示了我的应用程序的所有用户。图像(标记)也是从我的服务器提供的URL中获得的。这些标记必须放在Drawable内(如图所示的圆形图像)。我使用Canvas从网址创建了一个像Bitmap这样的圆圈。

    public Drawable showMe(String url)
    {
        Bitmap bitmap=null;
         try {
                URL newurl = new URL(url);
                bitmap = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        bitmap=getBitmap(url);
        Paint paint = new Paint();
           paint.setFilterBitmap(true);
           int targetWidth  = 30;
           int targetHeight = 30;
           Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
           RectF rectf = new RectF(0, 0, 30, 30);
           Canvas canvas = new Canvas(targetBitmap);
           Path path = new Path();
          path.addRoundRect(rectf, targetWidth, targetHeight, Path.Direction.CW);
        canvas.clipPath(path);
          canvas.drawBitmap( bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
                           new Rect(0, 0, targetWidth, targetHeight), paint);

           Matrix matrix = new Matrix();
           matrix.postScale(1f, 1f);
           Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 30, 30, matrix, true);
           Bitmap bitmap_circle=mergeBitmaps(resizedBitmap);
           BitmapDrawable bd = new BitmapDrawable(bitmap_circle);



          return bd;
    }

上面的函数将为marker创建最终的drawable。另外mergeBitmaps()函数将资源drawable和位图合并在一起..

public Bitmap mergeBitmaps(Bitmap manBitmap){

            try{

                Bitmap markerBitmap = BitmapFactory.decodeResource( this.getResources(), R.drawable.circle_bg);
                Bitmap bmOverlay = Bitmap.createBitmap(markerBitmap.getWidth(), markerBitmap.getHeight(), markerBitmap.getConfig());
                Canvas canvas = new Canvas(bmOverlay);
                Matrix matrix = new Matrix();
                matrix.postScale(1f, 1f);
                canvas.drawBitmap(markerBitmap, matrix, null);
                canvas.drawBitmap(manBitmap, 5, 5, null);
                return bmOverlay;

            }
            catch(Exception ex){
                ex.printStackTrace();
                return null;
            }

        }

但问题是,这个位图最适合背景Drawable,以便感觉两者一起会产生单个图像。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

变化

canvas.drawBitmap(manBitmap, 5, 5, null);

像smtg一样

canvas.drawBitmap(manBitmap, (markerBitmap.getWidth()-manbitmap.getWidth())/2, 
    (markerBitmap.getHeight()-manbitmap.getHeight())/2, null);