Android的 - 帆布 - 从文件加载图片,修改和完整的结果保存到一个文件 [英] Android - Canvas - load image from file, modify it and save the complete result to a file
问题描述
当画布的进入,我想从文件中加载图像。
然后我想修改它,并最终将其存储到同一个文件。
Upon entry of the canvas, I would like to load an image from a file. Then I would like to modify it and finally store it to the same file.
通过图像加载(或更新)到MYBITMAP一个按钮。
Via a button the image is loaded (or updated) into the myBitMap.
方法1:不工作。
protected void onDraw(Canvas canvas) {
if( myBitMap != null) {
// I added an image to it via a button
canvas = new Canvas( myBitMap.copy(Bitmap.Config.ARGB_8888, true));
myBitMap = null;
}
// rest of the drawing code
方法2:不工作...但我需要加载图像的重新划分都和新改变的东西。因此,在每一个变化(onTouch ...)我需要重新绘制两个图像。
Method 2: does work ... but needs both redrawing of my loaded image and the newly changed stuff. So, at each change (onTouch...) I need to redraw BOTH images.
protected void onDraw(Canvas canvas) {
if( myBitMap != null) canvas.drawBitmap( myBitMap, 0, 0, null);
// rest of the drawing code
这是为什么?
有没有把加载的图像到画布使用位图的方法吗?再有就是与onTouchEvents改变1个完整的位图。
Is there a way to put the loaded image into the bitmap the canvas is using? Then there is 1 complete bitmap that is changed with onTouchEvents.
推荐答案
选项1:画上加载的位图和整个绘制到视图的画布
Option 1: draw on the loaded bitmap and draw the whole to the View's canvas.
- 从文件加载位图。
- 绘制位图视图的画布上,例如canvas.DrawBitmap(位图,0,0,NULL);
- 新的(交互)图纸也可以直接在画布上完成的。
- 从DrawingCache读取保存整体。
缺点是,如果你使用滚动,视口以外的任何东西被裁剪。标准drawingcache具有有限的大小。
The disadvantage is that if you use scrolling, anything outside the viewport is clipped. The standard drawingcache has a limited size.
选项2:绘制一个位图,最后绘制视图的画布上。
Option 2: draw on a bitmap and finally draw that on the View's canvas.
- 加载该文件中的位图,例如loadedBitmap。
- 画布myCanvas =新的Canvas(loadedBitmap);
- myCanvas.drawRect(....);
- 更绘图动作和最后...
- canvas.drawBitmap(loadedBitmap);
- 此loadedBitmap当然可以容易地写入到一个文件(例如,PNG)。
该位应该是可变的(不可变= TRUE)。
The bitmap should be mutable (imMutable = true).
选项3:用自己的尺寸创建自己的cachedBitmap
Option 3: create your own cachedBitmap with your own size
这是类似的选项2,但你可以定义与您的特定大小的位图。你可以通过复制loadedBitmap到新的位图,等启动
This is similar to option 2, but you can then define a bitmap with your specific size. You could start by copying the loadedBitmap into your new bitmap, etc.
这篇关于Android的 - 帆布 - 从文件加载图片,修改和完整的结果保存到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!