在 xamarin android 中的某些设备中,ImageVIew 背景设置为黑色

问题描述

我尝试使用矩阵值旋转图像并将旋转后的位图设置为图像视图。在这种情况下,图像视图背景设置为黑色。请就此提出任何建议。

设备详情:摩托罗拉 Moto G(4) (Android 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,bitmap.Width,bitmap.Height,Matrix,false))
        {
         
            imageView.SetimageBitmap(tiltBitmap);
        }
        this.Matrix.dispose();
    }

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)