[英]Flip a bitmap horizontally and draw to specific part of canvas
How do I draw this bitmap to the canvas in the exact same placement, but flipped horizontally? 如何在完全相同的位置上将此位图绘制到画布上,但水平翻转? The matrix documentation is not exactly verbose.
矩阵文档并不十分详细。 Here is the initial drawing:
这是初始图:
canvas.DrawBitmap (_ballBmpRight, ParentView._ballX, ParentView._ballY, null);
My attempt to flip the bitmap: 我试图翻转位图:
flipHorizontalMatrix = new Matrix();
flipHorizontalMatrix.SetScale(-1,1);
flipHorizontalMatrix.PostTranslate(_ballBmpRight.Width, 0);
Then draw it (this is where I cannot place it in the correct part of the canvas): 然后绘制它(这是我无法将其放置在画布的正确部分的位置):
canvas.DrawBitmap (_ballBmpRight, flipHorizontalMatrix, null);
I ended up doing this: 我最终这样做:
BitmapDrawable flip(BitmapDrawable d)
{
Matrix m = new Matrix();
m.PreScale(-1, 1);
Bitmap src = d.Bitmap;
Bitmap dst = Bitmap.CreateBitmap(src, 0, 0, src.Width, src.Height, m, false);
//dst.SetDensity(DisplayMetrics.DENSITY_DEFAULT);
return new BitmapDrawable(dst);
}
Client code for flip: 客户端代码翻转:
var bd = new BitmapDrawable (_ballBmpRight);
BitmapDrawable bdFlipped = flip (bd);
_ballBmpLeft = bdFlipped.Bitmap;
Canvas drawing code: 画布绘制代码:
canvas.DrawBitmap (_ballBmpLeft, ParentView._ballX, ParentView._ballY, null);
Please note the method names have varied slightly (.getBitmap() becomes just .Bitmap) as I am using Xamarin.Android. 请注意,当我使用Xamarin.Android时,方法名称略有不同(.getBitmap()变为.Bitmap)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.