From 23ecb480574e3d5b3595f61f321cbf05baa616e0 Mon Sep 17 00:00:00 2001 From: Invinciblelee <1760316362@qq.com> Date: Fri, 31 May 2019 15:53:49 +0800 Subject: [PATCH] =?UTF-8?q?Glide=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 5 +- .../java/io/legado/app/base/BaseActivity.kt | 2 +- .../java/io/legado/app/help/ImageLoader.kt | 259 ++++++++++++++++++ .../io/legado/app/help/permission/Request.kt | 2 +- .../app/help/permission/RequestManager.kt | 4 +- .../io/legado/app/ui/main/MainActivity.kt | 2 +- 6 files changed, 268 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/ImageLoader.kt diff --git a/app/build.gradle b/app/build.gradle index c3d0fa13c..f3777b469 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index c85db5b5c..8e9072433 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -8,7 +8,7 @@ import androidx.databinding.ViewDataBinding import androidx.lifecycle.ViewModel import com.google.android.material.bottomnavigation.BottomNavigationView -abstract class BaseActivity : AppCompatActivity() { +abstract class BaseActivity : AppCompatActivity() { protected lateinit var dataBinding: BD private set diff --git a/app/src/main/java/io/legado/app/help/ImageLoader.kt b/app/src/main/java/io/legado/app/help/ImageLoader.kt new file mode 100644 index 000000000..370be11cc --- /dev/null +++ b/app/src/main/java/io/legado/app/help/ImageLoader.kt @@ -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 { + return ImageLoadBuilder(context, url) + } + + fun load(context: Context, @DrawableRes resId: Int?): ImageLoadBuilder { + return ImageLoadBuilder(context, resId) + } + + fun load(context: Context, file: File?): ImageLoadBuilder { + return ImageLoadBuilder(context, file) + } + + fun load(context: Context, uri: Uri?): ImageLoadBuilder { + return ImageLoadBuilder(context, uri) + } + + fun load(context: Context, drawable: Drawable?): ImageLoadBuilder { + return ImageLoadBuilder(context, drawable) + } + + fun load(context: Context, bitmap: Bitmap?): ImageLoadBuilder { + return ImageLoadBuilder(context, bitmap) + } + + fun load(context: Context, bytes: ByteArray?): ImageLoadBuilder { + return ImageLoadBuilder(context, bytes) + } + + + fun with(context: Context): ImageLoadBuilder { + return ImageLoadBuilder(context) + } + + fun clear(imageView: ImageView) { + with(imageView.context).clear(imageView) + } + + class ImageLoadBuilder 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 { + this.source = source + return this + } + + fun toRound(corner: Int): ImageLoadBuilder { + requestOptions = requestOptions.transform(RoundedCorners(corner)) + return this + } + + fun toCropRound(corner: Int): ImageLoadBuilder { + requestOptions = requestOptions.transforms(CenterCrop(), RoundedCorners(corner)) + return this + } + + fun toCircle(): ImageLoadBuilder { + requestOptions = requestOptions.transform(CircleCrop()) + return this + } + + fun centerInside(): ImageLoadBuilder { + requestOptions = requestOptions.transform(CenterInside()) + return this + } + + fun fitCenter(): ImageLoadBuilder { + requestOptions = requestOptions.transform(FitCenter()) + return this + } + + fun centerCrop(): ImageLoadBuilder { + requestOptions = requestOptions.transform(CenterCrop()) + return this + } + + fun crossFade(): ImageLoadBuilder { + crossFade = true + return this + } + + fun noCache(): ImageLoadBuilder { + noCache = true + return this + } + + fun placeholder(placeholder: Drawable): ImageLoadBuilder { + requestOptions = requestOptions.placeholder(placeholder) + return this + } + + fun placeholder(@DrawableRes resId: Int): ImageLoadBuilder { + requestOptions = requestOptions.placeholder(resId) + return this + } + + fun error(drawable: Drawable): ImageLoadBuilder { + requestOptions = requestOptions.error(drawable) + return this + } + + fun error(@DrawableRes resId: Int): ImageLoadBuilder { + requestOptions = requestOptions.error(resId) + return this + } + + fun override(width: Int, height: Int): ImageLoadBuilder { + requestOptions = requestOptions.override(width, height) + return this + } + + fun override(size: Int): ImageLoadBuilder { + requestOptions = requestOptions.override(size) + return this + } + + fun clear(imageView: ImageView) { + manager.clear(imageView) + } + + fun downloadOnly(target: ImageViewTarget) { + manager.downloadOnly().load(source).into(Target(target)) + } + + fun setAsDrawable(imageView: ImageView) { + asDrawable().into(imageView) + } + + fun setAsDrawable(target: ImageViewTarget) { + asDrawable().into(Target(target)) + } + + fun setAsBitmap(imageView: ImageView) { + asBitmap().into(imageView) + } + + fun setAsBitmap(target: ImageViewTarget) { + asBitmap().into(Target(target)) + } + + fun setAsGif(imageView: ImageView) { + asGif().into(imageView) + } + + fun setAsGif(target: ImageViewTarget) { + asGif().into(Target(target)) + } + + fun setAsFile(imageView: ImageView) { + asFile().into(imageView) + } + + fun setAsFile(target: ImageViewTarget) { + asFile().into(Target(target)) + } + + private fun asDrawable(): RequestBuilder { + var builder: RequestBuilder = ensureOptions(manager.asDrawable().load(source)) + + if (crossFade) { + builder = builder.transition(DrawableTransitionOptions.withCrossFade()) + } + + return builder + } + + private fun asBitmap(): RequestBuilder { + var builder: RequestBuilder = ensureOptions(manager.asBitmap().load(source)) + + if (crossFade) { + builder = builder.transition(BitmapTransitionOptions.withCrossFade()) + } + + return builder + } + + private fun asGif(): RequestBuilder { + var builder: RequestBuilder = ensureOptions(manager.asGif().load(source)) + + if (crossFade) { + builder = builder.transition(DrawableTransitionOptions.withCrossFade()) + } + + return builder + } + + private fun asFile(): RequestBuilder { + return manager.asFile().load(source) + } + + private fun ensureOptions(builder: RequestBuilder): RequestBuilder { + return builder.apply(requestOptions.diskCacheStrategy(if (noCache) DiskCacheStrategy.NONE else DiskCacheStrategy.RESOURCE)) + } + + private inner class Target constructor(private val target: ImageViewTarget) : + com.bumptech.glide.request.target.ImageViewTarget(target.view) { + + init { + if (this.target.waitForLayout) { + waitForLayout() + } + } + + override fun onResourceReady(resource: R, transition: Transition?) { + if (!target.onResourceReady(resource)) { + super.onResourceReady(resource, transition) + } + } + + override fun setResource(resource: R?) { + target.setResource(resource) + } + } + } + + abstract class ImageViewTarget(val view: ImageView) { + internal var waitForLayout: Boolean = false + + fun waitForLayout(): ImageViewTarget { + waitForLayout = true + return this + } + + fun setResource(resource: R?) { + + } + + fun onResourceReady(resource: R?): Boolean { + return false + } + + } + + +} diff --git a/app/src/main/java/io/legado/app/help/permission/Request.kt b/app/src/main/java/io/legado/app/help/permission/Request.kt index 948578e5b..ccd96741f 100644 --- a/app/src/main/java/io/legado/app/help/permission/Request.kt +++ b/app/src/main/java/io/legado/app/help/permission/Request.kt @@ -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? = null diff --git a/app/src/main/java/io/legado/app/help/permission/RequestManager.kt b/app/src/main/java/io/legado/app/help/permission/RequestManager.kt index ba4a339da..b3fede03f 100644 --- a/app/src/main/java/io/legado/app/help/permission/RequestManager.kt +++ b/app/src/main/java/io/legado/app/help/permission/RequestManager.kt @@ -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) } } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 8e9de7e29..9e20de414 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -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