mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
Glide封装
This commit is contained in:
parent
097a194931
commit
23ecb48057
@ -43,7 +43,7 @@ kapt {
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
|
||||
implementation 'androidx.core:core-ktx:1.0.2'
|
||||
implementation 'androidx.appcompat:appcompat:1.0.2'
|
||||
@ -81,6 +81,9 @@ dependencies {
|
||||
implementation 'com.squareup.okhttp3:logging-interceptor:3.14.0'//
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
|
||||
|
||||
//Glide
|
||||
implementation 'com.github.bumptech.glide:glide:4.8.0'
|
||||
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'androidx.test:runner:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
|
||||
|
@ -8,7 +8,7 @@ import androidx.databinding.ViewDataBinding
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView
|
||||
|
||||
abstract class BaseActivity<BD : ViewDataBinding, VM : ViewModel> : AppCompatActivity() {
|
||||
abstract class BaseActivity<BD : ViewDataBinding, VM : ViewModel?> : AppCompatActivity() {
|
||||
|
||||
protected lateinit var dataBinding: BD
|
||||
private set
|
||||
|
259
app/src/main/java/io/legado/app/help/ImageLoader.kt
Normal file
259
app/src/main/java/io/legado/app/help/ImageLoader.kt
Normal file
@ -0,0 +1,259 @@
|
||||
package io.legado.app.help
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.net.Uri
|
||||
import android.widget.ImageView
|
||||
import androidx.annotation.DrawableRes
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.RequestBuilder
|
||||
import com.bumptech.glide.RequestManager
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
||||
import com.bumptech.glide.load.resource.bitmap.*
|
||||
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
|
||||
import com.bumptech.glide.load.resource.gif.GifDrawable
|
||||
import com.bumptech.glide.request.RequestOptions
|
||||
import com.bumptech.glide.request.transition.Transition
|
||||
import java.io.File
|
||||
|
||||
object ImageLoader {
|
||||
|
||||
fun load(context: Context, url: String?): ImageLoadBuilder<String> {
|
||||
return ImageLoadBuilder(context, url)
|
||||
}
|
||||
|
||||
fun load(context: Context, @DrawableRes resId: Int?): ImageLoadBuilder<Int> {
|
||||
return ImageLoadBuilder(context, resId)
|
||||
}
|
||||
|
||||
fun load(context: Context, file: File?): ImageLoadBuilder<File> {
|
||||
return ImageLoadBuilder(context, file)
|
||||
}
|
||||
|
||||
fun load(context: Context, uri: Uri?): ImageLoadBuilder<Uri> {
|
||||
return ImageLoadBuilder(context, uri)
|
||||
}
|
||||
|
||||
fun load(context: Context, drawable: Drawable?): ImageLoadBuilder<Drawable> {
|
||||
return ImageLoadBuilder(context, drawable)
|
||||
}
|
||||
|
||||
fun load(context: Context, bitmap: Bitmap?): ImageLoadBuilder<Bitmap> {
|
||||
return ImageLoadBuilder(context, bitmap)
|
||||
}
|
||||
|
||||
fun load(context: Context, bytes: ByteArray?): ImageLoadBuilder<ByteArray> {
|
||||
return ImageLoadBuilder(context, bytes)
|
||||
}
|
||||
|
||||
|
||||
fun with(context: Context): ImageLoadBuilder<Any> {
|
||||
return ImageLoadBuilder(context)
|
||||
}
|
||||
|
||||
fun clear(imageView: ImageView) {
|
||||
with(imageView.context).clear(imageView)
|
||||
}
|
||||
|
||||
class ImageLoadBuilder<S> constructor(context: Context, private var source: S? = null) {
|
||||
|
||||
private val manager: RequestManager = Glide.with(context)
|
||||
private var requestOptions: RequestOptions = RequestOptions()
|
||||
private var crossFade: Boolean = false
|
||||
private var noCache: Boolean = false
|
||||
|
||||
fun load(source: S): ImageLoadBuilder<S> {
|
||||
this.source = source
|
||||
return this
|
||||
}
|
||||
|
||||
fun toRound(corner: Int): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.transform(RoundedCorners(corner))
|
||||
return this
|
||||
}
|
||||
|
||||
fun toCropRound(corner: Int): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.transforms(CenterCrop(), RoundedCorners(corner))
|
||||
return this
|
||||
}
|
||||
|
||||
fun toCircle(): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.transform(CircleCrop())
|
||||
return this
|
||||
}
|
||||
|
||||
fun centerInside(): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.transform(CenterInside())
|
||||
return this
|
||||
}
|
||||
|
||||
fun fitCenter(): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.transform(FitCenter())
|
||||
return this
|
||||
}
|
||||
|
||||
fun centerCrop(): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.transform(CenterCrop())
|
||||
return this
|
||||
}
|
||||
|
||||
fun crossFade(): ImageLoadBuilder<S> {
|
||||
crossFade = true
|
||||
return this
|
||||
}
|
||||
|
||||
fun noCache(): ImageLoadBuilder<S> {
|
||||
noCache = true
|
||||
return this
|
||||
}
|
||||
|
||||
fun placeholder(placeholder: Drawable): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.placeholder(placeholder)
|
||||
return this
|
||||
}
|
||||
|
||||
fun placeholder(@DrawableRes resId: Int): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.placeholder(resId)
|
||||
return this
|
||||
}
|
||||
|
||||
fun error(drawable: Drawable): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.error(drawable)
|
||||
return this
|
||||
}
|
||||
|
||||
fun error(@DrawableRes resId: Int): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.error(resId)
|
||||
return this
|
||||
}
|
||||
|
||||
fun override(width: Int, height: Int): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.override(width, height)
|
||||
return this
|
||||
}
|
||||
|
||||
fun override(size: Int): ImageLoadBuilder<S> {
|
||||
requestOptions = requestOptions.override(size)
|
||||
return this
|
||||
}
|
||||
|
||||
fun clear(imageView: ImageView) {
|
||||
manager.clear(imageView)
|
||||
}
|
||||
|
||||
fun downloadOnly(target: ImageViewTarget<File>) {
|
||||
manager.downloadOnly().load(source).into(Target(target))
|
||||
}
|
||||
|
||||
fun setAsDrawable(imageView: ImageView) {
|
||||
asDrawable().into(imageView)
|
||||
}
|
||||
|
||||
fun setAsDrawable(target: ImageViewTarget<Drawable>) {
|
||||
asDrawable().into(Target(target))
|
||||
}
|
||||
|
||||
fun setAsBitmap(imageView: ImageView) {
|
||||
asBitmap().into(imageView)
|
||||
}
|
||||
|
||||
fun setAsBitmap(target: ImageViewTarget<Bitmap>) {
|
||||
asBitmap().into(Target(target))
|
||||
}
|
||||
|
||||
fun setAsGif(imageView: ImageView) {
|
||||
asGif().into(imageView)
|
||||
}
|
||||
|
||||
fun setAsGif(target: ImageViewTarget<GifDrawable>) {
|
||||
asGif().into(Target(target))
|
||||
}
|
||||
|
||||
fun setAsFile(imageView: ImageView) {
|
||||
asFile().into(imageView)
|
||||
}
|
||||
|
||||
fun setAsFile(target: ImageViewTarget<File>) {
|
||||
asFile().into(Target(target))
|
||||
}
|
||||
|
||||
private fun asDrawable(): RequestBuilder<Drawable> {
|
||||
var builder: RequestBuilder<Drawable> = ensureOptions(manager.asDrawable().load(source))
|
||||
|
||||
if (crossFade) {
|
||||
builder = builder.transition(DrawableTransitionOptions.withCrossFade())
|
||||
}
|
||||
|
||||
return builder
|
||||
}
|
||||
|
||||
private fun asBitmap(): RequestBuilder<Bitmap> {
|
||||
var builder: RequestBuilder<Bitmap> = ensureOptions(manager.asBitmap().load(source))
|
||||
|
||||
if (crossFade) {
|
||||
builder = builder.transition(BitmapTransitionOptions.withCrossFade())
|
||||
}
|
||||
|
||||
return builder
|
||||
}
|
||||
|
||||
private fun asGif(): RequestBuilder<GifDrawable> {
|
||||
var builder: RequestBuilder<GifDrawable> = ensureOptions(manager.asGif().load(source))
|
||||
|
||||
if (crossFade) {
|
||||
builder = builder.transition(DrawableTransitionOptions.withCrossFade())
|
||||
}
|
||||
|
||||
return builder
|
||||
}
|
||||
|
||||
private fun asFile(): RequestBuilder<File> {
|
||||
return manager.asFile().load(source)
|
||||
}
|
||||
|
||||
private fun <ResourceType> ensureOptions(builder: RequestBuilder<ResourceType>): RequestBuilder<ResourceType> {
|
||||
return builder.apply(requestOptions.diskCacheStrategy(if (noCache) DiskCacheStrategy.NONE else DiskCacheStrategy.RESOURCE))
|
||||
}
|
||||
|
||||
private inner class Target<R> constructor(private val target: ImageViewTarget<R>) :
|
||||
com.bumptech.glide.request.target.ImageViewTarget<R>(target.view) {
|
||||
|
||||
init {
|
||||
if (this.target.waitForLayout) {
|
||||
waitForLayout()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResourceReady(resource: R, transition: Transition<in R>?) {
|
||||
if (!target.onResourceReady(resource)) {
|
||||
super.onResourceReady(resource, transition)
|
||||
}
|
||||
}
|
||||
|
||||
override fun setResource(resource: R?) {
|
||||
target.setResource(resource)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ImageViewTarget<R>(val view: ImageView) {
|
||||
internal var waitForLayout: Boolean = false
|
||||
|
||||
fun waitForLayout(): ImageViewTarget<R> {
|
||||
waitForLayout = true
|
||||
return this
|
||||
}
|
||||
|
||||
fun setResource(resource: R?) {
|
||||
|
||||
}
|
||||
|
||||
fun onResourceReady(resource: R?): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -14,7 +14,7 @@ import java.util.*
|
||||
|
||||
internal class Request : OnRequestPermissionsResultCallback {
|
||||
|
||||
val requestTime: Long
|
||||
internal val requestTime: Long
|
||||
private var requestCode: Int = TYPE_REQUEST_PERMISSION
|
||||
private var source: RequestSource? = null
|
||||
private var permissions: ArrayList<String>? = null
|
||||
|
@ -15,7 +15,7 @@ internal object RequestManager : OnPermissionsResultCallback {
|
||||
}
|
||||
|
||||
private val isCurrentRequestInvalid: Boolean
|
||||
get() = request?.let { System.currentTimeMillis() - it.startTime > 5 * 1000L } ?: true
|
||||
get() = request?.let { System.currentTimeMillis() - it.requestTime > 5 * 1000L } ?: true
|
||||
|
||||
init {
|
||||
RequestPlugins.setOnPermissionsResultCallback(this)
|
||||
@ -52,7 +52,7 @@ internal object RequestManager : OnPermissionsResultCallback {
|
||||
|
||||
requests?.let {
|
||||
request = if (it.empty()) null else it.pop()
|
||||
request?.let { handler.post(requestRunnable) }
|
||||
request?.let { handler.post(requestRunnable) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ import io.legado.app.help.storage.Backup
|
||||
import io.legado.app.help.storage.Restore
|
||||
import io.legado.app.ui.replacerule.ReplaceRuleActivity
|
||||
import io.legado.app.ui.search.SearchActivity
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.getViewModel
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.android.synthetic.main.app_bar_main.*
|
||||
import org.jetbrains.anko.startActivity
|
||||
|
Loading…
Reference in New Issue
Block a user