Canvas.DrawBitmap 意外结果(BUG 或奇怪的行为)Xamarin Android
Canvas.DrawBitmap unexpected result(BUG or strange behaviour) Xamarin Android
所以我在使用 Canvas.DrawBitmap() 方法时遇到问题,它得出了意想不到的结果。
让我们看看我的例子:
源图片
我有两个案例(仅供测试):
- 我想从0,0画红色矩形并给他尺寸1/4 的图像,我也想以相同的大小 (1/4) 绘制此位图并放在 Bottom.Right 位置。
- 再次绘制 具有相同条件的红色矩形(从 Top.Left(0,0) 开始,大小为 1/4图像)并绘制部分位图并显示为 1/4 大小的矩形(Bottom.Right)。
案例 #1
我正在这样做:
//source imageview
Source.SetImageResource(Resource.Drawable.lena30);
//bitmap
bt = ((BitmapDrawable)Source.Drawable).Bitmap.Copy(Bitmap.Config.Argb8888, true);
//second imageview, where i fill result
Draw.SetImageBitmap(bt);
can = new Canvas(bt);
Paint paint = new Paint();
paint.Color = Color.Red;
paint.SetStyle(Paint.Style.Fill);
//draw Red Rect with 1/4 size and locate Top.Left
can.DrawRect(0,0,bt.Width/2,bt.Height/2,paint);
//redraw new bitmap(all subset) and locate to Bottom.Right with 1/4 size
can.DrawBitmap(bt, null, new Rect(bt.Width/2, bt.Height / 2, bt.Width, bt.Height), null);
结果是:
案例 #2
相同,但现在是位图的一部分(不是位图的完整子集):
//source imageview
Source.SetImageResource(Resource.Drawable.lena30);
//bitmap
bt = ((BitmapDrawable)Source.Drawable).Bitmap.Copy(Bitmap.Config.Argb8888, true);
//second imageview, where i fill result
Draw.SetImageBitmap(bt);
can = new Canvas(bt);
Paint paint = new Paint();
paint.Color = Color.Red;
paint.SetStyle(Paint.Style.Fill);
//draw Red Rect with 1/4 size and locate Top.Left
can.DrawRect(0,0,bt.Width/2,bt.Height/2,paint);
//redraw new bitmap(not full,only part of Source Rectangle) and locate to Bottom.Right with 1/4 size
can.DrawBitmap(bt, new Rect(bt.Width/2,0,bt.Width,bt.Height), new Rect(bt.Width/2, bt.Height / 2, bt.Width, bt.Height), null);
所以我不明白,为什么会这样?(为什么图像没有缩放以适应大小并复制 矩形 !?)。
有任何想法吗?谢谢!
问题是您将 bt
Bitmap
绘制到自身上,这导致它递归绘制直到达到最小尺寸限制。这需要对您的代码进行一些修改,但您需要创建一个中间 Bitmap
和 Canvas
来进行绘图,然后将 Bitmap
设置在目标 ImageView
.
Source.SetImageResource(Resource.Drawable.lena30);
bt = ((BitmapDrawable) Source.Drawable).Bitmap.Copy(Bitmap.Config.Argb8888, true);
Paint paint = new Paint();
paint.Color = Color.Red;
paint.SetStyle(Paint.Style.Fill);
Canvas canSource = new Canvas(bt);
canSource.DrawRect(0, 0, bt.Width / 2, bt.Height / 2, paint);
Bitmap btDraw = Bitmap.CreateBitmap(bt.Width, bt.Height, Bitmap.Config.Argb8888);
Canvas canDraw = new Canvas(btDraw);
canDraw.DrawBitmap(bt, null, new Rect(0, 0, bt.Width, bt.Height), null);
canDraw.DrawBitmap(bt, null, new Rect(bt.Width / 2, bt.Height / 2, bt.Width, bt.Height), null);
Draw.SetImageBitmap(btDraw);
注意:我从未使用过 Xamarin,所以请原谅我尝试翻译中的任何语法错误。
所以我在使用 Canvas.DrawBitmap() 方法时遇到问题,它得出了意想不到的结果。
让我们看看我的例子:
源图片
我有两个案例(仅供测试):
- 我想从0,0画红色矩形并给他尺寸1/4 的图像,我也想以相同的大小 (1/4) 绘制此位图并放在 Bottom.Right 位置。
- 再次绘制 具有相同条件的红色矩形(从 Top.Left(0,0) 开始,大小为 1/4图像)并绘制部分位图并显示为 1/4 大小的矩形(Bottom.Right)。
案例 #1
我正在这样做:
//source imageview
Source.SetImageResource(Resource.Drawable.lena30);
//bitmap
bt = ((BitmapDrawable)Source.Drawable).Bitmap.Copy(Bitmap.Config.Argb8888, true);
//second imageview, where i fill result
Draw.SetImageBitmap(bt);
can = new Canvas(bt);
Paint paint = new Paint();
paint.Color = Color.Red;
paint.SetStyle(Paint.Style.Fill);
//draw Red Rect with 1/4 size and locate Top.Left
can.DrawRect(0,0,bt.Width/2,bt.Height/2,paint);
//redraw new bitmap(all subset) and locate to Bottom.Right with 1/4 size
can.DrawBitmap(bt, null, new Rect(bt.Width/2, bt.Height / 2, bt.Width, bt.Height), null);
结果是:
案例 #2
相同,但现在是位图的一部分(不是位图的完整子集):
//source imageview
Source.SetImageResource(Resource.Drawable.lena30);
//bitmap
bt = ((BitmapDrawable)Source.Drawable).Bitmap.Copy(Bitmap.Config.Argb8888, true);
//second imageview, where i fill result
Draw.SetImageBitmap(bt);
can = new Canvas(bt);
Paint paint = new Paint();
paint.Color = Color.Red;
paint.SetStyle(Paint.Style.Fill);
//draw Red Rect with 1/4 size and locate Top.Left
can.DrawRect(0,0,bt.Width/2,bt.Height/2,paint);
//redraw new bitmap(not full,only part of Source Rectangle) and locate to Bottom.Right with 1/4 size
can.DrawBitmap(bt, new Rect(bt.Width/2,0,bt.Width,bt.Height), new Rect(bt.Width/2, bt.Height / 2, bt.Width, bt.Height), null);
所以我不明白,为什么会这样?(为什么图像没有缩放以适应大小并复制 矩形 !?)。
有任何想法吗?谢谢!
问题是您将 bt
Bitmap
绘制到自身上,这导致它递归绘制直到达到最小尺寸限制。这需要对您的代码进行一些修改,但您需要创建一个中间 Bitmap
和 Canvas
来进行绘图,然后将 Bitmap
设置在目标 ImageView
.
Source.SetImageResource(Resource.Drawable.lena30);
bt = ((BitmapDrawable) Source.Drawable).Bitmap.Copy(Bitmap.Config.Argb8888, true);
Paint paint = new Paint();
paint.Color = Color.Red;
paint.SetStyle(Paint.Style.Fill);
Canvas canSource = new Canvas(bt);
canSource.DrawRect(0, 0, bt.Width / 2, bt.Height / 2, paint);
Bitmap btDraw = Bitmap.CreateBitmap(bt.Width, bt.Height, Bitmap.Config.Argb8888);
Canvas canDraw = new Canvas(btDraw);
canDraw.DrawBitmap(bt, null, new Rect(0, 0, bt.Width, bt.Height), null);
canDraw.DrawBitmap(bt, null, new Rect(bt.Width / 2, bt.Height / 2, bt.Width, bt.Height), null);
Draw.SetImageBitmap(btDraw);
注意:我从未使用过 Xamarin,所以请原谅我尝试翻译中的任何语法错误。