Android WebP的使用 - Go语言中文社区

Android WebP的使用


参考:https://blog.csdn.net/m13984458297/article/details/70159464

1、webp介绍:

webp是Google于2010年提出了一种新的图片压缩格式 — WebP,给图片的优化提供了新的可能。

WebP为网络图片提供了无损和有损压缩能力,同时在有损条件下支持透明通道。
	据官方实验显示:无损WebP相比PNG减少26%大小;有损WebP在相同的SSIM
	(Structural Similarity Index,结构相似性)下相比JPEG减少25%~34%的大小;
	有损WebP也支持透明通道,大小通常约为对应PNG的1/3。
	
参考:https://blog.csdn.net/f2006116/article/details/53141207

2、github下载webp源码:

      github源码地址:https://github.com/EverythingMe/webp-android

3、android导入webp源码,编译生成.so可用文件:

        (1)、android studio直接open打开下载解压好的 webp-android-master
                    3.1
        (2)、由于源码是17年之前搞得,所以gradle需要修改下,不然会报错 

apply plugin: 'com.android.library'

buildscript {
    repositories {
        mavenCentral()
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1'
    }
}
allprojects {
    repositories {
        mavenCentral()
        google()
        jcenter()
        maven { url 'https://maven.google.com' }
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots'
        }
        maven { url "https://jitpack.io" }
    }
}

android {
    compileSdkVersion 26
    buildToolsVersion "28.0.2"

    defaultConfig {
        targetSdkVersion 26
        minSdkVersion 4
        versionCode 1
        versionName "1.0.4"
    }

    sourceSets {
        main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['src/main/libs/']
        }
    }

    archivesBaseName=getName()
    version='1.0.'+(System.getenv('BUILD_NUMBER') ? System.getenv('BUILD_NUMBER') : '0-LOCAL')

    //rename the aar to {archivesBaseName}-{version}.aar
//    libraryVariants.all { variant ->
//        variant.outputs.each { output ->
//            def outputFile = output.outputFile
//            if (outputFile != null && outputFile.name.endsWith('.aar')) {
//                def fileName = "${archivesBaseName}-${version}.aar"
//                output.outputFile = new File(outputFile.parent, fileName)
//            }
//        }
//    }
    libraryVariants.all { variant ->
        variant.outputs.all {
            def fileName = "webp${archivesBaseName}_${version}.aar"
            outputFileName = fileName
        }
    }
}

// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
    String ndkDir = android.ndkDirectory
    commandLine "$ndkDir/ndk-build", '-j8', '-C', file('src/main/jni').absolutePath
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}
task cleanExtra(type: Delete) {
    delete 'src/main/obj'
}
clean.dependsOn(cleanExtra)

group = 'me.everything'

apply plugin: 'com.jfrog.artifactory-upload'
apply plugin: 'android-maven'

        (3)、ndk的版本当前太新会导致编译不成功,我这里下载了一个旧版的来用
        
            # 下载旧版本的ndk来支持webp:https://developer.android.google.cn/ndk/downloads/older_releases.html
            1
            # 配置android中的local.properties改成下载的ndk路径:
              3.2
            # 配置环境变量:
             3.3
        (4)、terminal 中cd到jni路径执行命令如下:
            ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk NDK_APPLICATION_MK=./Application.mk

           参考:http://www.bubuko.com/infodetail-3256499.html
            4

4、android项目使用webp的.so文件:

        (1)、android项目,libs中放入生成的.so文件
        (2)、gradle中添加libs资源路径

android {
	...
	sourceSets {
		main {
			jniLibs.srcDirs = ['libs']//设置目标的so存放路径
		}
	}
}

        (3)、copy源码中的webp工具类:
                WebPDecoder.java/WebpImageView.java
                5
        (4)、布局中使用:

               4.3

5、注意的点:

(1)、android导入源码的时候可能会有错误,需要更新自己的gradle信息

(2)、webp的动图加载,图片兼容框架就只有 fresco 

编译的源码

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u010326875/article/details/103823718
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:40:01
  • 阅读 ( 1306 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