可以使用canvas来操作图像吗

发布于 2024-09-11 17:35:25 字数 441 浏览 4 评论 0原文

是否可以使用画布操作图像?我们如何将图像放到画布上?

@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 技术交流群。

扫码二维码加入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 :)

半夏半凉 2024-09-18 17:35:25

您不应该在 onDraw 方法中执行此操作。尝试创建一个应该显示图像的 ImageView,然后通过 setImageBitmap(位图)

一般来说,您的代码中做错了一些事情。

  1. 每次在屏幕上绘制内容时加载图像会大大降低应用程序的速度。尝试仅加载图像一次,然后编辑并将其设置到图像视图。
  2. 您正在为您获得的参数分配一个新的画布。但这不会改变屏幕上的任何内容。想象一下有人把一张纸放在你面前并说:“请在纸上写下你的信息”。现在你拿另一张白纸并在上面写字。但对方只知道他的论文,并且会用这篇论文来阅读你的消息。

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.

  1. Loading an image every time something should be drawn to the screen will slow down your application a lot. Try to load the image only once and then just edit and set it to the imageview.
  2. You are assigning a new Canvas to the parameter you got. But this won't change anything on the screen. Think of it like somebody puts a paper in front of you and says:"Please write your message on the paper". Now you take another blank paper and write on it. But the other person only knows about his paper and will use this paper to read your message.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文