如何在android中正确使用SVG?

时间:2016-03-16 04:54:07

标签: android svg androidsvg

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

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

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

错误:(163)错误:drawable [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'
}

1 个答案:

答案 0 :(得分:1)

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

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

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