我尝试使用矩阵值旋转图像,并将旋转后的位图设置为图像视图。在这种情况下,图像视图背景被设置为黑色。请对此提出任何建议。
设备详细信息:摩托罗拉Moto G(4)(安卓7.0-Api 24)
样品:
我的代码:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
LinearLayout linearLayout = new LinearLayout(this);
imageView = new ImageView(this);
imageView.LayoutParameters = new Android.Views.ViewGroup.LayoutParams(LinearLayout.LayoutParams.MatchParent, 1200);
bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Nature);
linearLayout.AddView(imageView);
linearLayout.Orientation = Orientation.Vertical;
Button button = new Button(this);
button.Text = "Click me";
button.Click += Button_Click;
button.LayoutParameters = new Android.Views.ViewGroup.LayoutParams(150, 150);
linearLayout.AddView(button);
imageView.SetImageBitmap(bitmap);
SetContentView(linearLayout);
}
private void Button_Click(object sender, System.EventArgs e)
{
int angle = 45;
Matrix = new Matrix();
int cx = bitmap.Width / 2;
int cy = bitmap.Height / 2;
this.Matrix.PreTranslate(-cx, -cy);
this.Matrix.PostRotate(angle);
this.Matrix.PostTranslate(this.imageView.Width / 2, this.imageView.Height / 2);
using (var tiltBitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, Matrix, false))
{
imageView.SetImageBitmap(tiltBitmap);
}
this.Matrix.Dispose();
}