0

だから私はCanvasの方法に問題があります。DrawBitmap()、予期しない結果を描画します。
私の例を見てみましょう:
ソース画像
画像


私は2つのケースを持っています(テストのためだけに):

  1. 私は0,0から赤い四角形を描画し、彼にイメージのサイズ1/4を与えたいです。また、このビットマップを同じサイズ (1/4) で描画し、Bottom.Right の場所に配置したいと考えています。
  2. 同じ条件 ( Top.Left (0,0) から開始し、画像の 1/4 のサイズ) で再度赤い四角形を描画し、ビットマップの一部を描画して、1/4 サイズ ( Bottom.Right ) の Rectangle として表示します。

ケース #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);  

結果は次のとおりです。
結果 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);  

結果 case2


だから私は理解できません、なぜそれが起こるのですか?(サイズに合わせてスケーリングせず、長方形を複製するのはなぜですか?)
何か案は?ありがとう!

4

1 に答える 1

1

問題は、bt Bitmapそれ自体に描画しているため、最小サイズ制限に達するまで再帰的に描画されることです。コードを少し手直しする必要がありますが、中間を作成し、Bitmapその上Canvasで描画を行い、それをBitmaptarget に設定する必要があります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 に答える