248 lines
11 KiB
Kotlin
248 lines
11 KiB
Kotlin
package com.gh.common.util
|
|
|
|
import android.content.Context
|
|
import android.content.res.Resources
|
|
import android.graphics.drawable.Animatable
|
|
import android.graphics.drawable.ColorDrawable
|
|
import android.net.Uri
|
|
import android.support.annotation.DrawableRes
|
|
import android.support.v4.content.ContextCompat
|
|
import android.text.TextUtils
|
|
import com.facebook.common.executors.CallerThreadExecutor
|
|
import com.facebook.drawee.backends.pipeline.Fresco
|
|
import com.facebook.drawee.controller.BaseControllerListener
|
|
import com.facebook.drawee.controller.ControllerListener
|
|
import com.facebook.drawee.drawable.ScalingUtils
|
|
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder
|
|
import com.facebook.drawee.view.SimpleDraweeView
|
|
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber
|
|
import com.facebook.imagepipeline.image.ImageInfo
|
|
import com.facebook.imagepipeline.request.ImageRequest
|
|
import com.facebook.imagepipeline.request.ImageRequestBuilder
|
|
import com.gh.common.constant.Config
|
|
import com.gh.gamecenter.R
|
|
import com.gh.gamecenter.manager.UserManager
|
|
import com.gh.gamecenter.retrofit.Response
|
|
import com.lightgame.config.CommonDebug
|
|
import com.lightgame.download.FileUtils
|
|
import com.lightgame.utils.Utils
|
|
import org.json.JSONObject
|
|
import retrofit2.HttpException
|
|
import rx.Observable
|
|
import rx.Observer
|
|
import rx.android.schedulers.AndroidSchedulers
|
|
import rx.schedulers.Schedulers
|
|
import java.io.File
|
|
import java.net.HttpURLConnection
|
|
|
|
class ImageUtils private constructor() {
|
|
|
|
|
|
fun display(simpleDraweeView: SimpleDraweeView?, url: String?, listener: BaseControllerListener<ImageInfo>) {
|
|
simpleDraweeView?.controller = Fresco.newDraweeControllerBuilder()
|
|
.setUri(url)
|
|
.setControllerListener(listener)
|
|
.build()
|
|
}
|
|
|
|
// 自适应图片宽高
|
|
fun display(simpleDraweeView: SimpleDraweeView?, url: String?, width: Int) {
|
|
val listener = object : BaseControllerListener<ImageInfo>() {
|
|
override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?, animatable: Animatable?) {
|
|
if (imageInfo == null) {
|
|
return
|
|
}
|
|
val layoutParams = simpleDraweeView?.layoutParams
|
|
val scale = imageInfo.height.toFloat() / imageInfo.width.toFloat()
|
|
layoutParams?.height = (width * scale).toInt()
|
|
simpleDraweeView?.layoutParams = layoutParams
|
|
}
|
|
}
|
|
simpleDraweeView?.controller = Fresco.newDraweeControllerBuilder()
|
|
.setUri(url)
|
|
.setControllerListener(listener)
|
|
.build()
|
|
}
|
|
|
|
// 自适应图片宽高
|
|
fun displayScale(simpleDraweeView: SimpleDraweeView?, url: String?, height: Int) {
|
|
val listener = object : BaseControllerListener<ImageInfo>() {
|
|
override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?, animatable: Animatable?) {
|
|
if (imageInfo == null) {
|
|
return
|
|
}
|
|
val layoutParams = simpleDraweeView?.layoutParams
|
|
val scale = imageInfo.width.toFloat() / imageInfo.height.toFloat()
|
|
layoutParams?.width = (height * scale).toInt()
|
|
simpleDraweeView?.layoutParams = layoutParams
|
|
}
|
|
}
|
|
simpleDraweeView?.controller = Fresco.newDraweeControllerBuilder()
|
|
.setUri(url)
|
|
.setControllerListener(listener)
|
|
.build()
|
|
}
|
|
|
|
// 设置缩放类型,设置按压状态下的叠加图
|
|
fun display(resources: Resources?, simpleDraweeView: SimpleDraweeView?,
|
|
scaleType: ScalingUtils.ScaleType?, url: String?) {
|
|
if (simpleDraweeView == null) return
|
|
val context = simpleDraweeView.context ?: return
|
|
simpleDraweeView.hierarchy = GenericDraweeHierarchyBuilder(resources)
|
|
.setFadeDuration(500)
|
|
.setPressedStateOverlay(ColorDrawable(ContextCompat.getColor(context, R.color.pressed_bg)))
|
|
.setPlaceholderImage(R.drawable.occupy2, ScalingUtils.ScaleType.CENTER)
|
|
.setBackground(ColorDrawable(ContextCompat.getColor(context, R.color.placeholder_bg)))
|
|
.setActualImageScaleType(scaleType)
|
|
.build()
|
|
// simpleDraweeView.setImageURI(url);
|
|
display(simpleDraweeView, url)
|
|
}
|
|
|
|
// 设置占位符
|
|
fun display(resources: Resources?, simpleDraweeView: SimpleDraweeView?, url: String?, placeholderImage: Int) {
|
|
if (simpleDraweeView == null) return
|
|
val context = simpleDraweeView.context ?: return
|
|
simpleDraweeView.hierarchy = GenericDraweeHierarchyBuilder(resources)
|
|
.setFadeDuration(500)
|
|
.setPressedStateOverlay(ColorDrawable(ContextCompat.getColor(context, R.color.pressed_bg)))
|
|
.setBackground(ColorDrawable(ContextCompat.getColor(context, R.color.placeholder_bg)))
|
|
.setPlaceholderImage(placeholderImage)
|
|
.build()
|
|
// simpleDraweeView.setImageURI(url);
|
|
display(simpleDraweeView, url)
|
|
}
|
|
|
|
// 图片下载监听和设置低高分辨率图片
|
|
fun display(simpleDraweeView: SimpleDraweeView?, url: String?, lowUrl: String?,
|
|
listener: ControllerListener<in ImageInfo>) {
|
|
simpleDraweeView?.controller = Fresco.newDraweeControllerBuilder()
|
|
.setImageRequest(ImageRequest.fromUri(url))
|
|
.setControllerListener(listener)
|
|
.setLowResImageRequest(ImageRequest.fromUri(lowUrl)) // 低分辨率图片
|
|
.build()
|
|
}
|
|
|
|
// 获取bitmap
|
|
fun display(context: Context?, url: String?, dataSubscriber: BaseBitmapDataSubscriber) {
|
|
val imageRequest = ImageRequestBuilder
|
|
.newBuilderWithSource(Uri.parse(url))
|
|
.setProgressiveRenderingEnabled(true)
|
|
.build()
|
|
|
|
Fresco.getImagePipeline()
|
|
.fetchDecodedImage(imageRequest, context)
|
|
.subscribe(dataSubscriber, CallerThreadExecutor.getInstance())
|
|
}
|
|
|
|
companion object {
|
|
const val RESPONSE403: String = "RESPONSE403"
|
|
|
|
fun getInstance(): ImageUtils {
|
|
return Inner.anotherSingle
|
|
}
|
|
|
|
private object Inner {
|
|
val anotherSingle = ImageUtils()
|
|
}
|
|
|
|
fun display(simpleDraweeView: SimpleDraweeView, url: String?) {
|
|
simpleDraweeView.setImageURI(url)
|
|
}
|
|
|
|
fun display(draweeView: SimpleDraweeView, @DrawableRes res: Int?) {
|
|
draweeView.setImageURI("res:///" + res)
|
|
}
|
|
|
|
fun postImageArr(context: Context, imgArr: List<String>, listener: OnPostArrImageListener) {
|
|
val imgMap: HashMap<String, String> = HashMap()
|
|
|
|
Observable.create(Observable.OnSubscribe<JSONObject> { subscriber ->
|
|
var path: String
|
|
var index = 0
|
|
for (s in imgArr) {
|
|
path = context.getCacheDir().path + File.separator + System.currentTimeMillis() + index + ".jpg"
|
|
if (BitmapUtils.savePicture(path, s, 1024 * 1024)) {
|
|
subscriber.onNext(FileUtils.uploadFile(Config.API_HOST + "images?type=community", path, s, UserManager.getInstance().token))
|
|
index++
|
|
} else {
|
|
subscriber.onNext(FileUtils.uploadFile(Config.API_HOST + "images?type=community", s, s, UserManager.getInstance().token))
|
|
}
|
|
}
|
|
subscriber.onCompleted()
|
|
}).subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(object : Observer<JSONObject> {
|
|
override fun onCompleted() {
|
|
Utils.log("图片上传完成")
|
|
listener.postSuccess(imgMap)
|
|
}
|
|
|
|
override fun onError(e: Throwable) {
|
|
Utils.log("图片上传失败" + e.toString())
|
|
listener.postError()
|
|
}
|
|
|
|
override fun onNext(result: JSONObject?) {
|
|
if (result != null) {
|
|
try {
|
|
val statusCode = result.getInt("statusCode")
|
|
if (statusCode == HttpURLConnection.HTTP_OK) {
|
|
imgMap.put(result.getString("realPath"), result.getString("icon"))
|
|
} else if (statusCode == 403) {
|
|
imgMap.put(result.getString("realPath"), RESPONSE403)
|
|
}
|
|
} catch (e: Exception) {
|
|
e.printStackTrace()
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
fun postImage(context: Context?, picturePath: String?, listener: OnPostImageListener) {
|
|
if (context == null || TextUtils.isEmpty(picturePath)) return
|
|
Observable.create(Observable.OnSubscribe<JSONObject> { subscriber ->
|
|
val path = context.getCacheDir().path + File.separator + System.currentTimeMillis() + ".jpg"
|
|
if (BitmapUtils.savePicture(path, picturePath, 1024*1024)) {
|
|
subscriber.onNext(FileUtils.uploadFile(Config.API_HOST + "images?type=community", path, UserManager.getInstance().token))
|
|
} else {
|
|
subscriber.onNext(FileUtils.uploadFile(Config.API_HOST + "images?type=community", picturePath, UserManager.getInstance().token))
|
|
}
|
|
}).subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(object : Response<JSONObject>() {
|
|
override fun onResponse(response: JSONObject?) {
|
|
if (CommonDebug.IS_DEBUG) {
|
|
Utils.log("postImage:onResponse=>" + response.toString())
|
|
}
|
|
listener.postSuccess(response)
|
|
}
|
|
|
|
override fun onFailure(e: HttpException?) {
|
|
listener.postError()
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
|
|
interface OnPostImageListener {
|
|
fun postSuccess(response: JSONObject?)
|
|
|
|
fun postError()
|
|
}
|
|
|
|
interface OnPostArrImageListener {
|
|
/**
|
|
* key: 图片本地路径
|
|
* value: 图片提交成功后的链接
|
|
*/
|
|
fun postSuccess(imgMap: HashMap<String, String>)
|
|
|
|
fun postError()
|
|
}
|
|
|
|
}
|