【问题标题】:How do I use SVGs in android correctly?如何在 android 中正确使用 SVG?
【发布时间】:2016-07-01 19:48:10
【问题描述】:

我知道对 SVG 的官方支持直到 api 级别 23 才开始,但是我如何在最小 sdk 为 17 的应用中使用它?

我之前有过它的工作,但我不知道发生了什么变化。我正在使用 AndroidSVG 库。

我在尝试构建生产 APK 时收到此错误

错误:(163) 错误:预期资源类型为可绘制 [ResourceType]

这是我在其中遇到此错误的 150 行代码之一

socialTypeImage.setImageResource(R.raw.icon_other_blue);

我不知道要提供什么其他信息,所以如果您需要更多信息,请告诉我。

谢谢

我不知道我的问题是否重复,但如果是,这些解决方案都不起作用。

buildscript {
repositories {
    mavenCentral()
    jcenter()
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    maven { url 'http://repository.codehaus.org' }
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.0-alpha1'
    //classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.14.7'
    classpath 'com.google.code.ksoap2-android:ksoap2-android:3.1.1'
    classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.2'
    classpath 'io.fabric.tools:gradle:1.+'
}
}

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
maven { url "https://repo.commonsware.com.s3.amazonaws.com" }
maven { url "http://repository.codehaus.org" }
maven { url 'https://maven.fabric.io/public' }
}

import groovyx.net.http.HTTPBuilder

def getBuildNumber(projectName) {
    def http = new HTTPBuilder('http://eco-crossbar-620.appspot.com')
try {
    def resp = http.get(path: "/${projectName}")
    println "NEW BUILD NUMBER: ${resp.BODY}"
    resp.BODY
} catch (ignored) {
    println "ERROR RETRIEVING BUILD NUMBER"
    0
}
}

def getWorkingBranch() {
def workingBranch = "build"
try {
    workingBranch = """git --git-dir=${rootDir}/.git
                           --work-tree=${rootDir}/
                           rev-parse --abbrev-ref HEAD""".execute().text.trim()
} catch (ignored) {
    // git unavailable or incorrectly configured
}
println "Working branch: " + workingBranch
return workingBranch.replaceAll("/", "-")
}

