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

时间:2014-04-15 08:57:49

标签: android canvas google-maps-android-api-2 marker

如何使用Canvas在另一个图像中绘制图像; 像这样,看看图片:enter image description here

将它放在像这样的地图应用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);

如何做到这一点 请帮帮我

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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;

//为操作创建一个矩阵

    Matrix matrix = new Matrix();

//调整比特地图

    matrix.postScale(scaleWidth, scaleHeight);

//重新开始新的BITMAP

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;

}

}