Commit cf052242 authored by jyx's avatar jyx

添加上传照片功能

parent 331223d6
......@@ -256,6 +256,14 @@
android:name=".ui.activitys.BasicUserProfileActivity"
android:exported="false"
android:screenOrientation="portrait" />
<activity
android:name=".ui.activitys.AlbumActivity"
android:exported="false"
android:screenOrientation="portrait" />
<activity
android:name=".ui.preview.ImagePreviewActivity"
android:exported="false"
android:screenOrientation="portrait" />
<activity
android:name=".ui.activitys.IMActivity"
......
......@@ -2,6 +2,7 @@ package com.duben.roseplaylet.common
enum class OssType(var type: String) {
ALBUM("album"),
PHOTO("photo"),
VIDEO("video")
......
......@@ -53,7 +53,10 @@ class IMHelper private constructor() {
override fun onCompleted() {}
override fun onError(e: Throwable) {}
override fun onNext(baseResponse: BaseResponse<JsonObject>) {
if (baseResponse.status == 200) {
val psw = baseResponse.data["pass"].asString
SECRET_KEY = psw
}
}
})
}
......
package com.duben.roseplaylet.mvp.model
import com.chad.library.adapter.base.entity.MultiItemEntity
const val IMAGE_MULTI_ITEM_ADD = 1
const val IMAGE_MULTI_ITEM_NORMAL = 2
data class ImageMultiItemEntity(
override val itemType: Int,
var img: String,
) : MultiItemEntity, java.io.Serializable
package com.duben.roseplaylet.mvp.presenters
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import com.duben.roseplaylet.manager.AppHttpManager
import com.duben.roseplaylet.mvp.model.BaseResponse
import com.duben.roseplaylet.mvp.views.AlbumView
import com.google.gson.JsonObject
class AlbumPresenter : BasePresenter<AlbumView>() {
fun getImages() {
AppHttpManager.getInstance(loanApplication)
.call(loanService.getImages({}),
object : BaseSubscriber<BaseResponse<JsonObject>>() {
override fun onCompleted() {
if (isLinkView) return
}
override fun onError(e: Throwable) {
if (isLinkView) return
view.getImagesFail()
}
override fun onNext(baseResponse: BaseResponse<JsonObject>) {
if (isLinkView) return
view.hideLoading()
val code = baseResponse.status
val message = baseResponse.message
when (code) {
200 -> {
val ja = baseResponse.data["images"].asJsonArray
val imagesList = arrayListOf<String>()
ja.forEach {
imagesList.add(it.asString)
}
view.getImagesSuc(imagesList)
}
else -> {
view.showToast(message)
view.getImagesFail()
}
}
}
})
}
fun setImages(images: ArrayList<String> = arrayListOf(), image: String = "", type: Int = 0) {
val vo = HashMap<String, Any>()
if (images.isEmpty() && image.isEmpty()) return
if (images.isNotEmpty()) {
vo["images"] = images
} else {
vo["image"] = image
vo["type"] = type
}
AppHttpManager.getInstance(loanApplication)
.call(loanService.setImages(vo),
object : BaseSubscriber<BaseResponse<JsonObject>>() {
override fun onCompleted() {
if (isLinkView) return
}
override fun onError(e: Throwable) {
if (isLinkView) return
view.setImagesFail()
}
override fun onNext(baseResponse: BaseResponse<JsonObject>) {
if (isLinkView) return
view.hideLoading()
val code = baseResponse.status
val message = baseResponse.message
when (code) {
200 -> {
view.setImagesSuc()
}
else -> {
view.showToast(message)
view.setImagesFail()
}
}
}
})
}
}
\ No newline at end of file
package com.duben.roseplaylet.mvp.views
interface AlbumView : BaseView {
fun getImagesSuc(data: List<String>)
fun getImagesFail()
fun setImagesSuc()
fun setImagesFail()
}
......@@ -488,7 +488,7 @@ public interface LoanService {
* 查看图片
*/
@POST("roseApi/user/getImages")
Observable<BaseResponse<JsonObject>> getImages();
Observable<BaseResponse<JsonObject>> getImages(@Body Object obj);
/**
* 设置图片
......
package com.duben.roseplaylet.ui.activitys
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.view.View
import com.duben.library.utils.nodoubleclick.AntiShake
import com.duben.roseplaylet.R
import com.duben.roseplaylet.common.OssType
import com.duben.roseplaylet.manager.OssManager
import com.duben.roseplaylet.mvp.presenters.AlbumPresenter
import com.duben.roseplaylet.mvp.views.AlbumView
import com.duben.roseplaylet.ui.activitys.base.BaseActivity
import com.duben.roseplaylet.ui.preview.ImagePreviewActivity
import com.duben.roseplaylet.ui.widgets.DeletePhotoConfirmDialog
import com.duben.roseplaylet.ui.widgets.DialogListener
import com.duben.roseplaylet.ui.widgets.nineImage.AddImagesInfo
import com.duben.roseplaylet.ui.widgets.nineImage.AddImagesView
import com.duben.roseplaylet.utils.GlideEngine
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.compress.Luban
import com.luck.picture.lib.config.PictureConfig
......@@ -15,66 +32,29 @@ import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.style.PictureWindowAnimationStyle
import com.luck.picture.lib.thread.PictureThreadUtils
import com.luck.picture.lib.tools.DateUtils
import com.luck.picture.lib.tools.SdkVersionUtils
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.roseplaylet.R
import com.duben.roseplaylet.manager.AppHttpManager
import com.duben.roseplaylet.manager.OssManager
import com.duben.roseplaylet.mvp.model.BaseResponse
import com.duben.roseplaylet.mvp.model.OrderRecordBean
import com.duben.roseplaylet.ui.activitys.base.BaseActivity
import com.duben.roseplaylet.ui.widgets.nineImage.AddImagesInfo
import com.duben.roseplaylet.ui.widgets.nineImage.AddImagesView
import com.duben.roseplaylet.utils.GlideEngine
import com.duben.roseplaylet.utils.SpanUtils
import com.tbruyelle.rxpermissions.RxPermissions
import com.yalantis.ucrop.UCrop
import kotlinx.android.synthetic.main.activity_template.*
import kotlinx.android.synthetic.main.activity_album.*
import kotlinx.android.synthetic.main.header_layout.*
import java.io.File
import java.util.*
import kotlin.collections.HashMap
/**
* 描述:申请模板
*/
@SuppressLint("SetTextI18n")
class TemplateActivity : BaseActivity(), View.OnClickListener {
class AlbumActivity : BaseActivity(), View.OnClickListener, AlbumView {
private var mOssList = arrayListOf<String>()
companion object {
const val MAX_ALBUM_SIZE = 6
}
// 图片选择框架跳转动画
private var mCanUploadMax = MAX_ALBUM_SIZE
private var mUploadCount = 0
private lateinit var mWindowAnimationStyle: PictureWindowAnimationStyle
override fun getContentViewLayoutID() = R.layout.activity_template
private var mOssList = arrayListOf<String>()
private val albumPresenter by lazy { AlbumPresenter() }
override fun isApplyKitKatTranslucency() = false
override fun getContentViewLayoutID() = R.layout.activity_album
override fun initViewsAndEvents() {
tv_title.text = "计数模型申请"
iv_left_icon.visibility = View.VISIBLE
iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)
tv_name.text = SpanUtils()
.append("*")
.setForegroundColor(resources.getColor(R.color.color_FF4144))
.append("需要计数的物品名称")
.setForegroundColor(resources.getColor(R.color.black))
.create()
tv_phone.text = SpanUtils()
.append("*")
.setForegroundColor(resources.getColor(R.color.color_FF4144))
.append("联系方式")
.setForegroundColor(resources.getColor(R.color.black))
.create()
tv_photo.text = SpanUtils()
.append("*")
.setForegroundColor(resources.getColor(R.color.color_FF4144))
.append("需要计数的物品照片(5-10张图片)")
.setForegroundColor(resources.getColor(R.color.black))
.create()
albumPresenter.attachView(this)
mWindowAnimationStyle = PictureWindowAnimationStyle()
mWindowAnimationStyle.ofAllAnimation(
......@@ -82,75 +62,90 @@ class TemplateActivity : BaseActivity(), View.OnClickListener {
com.luck.picture.lib.R.anim.picture_anim_down_out
)
initListener()
albumPresenter.getImages()
initView()
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.iv_left_icon -> finish()
R.id.btn_submit -> {
if (TextUtils.isEmpty(et_name.text)) {
showToast("申请名称不能为空")
return
}
if (TextUtils.isEmpty(et_phone.text)) {
showToast("联系方式不能为空")
return
}
if (aiv.getAddImages().size < 5) {
showToast("模板照片最少需要5张")
return
}
mOssList.clear()
luban()
}
}
}
private fun initListener() {
private fun initView() {
tv_title.text = "我的相册"
tv_title.setTextColor(Color.WHITE)
iv_left_icon.visibility = View.VISIBLE
iv_left_icon.setImageResource(R.mipmap.ic_arrow_white)
iv_left_icon.setOnClickListener(this)
btn_submit.setOnClickListener(this)
line.visibility = View.GONE
aiv.enableCloseView(false)
aiv.setAddImagesViewListener(object : AddImagesView.AddImagesViewListener {
override fun onAdd() {
showAlbum()
}
override fun onDelete(position: Int) {
override fun onDelete(position: Int) {}
override fun onLongClick(item: AddImagesInfo?) {
DeletePhotoConfirmDialog(this@AlbumActivity, object : DialogListener() {
override fun onClick(dialog: Dialog?, v: View?) {
super.onClick(dialog, v)
dialog?.dismiss()
if (v?.id == R.id.tv_confirm) {
aiv.deleteImage(item)
// 删除图片
albumPresenter.setImages(arrayListOf(), item?.imgUrl.toString(), 2)
}
}
}).show()
}
override fun onPreview(item: AddImagesInfo?, lists: List<AddImagesInfo>?) {
override fun onPreview(position: Int, lists: List<AddImagesInfo>?) {
lists?.let {
val preData = arrayListOf<String>()
for (info in it) {
preData.add(info.imgUrl.toString())
}
val bundle = Bundle()
bundle.putStringArrayList(ImagePreviewActivity.IMAGE_PREVIEW_DATA, preData)
bundle.putInt(ImagePreviewActivity.IMAGE_PREVIEW_POSITION, position)
readyGo(ImagePreviewActivity::class.java, bundle)
}
}
})
}
override fun isApplyKitKatTranslucency() = false
override fun onClick(v: View?) {
if (AntiShake.check(v?.id)) return
when (v?.id) {
R.id.iv_left_icon -> finish()
}
}
private fun showAlbum() {
mCanUploadMax = MAX_ALBUM_SIZE - aiv.getAddImages().size
RxPermissions(this)
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
.subscribe { granted: Boolean ->
if (granted) {
val isEnableCrop = false
PictureSelector.create(this)
.openGallery(PictureMimeType.ofImage())
.theme(R.style.picture_WeChat_style) // 微信主题色
.isWeChatStyle(true) // 微信样式
.isCamera(false) //列表是否显示拍照按钮
.isZoomAnim(isEnableCrop)//图片选择缩放效果
.isEnableCrop(isEnableCrop)//是否开启裁剪
.isZoomAnim(false)//图片选择缩放效果
.isEnableCrop(true)//是否开启裁剪
.setPictureWindowAnimationStyle(mWindowAnimationStyle)
.withAspectRatio(2, 4)
.isCompress(true)//是否压缩
.compressFocusAlpha(false)//压缩后是否保持图片的透明通道
.compressFocusAlpha(true)//压缩后是否保持图片的透明通道
.minimumCompressSize(200)// 小于多少kb的图片不压缩
.compressQuality(60)//图片压缩后输出质量
.synOrAsy(true)//开启同步or异步压缩
.maxSelectNum(10)
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)//屏幕旋转方向
.maxSelectNum(mCanUploadMax)
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)//屏幕旋转方向
.selectionMode(PictureConfig.MULTIPLE)//单选or多选
.imageEngine(GlideEngine.createGlideEngine())// 图片加载引擎
.forResult(PictureConfig.CHOOSE_REQUEST)
} else {
showMissingPermissionDialog("存储")
}
......@@ -164,13 +159,9 @@ class TemplateActivity : BaseActivity(), View.OnClickListener {
// 相册获取图片
PictureConfig.CHOOSE_REQUEST -> {
val selectList: List<LocalMedia> = PictureSelector.obtainMultipleResult(data)
val imgList = arrayListOf<AddImagesInfo>()
for (localMedia in selectList) {
val addImagesInfo = AddImagesInfo(localMedia.compressPath, "2")
imgList.add(addImagesInfo)
}
aiv.addAddImages(imgList)
mUploadCount = selectList.size
mOssList.clear()
luban(selectList)
}
}
} else if (resultCode == UCrop.RESULT_ERROR) {
......@@ -182,23 +173,9 @@ class TemplateActivity : BaseActivity(), View.OnClickListener {
/**
* 鲁班压缩
*/
private fun luban() {
val dataList = arrayListOf<LocalMedia>()
aiv.getAddImages().let {
for (addImagesInfo in it) {
val cutPath = addImagesInfo.imgUrl as String
val media = LocalMedia(cutPath, 0, false, 1, 0, PictureMimeType.ofImage())
media.cutPath = cutPath
if (SdkVersionUtils.checkedAndroid_Q()) {
media.androidQToPath = cutPath
}
dataList.add(media)
}
}
private fun luban(images: List<LocalMedia>) {
runOnUiThread {
showLoading("加载中...")
showLoading("正在上传中...")
}
val newFileName = DateUtils.getCreateFileName("IMG_CMP_") + ".jpeg"
......@@ -207,7 +184,7 @@ class TemplateActivity : BaseActivity(), View.OnClickListener {
@Throws(Exception::class)
override fun doInBackground(): List<File> {
return Luban.with(mContext)
.loadMediaData<LocalMedia>(dataList)
.loadMediaData<LocalMedia>(images)
.ignoreBy(200)
.isCamera(false)
.setCompressQuality(80)
......@@ -218,7 +195,7 @@ class TemplateActivity : BaseActivity(), View.OnClickListener {
override fun onSuccess(files: List<File>) {
for (file in files) {
uploadOss(file.path, "APPLY_TEMPLATE")
uploadOss(file.path, OssType.ALBUM.type)
}
}
......@@ -249,30 +226,53 @@ class TemplateActivity : BaseActivity(), View.OnClickListener {
showToast("图片异常,请重新添加")
return@runOnUiThread
}
hideLoading()
mOssList.add(imgPath!!)
if (mOssList.size == aiv.getAddImages().size) {
val hashMap = HashMap<String, Any>()
hashMap["title"] = et_name.text.toString()
hashMap["mobile"] = et_phone.text.toString()
hashMap["imgs"] = mOssList.toArray()
// AppHttpManager.getInstance(baseApplication)
// .call(baseApplication.loanService.numFeedback(hashMap),
// object : BaseSubscriber<BaseResponse<OrderRecordBean>>() {
// override fun onError(e: Throwable) {
// hideLoading()
// }
//
// override fun onNext(t: BaseResponse<OrderRecordBean>) {
// hideLoading()
// finish()
// }
// })
if (mOssList.size == mUploadCount) {
val imgList = arrayListOf<AddImagesInfo>()
for (ossUrl in mOssList) {
val addImagesInfo = AddImagesInfo(ossUrl, "2")
imgList.add(addImagesInfo)
}
aiv.addAddImages(imgList)
Handler(Looper.getMainLooper()).postDelayed({
val uploadImages = arrayListOf<String>()
for (image in aiv.getAddImages()) {
uploadImages.add(image.imgUrl.toString())
}
albumPresenter.setImages(uploadImages)
}, 1000)
}
}
}
})
}
}
}
override fun onDestroy() {
super.onDestroy()
albumPresenter.detachView()
}
override fun getImagesSuc(data: List<String>) {
val imgList = arrayListOf<AddImagesInfo>()
for (s in data) {
val addImagesInfo = AddImagesInfo(s, "2")
imgList.add(addImagesInfo)
}
aiv.addAddImages(imgList)
}
override fun getImagesFail() {
}
override fun setImagesSuc() {
}
override fun setImagesFail() {
}
}
\ No newline at end of file
......@@ -62,7 +62,7 @@ class IMActivity : TUIBaseChatActivity() {
val chatView = chatFragment.chatView
chatView.viewline.visibility = View.GONE
chatView.inputView.setInterceptTouchEvent(true)
// chatView.inputView.setInterceptTouchEvent(true)
chatView.inputView.setBgColor(resources.getColor(R.color.color_171619))
chatView.inputView.setOnClickListener {
......
......@@ -57,6 +57,7 @@ public abstract class BaseActivity extends BaseAppCompatActivity implements Base
|| TextUtils.equals(getClass().getSimpleName(), "EditProfileActivity")
|| TextUtils.equals(getClass().getSimpleName(), "UserProfileActivity")
|| TextUtils.equals(getClass().getSimpleName(), "MainActivity")
|| TextUtils.equals(getClass().getSimpleName(), "AlbumActivity")
) {
setDarkStatusBar();
} else {
......
package com.duben.roseplaylet.ui.adapter
import android.view.View
import android.widget.ImageView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.duben.library.utils.GlideUtils
import com.duben.roseplaylet.R
import com.duben.roseplaylet.mvp.model.*
/**
* @author Assen
* @date 2024/1/9
* @desc
*/
class AlbumAdapter : BaseMultiItemQuickAdapter<ImageMultiItemEntity, BaseViewHolder>() {
init {
addItemType(IMAGE_MULTI_ITEM_ADD, R.layout.item_album_add)
addItemType(IMAGE_MULTI_ITEM_NORMAL, R.layout.item_album_normal)
}
override fun convert(holder: BaseViewHolder, item: ImageMultiItemEntity) {
when (holder.itemViewType) {
IMAGE_MULTI_ITEM_ADD -> {
holder.getView<View>(R.id.iv_add).setOnClickListener {
mOnAlbumClickListener?.onAddClick()
}
}
IMAGE_MULTI_ITEM_NORMAL -> {
val iv = holder.getView<ImageView>(R.id.iv_normal)
iv.setOnClickListener {
mOnAlbumClickListener?.onImageClick(holder.adapterPosition)
}
iv.setOnLongClickListener {
mOnAlbumClickListener?.onDeleteClick(holder.adapterPosition)
return@setOnLongClickListener true
}
GlideUtils.loadImageViewNoAnim2(context, item.img, iv)
}
}
}
private var mOnAlbumClickListener: OnAlbumClickListener? = null
fun setOnAlbumClickListener(onAlbumClickListener: OnAlbumClickListener) {
mOnAlbumClickListener = onAlbumClickListener
}
interface OnAlbumClickListener {
fun onDeleteClick(position: Int)
fun onAddClick()
fun onImageClick(position: Int)
}
}
\ No newline at end of file
......@@ -19,7 +19,6 @@ class SquarePageAdapter(
override fun getItemCount() = data.size
override fun createFragment(position: Int): Fragment {
LogUtil.d("AAAAAAAAAA$position")
val newFragment = SquareListFragment.newInstance(position + 1)
fragments[position] = newFragment
return newFragment
......
......@@ -110,13 +110,13 @@ class MainFragment : LazyLoadBaseFragment(), HomeView, View.OnClickListener, OnR
//获取游客登录数据
homePresenter.userLogin()
} else {
homePresenter.topTabs()
homePresenter.orders()
if (userManager.vipFlag) {
homePresenter.signAfterSeconds()
}
IMHelper.instance.getImMsg()
TrackManager.getInstance().getMyInfo()
// homePresenter.topTabs()
// homePresenter.orders()
// if (userManager.vipFlag) {
// homePresenter.signAfterSeconds()
// }
// IMHelper.instance.getImMsg()
// TrackManager.getInstance().getMyInfo()
}
AppConfig.showVipAdDialog = false
......
......@@ -99,6 +99,7 @@ class MyFragment : LazyLoadBaseFragment(), MyView, View.OnClickListener {
tuiLoginConfig.isInitLocalStorageOnly = true
val userID = instance.userID
val userSig = IMHelper.instance.getUserSig()
LogUtil.i("LoginWrapper ", "userSig $userSig")
LoginWrapper.getInstance().loginIMSDK(MintsApplication.getContext(),
IMHelper.IM_SDK_APPID, userID, userSig, tuiLoginConfig, object : TUICallback() {
override fun onSuccess() {
......@@ -129,8 +130,8 @@ class MyFragment : LazyLoadBaseFragment(), MyView, View.OnClickListener {
R.id.ll_my_setting -> {
// readyGo(SettingsActivity::class.java)
// readyGo(RealAuthActivity::class.java)
readyGo(BasicUserProfileActivity::class.java)
// readyGo(BasicUserProfileActivity::class.java)
readyGo(AlbumActivity::class.java)
}
R.id.tv_my_account, R.id.ll_my_login -> {
if (!userManager.userIsLogin()) {
......@@ -139,7 +140,9 @@ class MyFragment : LazyLoadBaseFragment(), MyView, View.OnClickListener {
}
R.id.ll_my_record -> {
// readyGo(WatchRecordActivity::class.java)
readyGo(UserProfileActivity::class.java)
IMHelper.instance.getImMsg()
// readyGo(UserProfileActivity::class.java)
}
R.id.ll_my_feed -> {
// readyGo(FeedbackActivity::class.java)
......
package com.duben.roseplaylet.ui.widgets
import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.duben.roseplaylet.R
class DeletePhotoConfirmDialog(
private val context: Context,
private val listener: DialogListener?
) :
Dialog(context, R.style.dialog) {
private val lp: WindowManager.LayoutParams
private val tv_confirm: TextView
private val iv_close: ImageView
init {
setContentView(R.layout.dialog_delete_photo_confirm)
// 设置window属性
lp = window!!.attributes
lp.gravity = Gravity.CENTER
lp.width = WindowManager.LayoutParams.MATCH_PARENT
lp.windowAnimations = R.style.DialogAnimFade
window!!.attributes = lp
// 设置外部不可关闭
setCancelable(false)
setCanceledOnTouchOutside(false)
setOnKeyListener { _, i, _ -> i == KeyEvent.KEYCODE_BACK }
tv_confirm = findViewById<View>(R.id.tv_confirm) as TextView
iv_close = findViewById<View>(R.id.iv_close) as ImageView
iv_close.setOnClickListener(listener)
tv_confirm.setOnClickListener(listener)
listener?.setDialog(this)
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.duben.roseplaylet.ui.widgets.nineImage
import android.content.Context
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -9,8 +10,7 @@ import android.view.WindowManager
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.duben.library.utils.GlideUtils
import com.duben.roseplaylet.R
class AddImagesAdapter(
......@@ -20,8 +20,7 @@ class AddImagesAdapter(
addCloseImgs: Int,
addErrImgs: Int,
addLimitNums: Int
) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var mContext: Context
var limtNum = 5
var addImgs = -1
......@@ -29,6 +28,7 @@ class AddImagesAdapter(
var addErrImgs = -1
var addLimitNums = 9
var addImages = ArrayList<AddImagesInfo>()
var enableClose = false
init {
this.mContext = mContext
......@@ -46,12 +46,7 @@ class AddImagesAdapter(
}
fun getHeight(): Int {
return (getScreenWidth(mContext)) / 5
}
private fun dip2px(context: Context, dp: Int): Int {
val density = context.resources.displayMetrics.density
return (dp * density + 0.5).toInt()
return (getScreenWidth(mContext)) / 2
}
private fun getScreenWidth(context: Context): Int {
......@@ -62,7 +57,6 @@ class AddImagesAdapter(
return outMetrics.widthPixels
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_add_images, parent, false)
......@@ -71,45 +65,71 @@ class AddImagesAdapter(
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val ktHolder = holder as AddImagesViewHolder
if (addImgs != -1) {
ktHolder.ivAdd.setBackgroundResource(addImgs)
} else {
ktHolder.ivAdd.setBackgroundResource(R.drawable.iv_add_images_add)
}
if (addCloseImgs != -1) {
ktHolder.ivClose.setBackgroundResource(addCloseImgs)
} else {
ktHolder.ivClose.setBackgroundResource(R.drawable.iv_add_img_close)
}
ktHolder.rlClose.setOnClickListener {
processDelete(getItem(position))
if (listener != null) listener!!.onDeleteClick(position)
listener?.onDeleteClick(position)
}
ktHolder.rlAdd.setOnClickListener {
if (listener != null) listener!!.onAddClick(ktHolder.rlAdd, getItem(position))
listener?.onAddClick(ktHolder.rlAdd, position)
}
ktHolder.ivContent.setOnClickListener {
if (listener != null) listener!!.onAddClick(ktHolder.ivContent, getItem(position))
listener?.onAddClick(ktHolder.ivContent, position)
}
ktHolder.ivContent.setOnLongClickListener() {
listener?.onLongClick(getItem(position))
return@setOnLongClickListener false
}
val width = (getScreenWidth(mContext) - dp2px(40f)) / limtNum
if (getItem(position)!!.type == "2") {
ktHolder.ivContent.visibility = View.VISIBLE
ktHolder.rlClose.visibility = View.VISIBLE
ktHolder.rlAdd.visibility = View.GONE
if (addErrImgs != -1) {
val requestOptions = RequestOptions().error(addErrImgs)
Glide.with(mContext).load(getItem(position)!!.imgUrl).apply(requestOptions)
.into(ktHolder.ivContent)
if (enableClose) {
if (addCloseImgs != -1) {
ktHolder.ivClose.setBackgroundResource(addCloseImgs)
} else {
ktHolder.ivClose.setBackgroundResource(R.drawable.iv_add_img_close)
}
} else {
val requestOptions = RequestOptions().error(R.drawable.iv_add_err)
Glide.with(mContext).load(getItem(position)!!.imgUrl).apply(requestOptions)
.into(ktHolder.ivContent)
ktHolder.ivClose.visibility = View.GONE
}
if (addErrImgs != -1) {
GlideUtils.loadImageViewNoAnim2(
mContext,
getItem(position)?.imgUrl,
ktHolder.ivContent
)
} else {
GlideUtils.loadImageViewNoAnim2(
mContext,
getItem(position)?.imgUrl,
ktHolder.ivContent
)
}
// 设置宽高一致
val layoutParams = ktHolder.ivContent.layoutParams
layoutParams.height = width
ktHolder.ivContent.layoutParams = layoutParams
} else {
// 设置宽高一致
val layoutParams2 = ktHolder.ivAdd.layoutParams
layoutParams2.height = width
ktHolder.ivAdd.layoutParams = layoutParams2
ktHolder.ivContent.visibility = View.GONE
ktHolder.rlAdd.visibility = View.VISIBLE
ktHolder.rlClose.visibility = View.GONE
......@@ -117,14 +137,13 @@ class AddImagesAdapter(
}
private fun processDelete(item: AddImagesInfo?) {
fun processDelete(item: AddImagesInfo?) {
addImages.remove(item)
if (!hasAdd()) {
addItem(AddImagesInfo(R.drawable.iv_add_images_add, "1"))
} else {
notifyDataSetChanged()
}
}
fun addItems(lists: List<AddImagesInfo>) {
......@@ -153,7 +172,6 @@ class AddImagesAdapter(
private fun processData(addDatas: List<AddImagesInfo>) {
val newList = processEnd(getItems())
// if (newList.size >= limtNum) return
if (newList.size + addDatas.size < addLimitNums) {
newList.addAll(addDatas)
newList.add(AddImagesInfo("", "1"))
......@@ -161,7 +179,6 @@ class AddImagesAdapter(
newList.addAll(addDatas.subList(0, addLimitNums - newList.size))
}
notifyDataSetChanged()
}
private fun processEnd(addDatas: ArrayList<AddImagesInfo>): ArrayList<AddImagesInfo> {
......@@ -174,7 +191,7 @@ class AddImagesAdapter(
}
private fun hasAdd(): Boolean {
getItems()!!.forEach {
getItems().forEach {
if (it.type == "1") {
return true
}
......@@ -194,8 +211,18 @@ class AddImagesAdapter(
return null
}
fun enableClose(enable: Boolean) {
this.enableClose = enable
}
private fun dp2px(dp: Float): Int {
val displayMetrics: DisplayMetrics = mContext.resources.displayMetrics
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics).toInt()
}
interface OnAddClickListener {
fun onAddClick(view: View?, item: AddImagesInfo?)
fun onAddClick(view: View?, position: Int)
fun onLongClick(item: AddImagesInfo?)
fun onDeleteClick(position: Int)
}
......
package com.duben.roseplaylet.ui.widgets.nineImage
class AddImagesInfo (var imgUrl:Any,var type:String)
\ No newline at end of file
class AddImagesInfo(var imgUrl: Any, var type: String)
\ No newline at end of file
......@@ -31,7 +31,7 @@ class AddImagesView @JvmOverloads constructor(
//最大图片数
private var add_limit_nums = 9
private var mAdapter: AddImagesAdapter? = null
private lateinit var mAdapter: AddImagesAdapter
init {
this.mContext = mContext
......@@ -68,42 +68,51 @@ class AddImagesView @JvmOverloads constructor(
add_limit_nums
)
rvContent.adapter = mAdapter
mAdapter!!.addItem(AddImagesInfo("", "1"))
mAdapter!!.setOnAddListener(object : AddImagesAdapter.OnAddClickListener {
override fun onAddClick(view: View?, item: AddImagesInfo?) {
when (view!!.id) {
mAdapter.addItem(AddImagesInfo("", "1"))
mAdapter.setOnAddListener(object : AddImagesAdapter.OnAddClickListener {
override fun onAddClick(view: View?, position: Int) {
when (view?.id) {
R.id.rl_add -> {
if (listener != null)
listener!!.onAdd()
listener?.onAdd()
}
R.id.iv_content -> {
if (listener != null)
listener!!.onPreview(item, mAdapter!!.getItems())
listener?.onPreview(position, mAdapter.getItems())
}
}
}
override fun onDeleteClick(position: Int) {
if (listener != null)
listener!!.onDelete(position)
override fun onLongClick(item: AddImagesInfo?) {
listener?.onLongClick(item)
}
override fun onDeleteClick(position: Int) {
listener?.onDelete(position)
}
})
}
//添加数据
fun addAddImages(lists: List<AddImagesInfo>) {
mAdapter!!.addItems(lists)
mAdapter.addItems(lists)
}
fun getAddImages(): List<AddImagesInfo> {
return mAdapter!!.getItems()
return mAdapter.getItems()
}
fun enableCloseView(enable: Boolean) {
mAdapter.enableClose(enable)
}
fun deleteImage(item: AddImagesInfo?) {
mAdapter.processDelete(item)
}
interface AddImagesViewListener {
fun onAdd()
fun onDelete(position: Int)
fun onPreview(item: AddImagesInfo?, lists: List<AddImagesInfo>?)
fun onLongClick(item: AddImagesInfo?)
fun onPreview(position: Int, lists: List<AddImagesInfo>?)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_232122"
android:orientation="vertical">
<include layout="@layout/header_layout" />
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/gray"
android:gravity="center"
android:text="最多上传6张图片,长按可删除照片"
android:textColor="@color/white" />
<com.duben.roseplaylet.ui.widgets.nineImage.AddImagesView
android:id="@+id/aiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:add_limit_nums="6"
app:column_nums="3" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_bg_hall"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="20dp"
android:gravity="center"
android:text="删除后无法找回,确定要删除吗?"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_confirm"
android:layout_width="260dp"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:background="@drawable/shape_square_tab"
android:gravity="center"
android:text="确定"
android:textColor="@color/black"
android:textSize="14sp" />
</LinearLayout>
<ImageView
android:id="@+id/iv_close"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/ll_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_close" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
......@@ -16,13 +15,14 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_margin="5dp"
android:scaleType="center" />
</RelativeLayout>
<ImageView
android:id="@+id/iv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:padding="5dp"
android:scaleType="center"
android:visibility="gone" />
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="6dp"
android:background="@drawable/shape_green"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_add"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ksad_reward_follow_add" />
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="6dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment