我无法理解我遇到的这个问题。我有2个图像被添加到画布并将被视为一个对象,现在我需要将此画布作为位图/可绘制返回,因为 以下是我将2位图添加到画布中的代码
Bitmap image1=BitmapFactory.decodeResource(getResources() ,R.drawable.icon1);
Bitmap image2=BitmapFactory.decodeResource(getResources() R.drawable.icon2);
Rect srcRect = new Rect(0, 0, image.getWidth(), image.getHeight());
Rect dstRect = new Rect(srcRect);
dstRect.offset(15, 0);
canvas.drawBitmap(image, srcRect, dstRect, null);
dstRect.offset(image.getWidth(), 0);
canvas.drawBitmap(image2, srcRect, dstRect, null);
//return???????????
请有人帮忙。 Tnx提前!
答案 0 :(得分:5)
您可以创建一个要绘制的位图。
Bitmap image1=BitmapFactory.decodeResource(getResources() ,R.drawable.icon1);
Bitmap image2=BitmapFactory.decodeResource(getResources() R.drawable.icon2);
Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);//Create the canvas to your image
Rect srcRect = new Rect(0, 0, image.getWidth(), image.getHeight());
Rect dstRect = new Rect(srcRect);
dstRect.offset(15, 0);
canvas.drawBitmap(image, srcRect, dstRect, null); //draw on it
dstRect.offset(image.getWidth(), 0);
canvas.drawBitmap(image2, srcRect, dstRect, null);
return result;//result will have the drawed images from the canvas
答案 1 :(得分:1)
你从哪里获得画布?如果来自位图,则该obj现在将具有您在应用于它的画布上绘制的任何内容。
Canvas只是一种绘制到它所支持的位图或drawable的方法。因此,如果您创建一个名为result的Bitmap,然后从中获取画布,则可以返回该画布。
如...
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
...do your stuff...
return result;