Skip to content

Commit

Permalink
[Fix]: 首页UI
Browse files Browse the repository at this point in the history
  • Loading branch information
why committed Dec 19, 2023
1 parent fd630dc commit 7d271a5
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xiaoyv.bangumi.ui.discover.mono.binder

import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.xiaoyv.bangumi.databinding.FragmentOverviewCharacterItemBinding
import com.xiaoyv.common.config.bean.AdapterTypeItem
Expand All @@ -26,7 +27,7 @@ class MonoGridBinder :
) {
val entity = item?.entity as? SampleImageEntity ?: return
val binding = holder.binding
binding.ivAvatar.loadImageAnimate(entity.image)
binding.ivAvatar.loadImageAnimate(entity.image, cropType = ImageView.ScaleType.FIT_START)
binding.tvName.text = entity.title
binding.tvPerson.text = entity.desc
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.ImageView
import androidx.lifecycle.LifecycleOwner
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
Expand Down Expand Up @@ -83,7 +84,7 @@ class PersonActivity : BaseViewModelActivity<ActivityPersonBinding, PersonViewMo
debugLog { "Person: " + entity.toJson(true) }

binding.ivCover.loadImageBlurBackground(entity.poster)
binding.ivCover.loadImageAnimate(entity.poster, centerCrop = false)
binding.ivCover.loadImageAnimate(entity.poster, cropType = ImageView.ScaleType.FIT_START)
binding.ivCover.setOnFastLimitClickListener {
RouteHelper.jumpPreviewImage(entity.posterLarge)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaoyv.bangumi.ui.feature.person.character

import android.widget.ImageView
import androidx.core.view.isVisible
import com.xiaoyv.bangumi.databinding.FragmentMediaPageItemBinding
import com.xiaoyv.common.api.parser.entity.CharacterEntity
Expand All @@ -18,7 +19,7 @@ class PersonCharacterAdapter : BaseQuickDiffBindingAdapter<CharacterEntity,
FragmentMediaPageItemBinding>(IdDiffItemCallback()) {

override fun BaseQuickBindingHolder<FragmentMediaPageItemBinding>.converted(item: CharacterEntity) {
binding.ivCover.loadImageAnimate(item.avatar)
binding.ivCover.loadImageAnimate(item.avatar, cropType = ImageView.ScaleType.FIT_START)
binding.tvTitle.text = item.nameCn.ifBlank { item.nameNative }
binding.tvTag.text = String.format("x%d", item.from.size)
binding.tvSource.isVisible = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaoyv.bangumi.ui.feature.person.cooperate

import android.widget.ImageView
import com.xiaoyv.bangumi.databinding.FragmentPersonCooperateItemBinding
import com.xiaoyv.common.api.parser.entity.PersonEntity
import com.xiaoyv.common.helper.callback.IdDiffItemCallback
Expand All @@ -17,7 +18,7 @@ class PersonCooperateAdapter : BaseQuickDiffBindingAdapter<PersonEntity.RecentCo
FragmentPersonCooperateItemBinding>(IdDiffItemCallback()) {

override fun BaseQuickBindingHolder<FragmentPersonCooperateItemBinding>.converted(item: PersonEntity.RecentCooperate) {
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.ivAvatar.loadImageAnimate(item.avatar, cropType = ImageView.ScaleType.FIT_START)
binding.tvName.text = item.name
binding.tvTimes.text = String.format("x%d", item.times)
binding.tvJobs.text = item.jobs.joinToString("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.xiaoyv.bangumi.ui.feature.person.overview.binder

import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.xiaoyv.bangumi.R
import com.xiaoyv.bangumi.databinding.FragmentPersonOverviewListBinding
import com.xiaoyv.bangumi.databinding.FragmentPersonOverviewListCharacterBinding
import com.xiaoyv.bangumi.ui.feature.person.overview.PersonOverviewAdapter
import com.xiaoyv.common.api.parser.entity.CharacterEntity
import com.xiaoyv.common.config.bean.AdapterTypeItem
import com.xiaoyv.common.helper.callback.IdDiffItemCallback
Expand Down Expand Up @@ -41,7 +41,7 @@ class PersonOverviewCharacterBinder(
override fun onBind(
holder: BaseQuickBindingHolder<FragmentPersonOverviewListBinding>,
position: Int,
item: AdapterTypeItem?
item: AdapterTypeItem?,
) {
item ?: return
holder.binding.tvItemTitle.text = item.title
Expand All @@ -55,7 +55,7 @@ class PersonOverviewCharacterBinder(
override fun onCreate(
context: Context,
parent: ViewGroup,
viewType: Int
viewType: Int,
): BaseQuickBindingHolder<FragmentPersonOverviewListBinding> {
val binding =
FragmentPersonOverviewListBinding.inflate(context.inflater, parent, false)
Expand All @@ -71,9 +71,9 @@ class PersonOverviewCharacterBinder(
FragmentPersonOverviewListCharacterBinding>(IdDiffItemCallback()) {

override fun BaseQuickBindingHolder<FragmentPersonOverviewListCharacterBinding>.converted(
item: CharacterEntity
item: CharacterEntity,
) {
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.ivAvatar.loadImageAnimate(item.avatar, cropType = ImageView.ScaleType.FIT_START)
binding.tvName.text = item.nameNative
binding.tvNameCn.text = item.nameCn

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.xiaoyv.bangumi.ui.feature.person.overview.binder

import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.isInvisible
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.xiaoyv.bangumi.R
import com.xiaoyv.bangumi.databinding.FragmentPersonOverviewListBinding
import com.xiaoyv.bangumi.databinding.FragmentPersonOverviewListVoiceBinding
import com.xiaoyv.bangumi.ui.feature.person.overview.PersonOverviewAdapter
import com.xiaoyv.common.api.parser.entity.PersonEntity
import com.xiaoyv.common.config.bean.AdapterTypeItem
import com.xiaoyv.common.helper.callback.IdDiffItemCallback
Expand Down Expand Up @@ -42,7 +42,7 @@ class PersonOverviewVoiceBinder(
override fun onBind(
holder: BaseQuickBindingHolder<FragmentPersonOverviewListBinding>,
position: Int,
item: AdapterTypeItem?
item: AdapterTypeItem?,
) {
item ?: return
holder.binding.tvItemTitle.text = item.title
Expand All @@ -56,7 +56,7 @@ class PersonOverviewVoiceBinder(
override fun onCreate(
context: Context,
parent: ViewGroup,
viewType: Int
viewType: Int,
): BaseQuickBindingHolder<FragmentPersonOverviewListBinding> {
val binding =
FragmentPersonOverviewListBinding.inflate(context.inflater, parent, false)
Expand All @@ -73,10 +73,13 @@ class PersonOverviewVoiceBinder(
FragmentPersonOverviewListVoiceBinding>(IdDiffItemCallback()) {

override fun BaseQuickBindingHolder<FragmentPersonOverviewListVoiceBinding>.converted(
item: PersonEntity.RecentlyPerformer
item: PersonEntity.RecentlyPerformer,
) {
binding.ivAvatar.isInvisible = item.character.id.isBlank()
binding.ivAvatar.loadImageAnimate(item.character.avatar)
binding.ivAvatar.loadImageAnimate(
item.character.avatar,
cropType = ImageView.ScaleType.FIT_START
)
binding.tvName.text = item.character.characterName
binding.tvNameCn.text = item.character.personJob

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaoyv.bangumi.ui.media.detail.character

import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import com.xiaoyv.bangumi.databinding.FragmentMediaCharacterItemBinding
Expand All @@ -18,24 +19,28 @@ class MediaCharacterAdapter : BaseQuickDiffBindingAdapter<MediaCharacterEntity,
FragmentMediaCharacterItemBinding>(ItemDiffItemCallback) {

override fun BaseQuickBindingHolder<FragmentMediaCharacterItemBinding>.converted(item: MediaCharacterEntity) {
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.ivAvatar.loadImageAnimate(item.avatar, cropType = ImageView.ScaleType.FIT_START)
binding.tvTitle.text = String.format("%s/%s", item.titleNative, item.titleCn)
binding.tvJob.text = item.personJob + ";" + item.personSex
binding.tvComment.text = String.format("讨论:%s", item.commentCount)
binding.tvComment.isVisible = item.commentCount.isNotBlank()
binding.tvJob.text = buildString {
append(item.personJob)
append(";")
append(item.personSex)
}
}

private object ItemDiffItemCallback : DiffUtil.ItemCallback<MediaCharacterEntity>() {
override fun areItemsTheSame(
oldItem: MediaCharacterEntity,
newItem: MediaCharacterEntity
newItem: MediaCharacterEntity,
): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(
oldItem: MediaCharacterEntity,
newItem: MediaCharacterEntity
newItem: MediaCharacterEntity,
): Boolean {
return oldItem == newItem
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaoyv.bangumi.ui.media.detail.maker

import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import com.xiaoyv.bangumi.databinding.FragmentMediaMakerItemBinding
Expand All @@ -18,7 +19,7 @@ class MediaMakerAdapter : BaseQuickDiffBindingAdapter<MediaMakerEntity,
FragmentMediaMakerItemBinding>(ItemDiffItemCallback) {

override fun BaseQuickBindingHolder<FragmentMediaMakerItemBinding>.converted(item: MediaMakerEntity) {
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.ivAvatar.loadImageAnimate(item.avatar, ImageView.ScaleType.FIT_START)
binding.tvTitle.text = String.format("%s/%s", item.titleNative, item.titleCn)
binding.tvJob.text = item.personInfo.joinToString(";")
binding.tvComment.text = String.format("讨论:%s", item.commentCount)
Expand All @@ -30,14 +31,14 @@ class MediaMakerAdapter : BaseQuickDiffBindingAdapter<MediaMakerEntity,
private object ItemDiffItemCallback : DiffUtil.ItemCallback<MediaMakerEntity>() {
override fun areItemsTheSame(
oldItem: MediaMakerEntity,
newItem: MediaMakerEntity
newItem: MediaMakerEntity,
): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(
oldItem: MediaMakerEntity,
newItem: MediaMakerEntity
newItem: MediaMakerEntity,
): Boolean {
return oldItem == newItem
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xiaoyv.bangumi.ui.media.detail.overview.binder

import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.xiaoyv.bangumi.R
Expand Down Expand Up @@ -53,7 +54,7 @@ class OverviewCharacterBinder(private val clickItemListener: (MediaDetailEntity.
private class ItemAdapter : BaseQuickDiffBindingAdapter<MediaDetailEntity.MediaCharacter,
FragmentOverviewCharacterItemBinding>(IdDiffItemCallback()) {
override fun BaseQuickBindingHolder<FragmentOverviewCharacterItemBinding>.converted(item: MediaDetailEntity.MediaCharacter) {
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.ivAvatar.loadImageAnimate(item.avatar, cropType = ImageView.ScaleType.FIT_START)
binding.tvName.text = item.characterNameCn.ifBlank { item.characterName }
binding.tvJobs.text = item.jobs.joinToString(";")
binding.tvCommentCount.text = String.format("讨论:%d", item.saveCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object ConfigHelper {
}

fun isImageAnimation(): Boolean {
return SPStaticUtils.getBoolean(KEY_IMAGE_ANIMATION, false)
return SPStaticUtils.getBoolean(KEY_IMAGE_ANIMATION, true)
}

fun setImageAnimation(enable: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.xiaoyv.common.helper

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import androidx.annotation.FloatRange
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.load.resource.bitmap.TransformationUtils
import com.bumptech.glide.load.resource.bitmap.TransformationUtils.PAINT_FLAGS
import java.nio.charset.Charset
import java.security.MessageDigest

class PositionCropTransformation(
@FloatRange(from = 0.0, to = 1.0) val xPercentage: Float = 0.0f,
@FloatRange(from = 0.0, to = 1.0) val yPercentage: Float = 0.0f,
) : BitmapTransformation() {

companion object {
private const val ID = "com.testbook.tbapp.base.utils.TopRightCropTransformation"
private val idBytes = ID.toByteArray(Charset.forName("UTF-8"))
}

override fun updateDiskCacheKey(messageDigest: MessageDigest) {
messageDigest.update(idBytes)
}

override fun transform(
pool: BitmapPool,
toTransform: Bitmap,
outWidth: Int,
outHeight: Int,
): Bitmap? {
val toReuse = pool[outWidth, outHeight, toTransform.config]
val transformed = crop(toReuse, toTransform, outWidth, outHeight, xPercentage, yPercentage)
if (toReuse != transformed) {
pool.put(toReuse)
}
return transformed
}

/**
* A potentially expensive operation to crop the given Bitmap so that it fills the given dimensions. This operation
* is significantly less expensive in terms of memory if a mutable Bitmap with the given dimensions is passed in
* as well.
*
* @param recycled A mutable Bitmap with dimensions width and height that we can load the cropped portion of toCrop
* into.
* @param toCrop The Bitmap to resize.
* @param width The width in pixels of the final Bitmap.
* @param height The height in pixels of the final Bitmap.
* @param xPercentage The horizontal percentage of the crop. 0.0f => left, 0.5f => center, 1.0f => right or anything in between 0 and 1
* @param yPercentage The vertical percentage of the crop. 0.0f => top, 0.5f => center, 1.0f => bottom or anything in between 0 and 1
* @return The resized Bitmap (will be recycled if recycled is not null).
*/
private fun crop(
recycled: Bitmap?,
toCrop: Bitmap?,
width: Int,
height: Int,
xPercentage: Float,
yPercentage: Float,
): Bitmap? {
if (toCrop == null) {
return null
} else if (toCrop.width == width && toCrop.height == height) {
return toCrop
}
// From ImageView/Bitmap.createScaledBitmap.
val scale: Float
var dx = 0f
var dy = 0f
val m = Matrix()
if (toCrop.width * height > width * toCrop.height) {
scale = height.toFloat() / toCrop.height.toFloat()
dx = width - toCrop.width * scale
dx *= xPercentage
} else {
scale = width.toFloat() / toCrop.width.toFloat()
dy = height - toCrop.height * scale
dy *= yPercentage
}
m.setScale(scale, scale)
m.postTranslate((dx + 0.5f), (dy + 0.5f))
val result: Bitmap = recycled ?: Bitmap.createBitmap(width, height, getSafeConfig(toCrop)!!)

// We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given.
TransformationUtils.setAlpha(toCrop, result)
val canvas = Canvas(result)
val paint = Paint(PAINT_FLAGS)
canvas.drawBitmap(toCrop, m, paint)
return result
}

private fun getSafeConfig(bitmap: Bitmap): Bitmap.Config {
return bitmap.config
}
}
Loading

0 comments on commit 7d271a5

Please sign in to comment.