feat: 增加推荐壁纸

This commit is contained in:
HuanChengFly 2021-07-20 14:28:01 +08:00
parent 68cc44b105
commit 7ed64c1faf
8 changed files with 249 additions and 101 deletions

View File

@ -18,6 +18,7 @@ import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import androidx.core.text.HtmlCompat
import androidx.palette.graphics.Palette
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import butterknife.BindView
import com.bumptech.glide.Glide
@ -26,21 +27,19 @@ import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.gyf.immersionbar.ImmersionBar
import com.huanchengfly.tieba.post.BaseApplication
import com.huanchengfly.tieba.post.*
import com.huanchengfly.tieba.post.BaseApplication.Companion.translucentBackground
import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.adapters.ThemeColorAdapter
import com.huanchengfly.tieba.post.adapters.WallpaperAdapter
import com.huanchengfly.tieba.post.api.LiteApi
import com.huanchengfly.tieba.post.api.retrofit.doIfSuccess
import com.huanchengfly.tieba.post.components.MyImageEngine
import com.huanchengfly.tieba.post.components.MyLinearLayoutManager
import com.huanchengfly.tieba.post.components.dividers.HorizontalSpacesDecoration
import com.huanchengfly.tieba.post.components.transformations.BlurTransformation
import com.huanchengfly.tieba.post.enableChangingLayoutTransition
import com.huanchengfly.tieba.post.interfaces.OnItemClickListener
import com.huanchengfly.tieba.post.toastShort
import com.huanchengfly.tieba.post.ui.theme.utils.ThemeUtils
import com.huanchengfly.tieba.post.utils.ColorUtils
import com.huanchengfly.tieba.post.utils.ImageUtil
import com.huanchengfly.tieba.post.utils.PermissionUtil
import com.huanchengfly.tieba.post.utils.ThemeUtil
import com.huanchengfly.tieba.post.utils.*
import com.huanchengfly.tieba.post.utils.ThemeUtil.TRANSLUCENT_THEME_DARK
import com.huanchengfly.tieba.post.utils.ThemeUtil.TRANSLUCENT_THEME_LIGHT
import com.huanchengfly.tieba.post.widgets.theme.TintMaterialButton
@ -51,6 +50,8 @@ import com.yanzhenjie.permission.Action
import com.yanzhenjie.permission.runtime.Permission
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import java.io.File
class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBarChangeListener, ColorPickerDialogListener {
@ -62,6 +63,12 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar
@BindView(R.id.select_color)
lateinit var mSelectColor: View
@BindView(R.id.recommend_wallpapers)
lateinit var recommendWallpapers: View
@BindView(R.id.wallpapers_rv)
lateinit var recommendWallpapersRv: RecyclerView
@BindView(R.id.progress)
lateinit var mProgress: View
@ -80,41 +87,85 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar
@BindView(R.id.color_theme)
lateinit var colorTheme: ViewGroup
private var mAdapter: ThemeColorAdapter? = null
var wallpapers: List<String>? = null
set(value) {
field = value
refreshWallpapers()
}
private val wallpaperAdapter: WallpaperAdapter by lazy { WallpaperAdapter(this) }
private val themeColorAdapter: ThemeColorAdapter by lazy { ThemeColorAdapter(this) }
private fun launchUCrop(sourceUri: Uri) {
mProgress.visibility = View.VISIBLE
Glide.with(this)
.asBitmap()
.load(sourceUri)
.into(object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) {}
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
mProgress.visibility = View.GONE
val file =
ImageUtil.bitmapToFile(resource, File(cacheDir, "origin_background.jpg"))
val sourceFileUri = Uri.fromFile(file)
val destUri = Uri.fromFile(File(filesDir, "cropped_background.jpg"))
val height = BaseApplication.ScreenInfo.EXACT_SCREEN_HEIGHT.toFloat()
val width = BaseApplication.ScreenInfo.EXACT_SCREEN_WIDTH.toFloat()
UCrop.of(sourceFileUri, destUri)
.withAspectRatio(width / height, 1f)
.withOptions(UCrop.Options().apply {
setShowCropFrame(true)
setShowCropGrid(true)
setStatusBarColor(
ColorUtils.getDarkerColor(
ThemeUtils.getColorByAttr(
this@TranslucentThemeActivity,
R.attr.colorPrimary
)
)
)
setToolbarColor(
ThemeUtils.getColorByAttr(
this@TranslucentThemeActivity,
R.attr.colorPrimary
)
)
setToolbarWidgetColor(
ThemeUtils.getColorByAttr(
this@TranslucentThemeActivity,
R.attr.colorTextOnPrimary
)
)
setActiveControlsWidgetColor(
ThemeUtils.getColorByAttr(
this@TranslucentThemeActivity,
R.attr.colorAccent
)
)
setLogoColor(
ThemeUtils.getColorByAttr(
this@TranslucentThemeActivity,
R.attr.colorPrimary
)
)
setCompressionFormat(Bitmap.CompressFormat.JPEG)
})
.start(this@TranslucentThemeActivity)
}
override fun onLoadFailed(errorDrawable: Drawable?) {
mProgress.visibility = View.GONE
toastShort(R.string.text_load_failed)
}
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_CHOOSE && resultCode == Activity.RESULT_OK) {
val sourceUri = Matisse.obtainResult(data)[0]
Glide.with(this)
.asDrawable()
.load(sourceUri)
.into(object : CustomTarget<Drawable>() {
override fun onLoadCleared(placeholder: Drawable?) {}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
val bitmap = ImageUtil.drawableToBitmap(resource)
val file = ImageUtil.bitmapToFile(bitmap, File(cacheDir, "origin_background.jpg"))
val sourceFileUri = Uri.fromFile(file)
val destUri = Uri.fromFile(File(filesDir, "cropped_background.jpg"))
val height = BaseApplication.ScreenInfo.EXACT_SCREEN_HEIGHT.toFloat()
val width = BaseApplication.ScreenInfo.EXACT_SCREEN_WIDTH.toFloat()
val uCropOptions = UCrop.Options()
uCropOptions.setShowCropFrame(true)
uCropOptions.setShowCropGrid(true)
uCropOptions.setStatusBarColor(ColorUtils.getDarkerColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorPrimary)))
uCropOptions.setToolbarColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorPrimary))
uCropOptions.setToolbarWidgetColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorTextOnPrimary))
uCropOptions.setActiveWidgetColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorAccent))
uCropOptions.setActiveControlsWidgetColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorAccent))
uCropOptions.setLogoColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorPrimary))
uCropOptions.setCompressionFormat(Bitmap.CompressFormat.JPEG)
UCrop.of(sourceFileUri, destUri)
.withAspectRatio(width / height, 1f)
.withOptions(uCropOptions)
.start(this@TranslucentThemeActivity)
}
})
launchUCrop(sourceUri)
} else if (resultCode == Activity.RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
mUri = UCrop.getOutput(data!!)
invalidateFinishBtn()
@ -125,6 +176,15 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar
}
}
private fun refreshWallpapers() {
if (wallpapers.isNullOrEmpty()) {
recommendWallpapers.visibility = View.GONE
} else {
recommendWallpapers.visibility = View.VISIBLE
wallpaperAdapter.setData(wallpapers)
}
}
private fun refreshBackground() {
mProgress.visibility = View.VISIBLE
if (mUri == null) {
@ -148,7 +208,7 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar
val bitmap = ImageUtil.drawableToBitmap(resource)
findViewById(R.id.background).background = BitmapDrawable(resources, bitmap)
mPalette = Palette.from(bitmap).generate()
mAdapter!!.setPalette(mPalette)
themeColorAdapter.setPalette(mPalette)
mSelectColor.visibility = View.VISIBLE
mProgress.visibility = View.GONE
}
@ -183,15 +243,37 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar
).forEach {
it.setOnClickListener(this@TranslucentThemeActivity)
}
wallpapers =
CacheUtil.getCache(this, "recommend_wallpapers", List::class.java) as List<String>?
colorTheme.enableChangingLayoutTransition()
mAdapter = ThemeColorAdapter(this)
mAdapter!!.onItemClickListener = OnItemClickListener { _: View?, themeColor: Int, _: Int, _: Int ->
wallpaperAdapter.setOnItemClickListener { _, item, _ ->
launchUCrop(Uri.parse(item))
}
recommendWallpapersRv.addItemDecoration(
HorizontalSpacesDecoration(
0,
0,
16.dpToPx(),
16.dpToPx(),
false
)
)
recommendWallpapersRv.adapter = wallpaperAdapter
recommendWallpapersRv.layoutManager =
LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
themeColorAdapter.onItemClickListener =
OnItemClickListener { _: View?, themeColor: Int, _: Int, _: Int ->
appPreferences.translucentPrimaryColor = toString(themeColor)
ThemeUtils.refreshUI(this)
}
(findViewById(R.id.select_color_recycler_view) as RecyclerView).apply {
layoutManager = MyLinearLayoutManager(this@TranslucentThemeActivity, MyLinearLayoutManager.HORIZONTAL, false)
adapter = mAdapter
addItemDecoration(HorizontalSpacesDecoration(0, 0, 16.dpToPx(), 16.dpToPx(), false))
layoutManager = MyLinearLayoutManager(
this@TranslucentThemeActivity,
MyLinearLayoutManager.HORIZONTAL,
false
)
adapter = themeColorAdapter
}
alpha = appPreferences.translucentBackgroundAlpha
blur = appPreferences.translucentBackgroundBlur
@ -212,6 +294,18 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar
}
refreshBackground()
refreshTheme()
fetchWallpapers()
}
private fun fetchWallpapers() {
launch(IO + job) {
LiteApi.instance
.wallpapersAsync()
.doIfSuccess {
CacheUtil.putCache(this@TranslucentThemeActivity, "recommend_wallpapers", it)
wallpapers = it
}
}
}
@SuppressLint("ApplySharedPref")

View File

@ -0,0 +1,20 @@
package com.huanchengfly.tieba.post.adapters
import android.content.Context
import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.adapters.base.BaseSingleTypeAdapter
import com.huanchengfly.tieba.post.components.MyViewHolder
import com.huanchengfly.tieba.post.utils.ImageUtil
class WallpaperAdapter(context: Context) : BaseSingleTypeAdapter<String>(context) {
override fun getItemLayoutId(): Int = R.layout.item_wallpaper
override fun convert(viewHolder: MyViewHolder, item: String, position: Int) {
ImageUtil.load(
viewHolder.getView(R.id.image_view),
ImageUtil.LOAD_TYPE_SMALL_PIC,
item,
true
)
}
}

View File

@ -1,47 +0,0 @@
package com.huanchengfly.tieba.post.components.dividers;
import android.graphics.Rect;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class HorizontalSpacesDecoration extends RecyclerView.ItemDecoration {
private int top;
private int bottom;
private int left;
private int right;
public HorizontalSpacesDecoration(int space) {
this(space, space, space, space);
}
public HorizontalSpacesDecoration(int top, int bottom, int left, int right) {
this.top = top;
this.bottom = bottom;
this.left = left;
this.right = right;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
RecyclerView.Adapter adapter = parent.getAdapter();
if (adapter == null) {
return;
}
if (position == 0) {
outRect.left = left;
outRect.right = right / 2;
} else if (position == adapter.getItemCount() - 1) {
outRect.left = left / 2;
outRect.right = right;
} else {
outRect.left = left / 2;
outRect.right = right / 2;
}
outRect.bottom = bottom;
outRect.top = top;
}
}

