Canvas.DrawBitmap意外结果(BUG或奇怪的行为)Xamarin Android

时间:2015-12-09 16:09:00

标签: android canvas bitmap xamarin drawbitmap

所以我对Canvas的方法有问题。 DrawBitmap(),这会产生意想不到的结果。
让我们看看我的例子:
来源图片
image

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

  1. 我想绘制来自 0,0 的红色矩形,并为他提供尺寸 1/4 的图片,我也想画画这个位图大小相同(1/4)并放到Bottom.Right位置。
  2. 再次绘制具有相同条件的红色矩形(从 Top.Left (0,0)和图像的大小1/4开始)并绘制部分位图并显示为1/4大小的矩形( Bottom.Right )。
  3. 案例#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);  
    

    结果是:
    result case1

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

    result case2

    所以我无法理解为什么会发生这种情况?(为什么图像没有缩放以适合大小并重复矩形!?)。
    有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

问题是您正在将bt Bitmap绘制到自身上,导致它以递归方式绘制,直到达到最小大小限制。它会对您的代码进行一些修改,但您需要创建一个中间BitmapCanvas来进行绘制,然后设置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,所以请原谅我试图翻译中的语法错误。