Show marker inside a resource drawable which obtained from url

显示uers的地图

Here is the map page I have, which shows all the users of my App. Also the images(markers) are obtained from the URL given from my server. These markers has to put inside a Drawable(a circle like image as shown). I created a circle like Bitmap from the url using Canvas.

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

The above function will create the final drawable for the marker.Also the mergeBitmaps() function merge the both the resource drawable and the bit map together..

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

        }

But the problem is, this bitmap is not best fit inside the background Drawable in order to get a feeling that both together will give a single image.

Can anyone help me ?


change

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

to smtg like

canvas.drawBitmap(manBitmap, (markerBitmap.getWidth()-manbitmap.getWidth())/2, 
    (markerBitmap.getHeight()-manbitmap.getHeight())/2, null);
链接地址: http://www.djcxy.com/p/10942.html

上一篇: 将2D网格图数据结构转换为树

下一篇: 在从url获取的资源drawable中显示标记