Skip to content

Commit

Permalink
1.修改ImageLoaderManager的创建方式,通过传入一个ImageLoader的实现类的方式传入进去
Browse files Browse the repository at this point in the history
2.删除Fresco getConfig()
  • Loading branch information
keep2iron committed Oct 29, 2018
1 parent 9283836 commit b661928
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ class FrescoImageLoader : ImageLoader {
private lateinit var config: ImagePipelineConfig
private val handler: Handler = Handler()

override fun getConfig(): Any {
return config
}

override fun init(context: Application) {
val createMemoryCacheParams = {
val maxHeapSize = Runtime.getRuntime().maxMemory().toInt()
Expand Down Expand Up @@ -210,39 +206,19 @@ class FrescoImageLoader : ImageLoader {
val dataSource = imagePipeline.fetchDecodedImage(request.build(), context.applicationContext)
dataSource.subscribe(object : BaseBitmapDataSubscriber() {
override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage>>?) {
Looper.getMainLooper().thread
handler.post {
onGetBitmap(null)
}
}

override fun onNewResultImpl(bitmap: Bitmap?) {
val copyBitmap = bitmap?.copy(Bitmap.Config.ARGB_8888, true)
handler.post {
onGetBitmap(copyBitmap)
onGetBitmap(bitmap)
}
}
}, ExecutorManager.cacheExecutor)
}

override fun getImage(context: Context,
url: String,
options: ImageLoaderOptions,
onGetBitmap: (CloseableImageWrapper?) -> Unit) {
val request = buildImageRequest(Uri.parse(url), options)
val imagePipeline = Fresco.getImagePipeline()
val dataSource = imagePipeline.fetchDecodedImage(request.build(), context.applicationContext)
dataSource.subscribe(object : BaseDataSubscriber<CloseableReference<CloseableImage>>() {
override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage>>) {
onGetBitmap(null)
}

override fun onNewResultImpl(dataSource: DataSource<CloseableReference<CloseableImage>>) {
onGetBitmap(CloseableImageWrapper(dataSource.result))
}
}, ExecutorManager.cacheExecutor)
}

private fun setImageLoaderOptions(options: ImageLoaderOptions, draweeView: SimpleDraweeView) {
val builder = GenericDraweeHierarchyBuilder(draweeView.context.resources)
val hierarchy = builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ interface ImageLoader {
// blurRadius: Int)


/**
* 获取网络url指定的Bitmap
*/
fun getImage(context: Context,
url: String,
options: ImageLoaderOptions = ImageLoaderOptions.getDefaultOption(),
onGetBitmap: (CloseableImageWrapper?) -> Unit)
// /**
// * 获取网络url指定的Bitmap
// */
// fun getImage(context: Context,
// url: String,
// options: ImageLoaderOptions = ImageLoaderOptions.getDefaultOption(),
// onGetBitmap: (Bitmap) -> Unit)

fun getBitmap(context: Context, url: String, options: ImageLoaderOptions = ImageLoaderOptions.getDefaultOption(), onGetBitmap: (Bitmap?) -> Unit)

Expand All @@ -65,9 +65,4 @@ interface ImageLoader {
* 清除所有缓存
*/
fun clearAllCache()

/**
* 获取配置
*/
fun getConfig(): Any
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
package io.github.keep2iron.pineapple

import android.app.Application
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri

/**
*
* @author keep2iron <a href="http://keep2iron.github.io">Contract me.</a>
* @version 1.0
* @since 2018/06/25 19:42
*
* 相当于ImageLoader的代理
*/
class ImageLoaderManager private constructor() {

companion object {
@JvmStatic
private val instance: ImageLoader by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
FrescoImageLoader()
}

@JvmStatic
fun getImageLoader(): ImageLoader {
return instance
}
class ImageLoaderManager(private val imageLoader: ImageLoader, application: Application) : ImageLoader {

init {
this.init(application)
}

override fun init(context: Application) {
imageLoader.init(context)
}

override fun showImageView(imageView: MiddlewareView, url: String, options: ImageLoaderOptions) {
imageLoader.showImageView(imageView, url, options)
}

override fun showImageView(imageView: MiddlewareView, uri: Uri, options: ImageLoaderOptions) {
imageLoader.showImageView(imageView, uri, options)
}

override fun getBitmap(context: Context, url: String, options: ImageLoaderOptions, onGetBitmap: (Bitmap?) -> Unit) {
imageLoader.getBitmap(context, url, options, onGetBitmap)
}

override fun cleanMemory(context: Context) {
imageLoader.cleanMemory(context)
}

override fun pause(context: Context) {
imageLoader.pause(context)
}

override fun resume(context: Context) {
imageLoader.pause(context)
}

override fun clearAllCache() {
imageLoader.clearAllCache()
}
}

0 comments on commit b661928

Please sign in to comment.