Question

how to use Canvas to draw an image inside another image; like this, look at the picture : enter image description here

to put it in a map app v2 like this

 marker = gmap.addMarker(new MarkerOptions().title("test")
                        .position(new LatLng(0, 0))
                        .snippet("snipet test")
                        .icon(BitmapDescriptorFactory.fromBitmap(bitmap))

I already draw a picture in a rectangle like this

            InputStream inputStream = connection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);

            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);


            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawOval(rectF, paint);

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

how to do this please help me

Was it helpful?

Solution 2

I found a solution for this problem: you put the image in a circle with canvas after that you resize the result with the exact size, and put it inside the marker image.

public class IconMarker {
    public IconMarker(){}

    public Bitmap DrawMarker(String userid,int typeid,Resources rcs){

        //  image from database: ...InputStream inputStream = connection.getInputStream();
        //Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        Bitmap img1=new UserInfoBd().getPhotoProfil(userid);
        if(img1==null) img1=BitmapFactory.decodeResource(rcs,R.drawable.espace_photo);
        Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        Bitmap bmp = BitmapFactory.decodeResource(rcs,
                typeid);
        Bitmap output = Bitmap.createBitmap(bmp.getWidth(),
                bmp.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas1 = new Canvas(output);
        canvas1.drawBitmap(bmp, 0,0, null);
        Bitmap output1 = Bitmap.createBitmap(img1.getWidth(),
                img1.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output1);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, img1.getWidth(), img1.getHeight());
        final RectF rectF = new RectF(rect);


        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(img1, rect, rect, paint);
        Bitmap img=getResizedBitmap(output1,bmp.getHeight()*3/4-4,bmp.getWidth()*3/4);

        canvas1.drawBitmap(img,(float)4.7,(float)3.5,null);

        return output;

    }
    public  Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

        int width = bm.getWidth();

        int height = bm.getHeight();

        float scaleWidth = ((float) newWidth) / width;

        float scaleHeight = ((float) newHeight) / height;

        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();

        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        // RECREATE THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

        return resizedBitmap;
    }
}

OTHER TIPS

Set a clipping path for your canvas with the shape of the frame you want:

Path frame = new Path();
frame.addCircle(centerX, centerY, radius, Direction.CW);
canvas.getClipBounds(oldClipBounds); //see below
canvas.clipPath(frame);

Everything you draw into the canvas afterwards will not be visible if it's outside of the circle.

If you need additional drawing, which should take place only outside of this frame, you can protect it later on by:

canvas.clipRect(oldClipBounds, Region.Op.REVERSE_DIFFERENCE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top