移动屏幕上不会立即显示SVG图像

时间:2016-04-26 09:55:51

标签: c# android xamarin xamarin.android

我有以下SVG图片

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:id="@+id/content"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp">

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:gravity="center_horizontal">


        <XamSvg.SvgImageView
            android:id="@+id/icon"
            local:svg="res:images.Q317664"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </LinearLayout>

    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/drawableAutoSize"
        android:scaleType="fitCenter"
        android:background="#AAD39F" />
</LinearLayout>
</ScrollView>

我想显示此SVG但是当我运行此代码时,我的移动屏幕上没有显示SVG图像。当我触摸移动屏幕15 t0 20次然后它按预期显示。我该如何解决这个问题?
为什么手机屏幕上的图像会迟到?

using System;
using System.Linq;
using System.Reflection;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using XamSvg;
using Android.Graphics;
namespace App18
{
[Activity(Label = "App18", MainLauncher = true, Theme = "@android:style/Theme.Holo.Light.NoActionBar.Fullscreen")]
public class MainActivity : Activity
{
    int[] rawIds;
    int[] drawableViewIds;
    private string[] sharedNames;

    int currentId;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
         Setup.InitSvgLib();

        //Tells XamSvg in which assembly to search for svg when "res:" is used
         var assembly = typeof(MainActivity).Assembly;

        XamSvg.Shared.Config.ResourceAssembly = assembly;





        //Get all svg resource ids in the drawable folder
        rawIds = typeof(Resource.Drawable)
            .GetFields()
            .Where(f => f.IsLiteral)
            .Select(f => (int)f.GetRawConstantValue())
            .ToArray();

        //Get all drawing zones in the current layout
       drawableViewIds = typeof(Resource.Id)
            .GetFields()
            .Where(f => f.IsLiteral && f.Name.StartsWith("drawable"))
            .Select(f => (int)f.GetRawConstantValue())
           .ToArray();

        //Get all svg resources in the shared assembly
        sharedNames = assembly.GetManifestResourceNames().Where(n => n.EndsWith(".svg")).OrderBy(n => n).ToArray();

        //When clicked, change the svg source in all zones
        var contentView = FindViewById(Resource.Id.content);
        contentView.Click += (sender, e) =>
        {
            if(currentId<rawIds.Length)
                LoadImageTest(rawIds[currentId]);
            else
         {
       }

        };
        LoadImageTest(rawIds[currentId++]);

    }

    void LoadImageTest(int rawId)
    {
        foreach (var drawableId in drawableViewIds)
        {

            var v = FindViewById<ImageView>(drawableId);
           var drawable = SvgFactory.GetDrawable(Resources, rawId);
         v.SetImageDrawable(drawable);
        }
    }

}

}

1 个答案:

答案 0 :(得分:2)

您正在为Xamarin.Forms库使用XamSvg代码。请改为使用Xamarin.Android代码,因为您的项目定位于Android设备。以下是XamSvg component store Getting Started guide的代码:

  

Android 3.1+(api 12)

     

打开你的Android项目,打开nuget,然后添加&#34; XamSvg&#34;。 nuget包已由组件安装程序复制到本地缓存中。

     

在标准资源文件夹

下添加名为raw的文件夹      

将svg图像添加到此原始文件夹。确保他们有AndroidResource构建操作。

     

在Application或MainActivity类中,通过调用XamSvg.Setup.InitSvgLib()初始化Svg lib;这非常快,并将跨平台颜色助手与特定的Android版本绑定。

[Application]
public class MainApp : Application
{
    public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
        XamSvg.Setup.InitSvgLib();
    }
}
  

显示myimage.svg图片:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
        <XamSvg.SvgImageView
            local:svg="@raw/myimage"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
</RelativeLayout>