Android在另一个图像内绘制图像

问题描述 投票:0回答:2

如何使用画布在另一个图像中绘制图像;像这样,看一下图片:“在此处输入图像描述”

将其放入这样的地图应用v2中

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

我已经在这样的矩形中绘制了图片

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

如何执行此操作请帮助我

android canvas google-maps-android-api-2 marker
2个回答
0
投票

使用所需框架的形状为画布设置剪切路径:

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

如果您随后在画布中绘制的所有内容都不在圆内,则将不可见。

如果您需要其他图纸,并且只能在此框架之外进行,则可以通过以下方式对其进行保护:

canvas.clipRect(oldClipBounds, Region.Op.REVERSE_DIFFERENCE);

0
投票

我找到了解决此问题的方法:您可以将图像放置在画布上,然后将其调整为正确大小,然后将其放置在标记图像中。

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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.