android {
useLibrary 'org.apache.http.legacy'
compileSdkVersion 23
buildToolsVersion '23.0.2'

def build = getBuildNumber("lookcounter-android")

defaultConfig {
    applicationId "com.lookcounter"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 67
    versionName "0.1.$versionCode"
    vectorDrawables.useSupportLibrary = true
    buildConfigField "String", "DEFAULT_USERNAME", "\"\""
    buildConfigField "String", "DEFAULT_PASSWORD", "\"\""
    resValue "string", "base64_encoded_public_key", "key"
}

buildTypes {
    release {
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", "DEFAULT_USERNAME", "\"\""
        buildConfigField "String", "DEFAULT_PASSWORD", "\"\""
        debuggable false
        jniDebuggable false
        renderscriptDebuggable false
        pseudoLocalesEnabled false
    }
    debug {
        minifyEnabled false
        debuggable true
        jniDebuggable true
        renderscriptDebuggable true
        pseudoLocalesEnabled true
        zipAlignEnabled false
    }
}

signingConfigs {
    dev {
        storeFile file("../LookCounter.keystore")
        storePassword "izpa55word"
        keyAlias "lookcounterkey"
    }
    prod {
        storeFile file("../LookCounter.keystore")
        keyAlias "lookcounterprodkey"
    }
}

productFlavors {
    production {
        buildConfigField "boolean", "DEV_BUILD", "false"
        resValue "string", "app_name", "Lookcounter"
        signingConfig signingConfigs.prod
        versionCode 2
        minSdkVersion 17
        targetSdkVersion 23
    }
    dev {
        buildConfigField "boolean", "DEV_BUILD", "true"
        resValue "string", "app_name", "(DEV) Lookcounter"
        signingConfig signingConfigs.dev            
        applicationId 'com.lookcounter'
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
}

android.applicationVariants.all { variant ->
    if (variant.buildType.name.equals("release")) {
        variant.assemble.doLast {
            def apkName
            def dirName = System.getProperty("user.home") + "/Desktop/lookcounter/apk"
            if (variant.name.contains("production")) {
                dirName += "PlayStore"
                apkName = "Lookcounter_v${versionCode}_PlayStore_${versionName}.apk"
            } else {
                dirName += "dev"
                def branchName = getWorkingBranch()
                if (branchName.equals("develop")) {
                    apkName = "Lookcounter_v${versionCode}_DEV_${build}.apk"
                } else {
                    apkName = "Lookcounter_v${versionCode}_DEV_${branchName}_${build}.apk"
                }
            }
            copy {
                def source = variant.outputs.get(0).getOutputFile()
                from source
                into dirName
                include source.name
                rename source.name, apkName
                println "Output APK copied to $dirName/$apkName"
            }
        }
    }
}
}

repositories {
maven {
    url "https://jitpack.io"
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//    compile "com.dev.sacot41:scviewpager:0.0.4"
compile project(':androidSVG')
compile files('../libraries/PriorityDeque-1.0.jar')
compile project(':cropper')
testCompile 'junit:junit:4.12'
// Possibly Keep
compile('com.doomonafireball.betterpickers:library:1.6.0') {
    exclude group: 'com.android.support', module: 'support-v4'
}
// KEEP
compile('com.nostra13.universalimageloader:universal-image-loader:1.9.5') {
    exclude group: 'com.android.support', module: 'support-v7'
}
compile('com.twitter.sdk.android:tweet-composer:0.7.3@aar') {
    transitive = true;
}
compile('com.crashlytics.sdk.android:answers:1.3.6@aar') {
    transitive = true;
}
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
    transitive = true;
}
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:support-v4:23.2.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
compile 'com.jakewharton:butterknife:5.1.2'
compile 'org.apache.commons:commons-lang3:3.1'
compile 'com.github.clans:fab:1.6.2'
compile 'com.facebook.android:facebook-android-sdk:3.21.1'
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.google.code.ksoap2-android:ksoap2-android:3.4.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:support-vector-drawable:23.2.0'
compile 'com.android.support:animated-vector-drawable:23.2.0'
}

【问题讨论】:

  • 看起来它需要一个可绘制对象,而您提供的是 raw 资源。 AndroidSVG 允许您解析 SVG 并将其渲染到画布,但不能将它们分配给 ImageView,afaik。 SVGAndroid 让您更接近。
  • @323go 我知道它要求绘制,但据我了解 AndroidSVG 和/或 api 23 允许使用 svg 和 gradle 在构建时创建适当的 png
  • official support for SVG didn't start until api level 23从未真正开始。 VectorDrawables 不是 SVGs。他们使用SVG 定义的简化子集
  • 你的 proguard 文件有什么奇怪的地方吗?否则,也许尝试在您的发布版本中一次更改一件事以匹配调试版本,然后查看它是否开始工作。

标签: android svg androidsvg


【解决方案1】:

您不能将 SVG 直接用作资源。 Android 资源加载系统不知道 SVG 是什么。您需要使用的是:

SVG svg = SVG.getFromResource(R.raw.icon_other_blue);
socialTypeImage.setImageDrawable(new PictureDrawable(svg.toPictureDrawable()));

或者您可以使用提供的SVGImageView 小部件类。 Documentation here.

【讨论】:

  • 我实际上使用的是 SvgImageView。对不起,我继承了这段代码,无论如何我都不是安卓大师。您能向我解释一下如何进行调试构建,它工作得很好,没有问题,但是一旦我尝试发布构建,它就会出现问题?
  • 好的。我对您的错误感到困惑,因为这不是我在使用 AndroidSVG 时期望得到的错误。您的调试和发布版本有什么区别。您可以将您的 build.gradle 文件发布到问题中吗?
  • 好的,我添加了 build.gradle 我删除了一些敏感信息,但没有影响构建。
猜你喜欢
  • 2018-06-17
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
相关资源
最近更新 更多