【问题标题】:svg-android imageview not workingsvg-android imageview 不工作
【发布时间】:2021-02-15 01:19:40
【问题描述】:

我需要在我的 android 应用中显示 svg 文件。 svg-android 似乎是唯一一个有任何文档的库,因此是我的第一种方法。唯一可用的示例演示了如何创建图像视图附加 svg 图像并将其附加到主内容视图。但是,我希望 svg 文件显示在我已经定义的 RelativeLayout 上。我尝试了这样的实现:

ImageView imageView = new ImageView(this); 
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.logo); 
imageView.setImageDrawable(svg.createPictureDrawable());
RelativeLayout home_header = (RelativeLayout) findViewById(R.id.home_header);
home_header.addView(imageView);

一切看起来都很好(没有警告/错误)但是当我测试应用程序日志猫报告时:

05-27 11:25:43.940: I/Adreno200-EGLSUB(28492): <ConfigWindowMatch:2078>: Format RGBA_8888.
05-27 11:25:43.950: E/(28492): Can't open file for reading
05-27 11:25:43.960: E/(28492): Can't open file for reading

我已验证以下内容: - 文件未在任何其他程序中打开 - 文件格式正确

我在这里缺少什么?对可能发生的事情有什么建议吗?

【问题讨论】:

  • 更新:似乎与 svg-android 无关,而是与 android 本身有关。我的模拟器上有一个带有上述代码的工作示例……但问题仍然存在于我的 Galaxy s3 上。任何加载和读取 svg 文件的尝试都会导致“无法打开文件进行读取”错误。

标签: android vector svg imageview native


【解决方案1】:

android:hardwareAccelerated="false" 将禁用整个活动的硬件渲染。另一种方法可能是只使用:

imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

应该只为该视图禁用它。

PS。如果您正在寻找具有更好文档(和更多功能)的 SVG 库,请尝试我的:http://code.google.com/p/androidsvg/

【讨论】:

  • +1,该库几乎可以使用 :) - 我正在尝试,但有很多不良影响
  • 您遇到问题的是 svg-android 还是 AndroidSVG?如果是后者,请给我反馈,我会尽我所能。
  • 它是你的,另一个没用 :) 主要问题之一是:加载 svg 需要相对大量的时间,渲染到画布所需的时间几乎相同。它依赖于设备和 svg,但我的 svg 将 gui 锁定 2-5 秒甚至更长时间。我也可以得到 ANR。尝试将渲染图像保存到 SD 卡以便下次从那里加载。文本元素没有得到很好的支持,并且在路径或其他元素中发现了一些错误,但我现在已经更改了 svg 文件格式。在发送功能请求列表之前,我会先做我能做的事情:)
  • 对我来说没有工作的“软件”我必须使用:imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  • 这很奇怪。我不明白为什么它在那里有“软件”。我一定是脑子里放了个屁。现在修好了。
【解决方案2】:

经过一些调试并将模拟器与本机应用程序进行比较后,我发现“无法打开文件进行读取”与未显示的 svg 文件无关。相反,它与硬件加速有关。我必须在清单中设置以下内容

android:hardwareAccelerated="false" 

问题解决了。浪费时间。脑筋急转弯。

【讨论】:

    【解决方案3】:

    现在有另一个选项不需要android:hardwareAccelerated="false" 使用这个库 - https://github.com/wnafee/vector-compat (api 14+)

    android {
        // use version 22 or higher
        buildToolsVersion "22.0.1"
        ...
    }
    dependencies {
        compile 'com.wnafee:vector-compat:1.0.5'
        ...
    }
    

    并创建一个使用矢量兼容类的自定义 ImageView 类 -

    public class SvgImageView extends ImageView {        
        private Drawable icon;
    
    
        public SvgImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            TypedArray ta = context.obtainStyledAttributes(attrs,
                    R.styleable.button_left, 0, 0);
    
            try {
                int resId = ta.getResourceId(R.styleable.button_left_b_icon, -1);            
                if (resId != -1) {
                    icon = ResourcesCompat.getDrawable(this.getContext(), resId);
    
                }
    
    
            } finally {
                ta.recycle();
            }
    
    
            if (icon != null) {
                setImage(icon);
            }
    
        }   
    
        public void setImage(Drawable icon) {
            SvgImageView.this.setImageDrawable(icon);
    
        }
    
    
    
    }
    

    矢量图像示例 -

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:width="@dimen/logo_dimen"
        android:height="@dimen/logo_dimen"
        android:viewportWidth="@integer/view_port_dimen_logo"
        android:viewportHeight="@integer/view_port_dimen_logo"
    
        app:vc_viewportWidth="@integer/view_port_dimen_logo"
        app:vc_viewportHeight="@integer/view_port_dimen_logo">
        <group
            android:name="rotationGroup"
            android:pivotX="0"
            android:pivotY="0"
            android:rotation="0">
    
            <path
                android:name="v"
                android:fillColor="@color/white"
                android:pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z"
                app:vc_fillColor="@color/white"
                app:vc_pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z" />
    
        </group>
    </vector>
    

    示例 -

    <packagename.SvgImageView     
    
                app:b_icon="@drawable/google_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView3" />
    

    【讨论】:

      【解决方案4】:

      使用 androidx.appcompat.widget.AppCompatImageView 而不是 ImageView 对我有用。

      【讨论】:

        【解决方案5】:

        AppCompatImageView

        来自包装

        android.support.v7.widget

        检查这个post,我已经给出了使用svg的所有细节。根据我的经验,您可以在 Android 中完美地使用 svg。

        优点:

        1. 没有第三方库(需要官方的android支持库)
        2. gradle文件没有变化
        3. 对所有 `ImageViews` 使用 `android:src` 而不是 'app:srcCompat` 用于 svg 和 `android:src` 用于其他图像。
        4. 无需使用 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);在 BaseActivity 的静态块中。

        【讨论】:

        • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
        • @VivekMishra 简单地使用 AppCompatImageView 而不是 ImageView 并且什么都不做。一切都写在这个答案中。该链接包含 rightDrawable 和 leftDrawable 信息以及在这种情况下如何使用 svg。这个问题没有直接问到哪个。
        猜你喜欢
        • 1970-01-01
        • 2022-01-24
        • 2017-06-24
        • 1970-01-01
        • 1970-01-01
        • 2015-12-25
        • 2015-06-06
        • 2016-02-24
        • 1970-01-01
        相关资源
        最近更新 更多