【问题标题】:Why won't my SVG image display on my Galaxy Nexus?为什么我的 SVG 图像无法在 Galaxy Nexus 上显示?
【发布时间】:2013-12-04 02:10:12
【问题描述】:

我正在尝试使用 svg-android 库将一张图片作为另一张图片的背景。当我在三星 Galaxy Nexus(4.3,Sprint)上查看图像时,背景图像不显示。当我在模拟器上查看它时,它是正确的。我什至删除了前景,这保持不变。以下是 XML 布局的关键部分:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.kd7uiy.hamfinder.SvgView
        android:id="@+id/svgImage"
        android:src="@raw/paper"
        android:layout_alignLeft="@+id/scroll"
        android:layout_alignRight="@id/scroll"
        android:layout_alignTop="@id/scroll"
        android:layout_above="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ScrollView
            android:id="@+id/scroll"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/buttons"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:background="@android:color/transparent"
            android:layout_below="@id/adView">
        <TableLayout
            android:id="@+id/tableView"
                android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:shrinkColumns="1"
            android:stretchColumns="1"
        />
    </ScrollView>
</RelativeLayout>

还有自定义类。请注意,我尝试了来自svg-android 的提示,但没有运气:

public class SvgView extends View {

    Picture picture;

    long startTime;
    float scaleFactor;

    public SvgView(Context context,AttributeSet attrs) {
        super(context,attrs);
        setImage(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android","src", 0));
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
        {
            disableHardwareAcceleration();
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void disableHardwareAcceleration()
    {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    public SvgView(Context context) {
        super(context);

    }

    public void setImage(int img_resource)
    {
        // Parse the SVG file from the resource
        SVG svg = SVGParser.getSVGFromResource(getResources(),img_resource);
        picture = svg.getPicture();
    }

    public void onDraw(Canvas canvas) {
        if (picture!=null)
        {
            scaleFactor=Math.min((float)getHeight()/picture.getHeight(),(float)getWidth()/picture.getWidth());
            canvas.scale((float)scaleFactor,(float)scaleFactor);
            canvas.drawPicture(picture);
        }

    }
}

最后,这是来自我的 Galaxy Nexus 和模拟器的图像。注意背景的不同。

【问题讨论】:

标签: android svg-android


【解决方案1】:

事实证明,提供的提示是正确的,但我有一个小错字。硬件加速似乎是罪魁祸首。因为我没有任何复杂的东西,所以禁用它就可以了。我几乎有正确的解决方案,只是有错误的比较器。更改代码如下修复它:

public SvgView(Context context,AttributeSet attrs) {
    super(context,attrs);
    setImage(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android","src", 0));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        disableHardwareAcceleration();
    }
}

【讨论】:

  • 据我所知,您不需要在从 Honeycomb 开始的所有版本上禁用硬件加速。问题是某些绘图功能没有在某些操作系统版本上实现,但在 Kitkat 上它工作得很好。我不确定它不会造成任何问题的确切 API 版本,它甚至可能取决于渲染的 SVG。但对我来说,JELLY_BEAN_MR2 以上的一切似乎都运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 2021-06-24
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2016-10-01
  • 2020-08-29
相关资源
最近更新 更多