可以使用canvas来操作图像吗
是否可以使用画布操作图像?我们如何将图像放到画布上?
@Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mBitmap = bitmap.copy(bitmap.getConfig(), true);
canvas = new Canvas(mBitmap);
Matrix matrix = new Matrix();
canvas.drawBitmap(mBitmap, matrix, mPaint);
}
我无法在屏幕上看到图像。 canvas.drawBitmap() 应该不是必需的,因为我正在使用构造函数并传递 mBitmap。
Is it possible to manipulate images using canvas? How do we get the image onto the canvas?
@Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mBitmap = bitmap.copy(bitmap.getConfig(), true);
canvas = new Canvas(mBitmap);
Matrix matrix = new Matrix();
canvas.drawBitmap(mBitmap, matrix, mPaint);
}
I'm unable to see the image on the screen. canvas.drawBitmap() shouldn't be necessary since I'm using the constructor and passing mBitmap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以,但你不能“将图像放到画布上”,画布只是一个在位图上绘制的界面。为此,只需创建一个 Canvas 并提供对可变位图的引用:
Canvas c = new Canvas(myBitmap);
非常简单:)
Yes you can, but you don't "get the image onto the Canvas," a Canvas is just an interface to draw onto a Bitmap. To do so, simply create a Canvas and give a reference to your mutable Bitmap:
Canvas c = new Canvas(myBitmap);
Very easy :)
您不应该在 onDraw 方法中执行此操作。尝试创建一个应该显示图像的 ImageView,然后通过 setImageBitmap(位图)。
一般来说,您的代码中做错了一些事情。
You shouldn't do this in your onDraw method. Try to create an ImageView that should display the image and then set the bitmap via setImageBitmap(bitmap).
In general you are doing somethings wrong in your code.