0

所以我对 Canvas 的方法有疑问。DrawBitmap(),绘制出意外的结果。
让我们看看我的例子:
源图像
图片


我有两个案例(仅供测试):

  1. 我想从0,0绘制红色矩形并给他1/4大小的图像,我也想以相同大小(1/4)绘制这个位图并放在底部。正确的地方。
  2. 再次绘制具有相同条件的红色矩形(从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);  

结果是:
结果案例1


案例#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);  

结果案例2


所以我不明白,为什么会这样?(为什么图像没有缩放以适应大小并重复矩形!?)。
有任何想法吗?谢谢!

4

1 回答 1

1

问题是您正在绘制bt Bitmap自身,这导致它递归绘制直到达到最小尺寸限制。这将需要对您的代码进行一些修改,但您需要创建一个中间件BitmapCanvas在其上进行绘图,然后将其设置Bitmap在 target 上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,因此请原谅我尝试翻译中的任何语法错误。

于 2015-12-09T21:27:26.747 回答