View File

@ -0,0 +1,36 @@
package com.huanchengfly.tieba.post.components.dividers
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
class HorizontalSpacesDecoration @JvmOverloads constructor(
private val top: Int,
private val bottom: Int,
private val left: Int,
private val right: Int,
private val spaceOnEdge: Boolean = true
) : ItemDecoration() {
constructor(space: Int) : this(space, space, space, space)
override fun getItemOffsets(
outRect: Rect, view: View,
parent: RecyclerView, state: RecyclerView.State
) {
val position = parent.getChildAdapterPosition(view)
val adapter = parent.adapter ?: return
if (position == 0) {
outRect.left = if (spaceOnEdge) left else 0
outRect.right = right / 2
} else if (position == adapter.itemCount - 1) {
outRect.left = left / 2
outRect.right = if (spaceOnEdge) right else 0
} else {
outRect.left = left / 2
outRect.right = right / 2
}
outRect.bottom = bottom
outRect.top = top
}
}

View File

@ -52,13 +52,25 @@
<com.huanchengfly.tieba.post.widgets.theme.TintLinearLayout
android:background="@drawable/bg_top_radius_10dp"
app:backgroundTint="@color/theme_color_card_light"
app:behavior_peekHeight="120dp"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
android:paddingTop="8dp"
android:paddingBottom="16dp"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<com.huanchengfly.tieba.post.widgets.theme.TintView
android:layout_width="64dp"
android:layout_height="4dp"
android:background="@drawable/bg_radius_50dp"
app:backgroundTint="@color/default_color_on_toolbar_bar"
android:layout_gravity="center_horizontal" />
<com.huanchengfly.tieba.post.widgets.theme.TintTextView
android:layout_marginTop="16dp"
app:tint="@color/color_text"
android:textSize="14sp"
android:textStyle="bold"
@ -147,7 +159,7 @@
<FrameLayout
android:id="@+id/custom_color"
android:padding="8dp"
android:layout_marginEnd="16dp"
android:clipToPadding="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
@ -213,6 +225,28 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/recommend_wallpapers">
<com.huanchengfly.tieba.post.widgets.theme.TintTextView
android:layout_marginTop="16dp"
android:textSize="14sp"
android:textStyle="bold"
android:text="@string/title_translucent_theme_wallpapers"
app:tint="@color/color_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/wallpapers_rv"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.huanchengfly.tieba.post.widgets.theme.TintTextView
android:layout_marginTop="12dp"
app:tint="@color/color_text_secondary"

View File

@ -2,7 +2,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="8dp"
android:paddingBottom="8dp"
android:clipToPadding="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_view"
android:layout_width="60dp"
android:layout_height="120dp" />
</FrameLayout>

View File

@ -457,4 +457,5 @@
<string name="title_ui_settings">界面设置</string>
<string name="dark_color">深色</string>
<string name="light_color">浅色</string>
<string name="title_translucent_theme_wallpapers">推荐壁纸</string>
</resources>