Xamarin形式:图像输入无法按预期在android中运行

时间:2019-11-07 11:23:33

标签: xamarin.forms

我遵循此blog在条目中添加图片。最初在android和iOS中运行良好。

但是,当我将android目标版本和目标框架更改为9.0时,此功能开始失效。请查看以下屏幕截图。

enter image description here

条目内部的图像被放大,并且未按预期显示。我已经更新了所有的android SDK,但是输出没有变化。我应该做其他事情来解决android中的这个问题吗?

我的代码:

   let payLoad = {
  "notification": {
    "title": "The push notification title",
    "actions": [
      {
        "action": "actionOne",
        "title": "Action One"
      }
    ],
    "body": "The is the body which will be shown on notification",
    "dir": "auto",
    "icon": "path to icon",
    "badge": "path to badge",
    "lang": "en",
    "renotify": true,
    "requireInteraction": true,
    "tag": 926796012340920300,
    "vibrate": [300, 100, 400],
    "data": {
      // this object can contain arbitrary info
    }
  }
}

如果我将版本从9.0(pie)降级到8.1(oreo),则UI将按预期工作。为什么在9.0中无法正常工作?

1 个答案:

答案 0 :(得分:2)

在Android 9.0(API 28)之后,您应该提供更多大小的图像资产。

解决方案1:

Drawable xxx-hdpi xxx-mdpi 等中放置相同的图像。

解决方案2:

您可以在不同版本的Android中处理不同的逻辑。

在ImageEntryRenderer中

private BitmapDrawable GetDrawable(string imageEntryImage)
{
   int resID = Resources.GetIdentifier(imageEntryImage, "drawable", this.Context.PackageName);
   var drawable = ContextCompat.GetDrawable(this.Context, resID);
   var bitmap = ((BitmapDrawable)drawable).Bitmap;

   if(Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.P)
   {
     return new BitmapDrawable(Resources, Bitmap.CreateScaledBitmap(bitmap, element.ImageWidth * 2, element.ImageHeight * 2, true));
   }

   return new BitmapDrawable(Resources, Bitmap.CreateScaledBitmap(bitmap, element.ImageWidth , element.ImageHeight , true));
}