GithubHelp home page GithubHelp logo

glidedsl's Introduction

GlideDsl

glide 的封装

使用

allprojects {
  repositories {
    maven { url 'https://jitpack.io' }
  }
}

dependencies {
      implementation 'com.github.EspoirX:GlideDsl:vX.X.X'
}

简单使用:

先自己依赖 glide:

implementation "com.github.bumptech.glide:glide:$glide_version"

在 Application 里面给一下上下文:

 GlideDsl.init(this)
    .setImageUrlInterceptor() //可添加url拦截器,可以做一些转格式之类的操作

然后可以使用了(简单的用法示例代码见 MainActivity):

  imageView.loadImage(url)

如果加载的 resource 属于 Animatable,会自动调用 start()

配置加载参数:

  ImageView.loadImage(url){
    placeholder()      //占位,提供了 resId 和 drawable 两个方法
    error()            //错误占位,提供了 resId 和 drawable 两个方法
    decodeFormat()     //对应 Glide 的 format 配置
    centerCrop()       //显示模式,对应 centerCrop
    centerInside()     //显示模式,对应 centerInside
    fitCenter()        //显示模式,对应 fitCenter
    skipLocalCache()   //跳过缓存,下面几个都是
    skipNetCache()
    skipMemoryCache()
    skipOverride()     // 设置这个,就不会执行 override,即使设置了宽高
    circleCrop()       //圆形
    roundAngle()       //圆角
    maskColor()        //设置遮照颜色
    dontAnimate()      //不要动画
    blur()             //高斯模糊
    targetSize()       //设置宽高
    grayImage()        //彩色置灰
  }

如上,可以设置很多种基础配置。

加载回调

  1. 回调 drawable
imageView.loadImage(url){
    onDrawableSuccess { drawable ->
       //...
    }
    onLoadFailed { 
        //...
    }
}

也可以这样:

context.loadImage(url){
    onDrawableSuccess { drawable ->
        imageView.setImageDrawable(drawable)
    }
}
  1. 回调 bitmap,同理,不过需要调用 asBitmap
imageView.loadImage(url){
    asBitmap()
    onBitmapSuccess { drawable ->
       //...
    }
}

加载 gif

  imageView.loadImage(url){ asGif() }

asGif() 有个参数 getGifDrawable,默认 false,true 的话会在 onDrawableSuccess 里面得到 GifDrawable

加载 webp 动画

  imageView.loadImage(url){ webpGif() }

webp动画需要自己依赖解码库:

  implementation "com.zlc.glide:webpdecoder:2.0.4.12.0"

加载 svga

加载svga需要自己依赖svga库:

    implementation("com.github.yyued:SVGAPlayer-Android:2.6.1")

然后加载 svga 可选择用 Glide 去加载或者用 svga 自己的加载器加载,推荐用 glide 加载,需要自己依赖:

 // 手Y SVGA管理
   implementation('com.github.YvesCheung:SVGAGlidePlugin:4.13.3') {
        exclude group: 'com.github.yyued', module: 'SVGAPlayer-Android'
   }

然后使用:

  imageView.loadImage(url){ asSvga() }

默认使用的是 glide 加载,不想的话 asSvga(false) 即可。

回调的话也是一样:

context.loadImage(url) {
    asSvga() 
    onSvgaSuccess { entity, width, height, drawable ->
       //...
    }
}

给 svga 添加 key:

context.loadImage(svgaTitle) {
    asSvga()
    addSvgaText {
        key = ""     //key
        text = ""    //文案 
        colorString = "#FFFFFF"  //颜色
        textSize = 20f.sp2px  //字体大小
        typeface = FontCache.getTypeface("XXX.ttf", context) //字体
    }
    addSvgaText {} //可添加多个
    //...
    addSvgaImage{
        key = ""     //key
        url=""     //图片url
    }
    addSvgaImage {} //可添加多个
    //...
    onSvgaSuccess { entity, width, height, drawable ->
        svgaView?.setImageDrawable(drawable)
        svgaView?.startAnimation()
    }
}

svga 添加图片 key 时支持用 Bitmap

  addSvgaImageBitmap {
    key = "img_107"
    width = 94.dp2px
    height = 142.dp2px
    roundAngle = 20f.dp2px
    url = "https://www.surenxianqu.com/wp-content/uploads/2023/07/2023071114474362.png"
 }
 addSvgaImageBitmap {} //可添加多个
 //...

会自动根据url获取到 bitmap 并支持对 bitmap 进行宽高和圆角的变化
该功能是用用协程实现的,所以需要依赖:

  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"

glidedsl's People

Contributors

espoirx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.