How can I rotate an ImageView?

后端 未结 1 1839
慢半拍i
慢半拍i 2021-01-27 08:49

I want to rotate an ImageView programmatically in my Activity. Currently I couldn\'t find any solutions for Xamarin which works and translated solutions from Android didn\'t wor

相关标签:
1条回答
  • 2021-01-27 09:23

    The line

    Bitmap bMap = BitmapFactory.DecodeResource(Resources, Resource.Id.imageView1);
    

    Is where your app will fail. Why? The Resource Id you are passing in is not for an actual image, but for the ImageView instance. DecodeResource is used for decoding images and not views.

    Instead you should do something like:

    var drawable = image.Drawable;
    using (var bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Config.ARGB_8888))
    using(var canvas = new Canvas(bitmap))
    {
        drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
        drawable.Draw(canvas);
    
        var mat = new Matrix();
        mat.PostRotate(45);
        using(var bMapRotate = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mat, true))
            image.SetImageBitmap(bMapRotate);
    }
    
    0 讨论(0)
提交回复
热议问题