Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

android - Using method -canvas.drawBitmap(bitmap, src, dst, paint)

Everytime I use this code nothing is drawn. I need to draw a bitmap inside of a specified rectangle.

canvas.drawBitmap(MyBitmap, null, rectangle, null)

I've looked online but can't find much help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT


The original answer is incorrect. You can use the sourceRect to specify a part of a Bitmap to draw. It may be null, in which case the whole image will be used.

As per the fryer comment he was drawing beneath something, I'll add a note on that.

drawBitmap(bitmap, srcRect, destRect, paint) does not handle Z ordering (depth) and the order of calling draw on object matters.

If you have 3 shapes to be drawn, square, triangle and circle. If you want the square to be on top then it must be drawn last.


You're not specified any source, so its not drawn anything.

Example:

You have a Bitmap 100x100 pixels. You want to draw the whole Bitmap.

canvas.drawBitmap(MyBitmap, new Rect(0,0,100,100), rectangle, null);

You want to draw only the left half of the bitmap.

canvas.drawBitmap(MyBitmap, new Rect(0,0,50,100), rectangle, null);

You need to specify the source rect, the source rect can be a rectangle anywhere from 0,0 to the width,height of the bitmap.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...