开发者

Canvas as ImageView/Bitmap

开发者 https://www.devze.com 2023-02-25 02:55 出处:网络
I cant get my head around this problem I have. I have 2 im开发者_C百科ages which were added to a canvas and will be treated as one object, now I need to return this canvas as bitmap/drawable, since

I cant get my head around this problem I have. I have 2 im开发者_C百科ages which were added to a canvas and will be treated as one object, now I need to return this canvas as bitmap/drawable, since Here is code how I added 2 bitmaps into a canvas

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???????????

Please someone help. Tnx in advance!


You can create a Bitmap to draw into.

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


Where did you get the canvas from? If from a bitmap, then that obj will now have whatever you drew on the canvas applied to it.

Canvas is just a way to draw onto a bitmap or drawable that it is backed by. So if you create a Bitmap called result and then get your canvas from that you can just return that.

As in..

Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
...do your stuff...
return result;
0

精彩评论

暂无评论...
验证码 换一张
取 消