pull/32/head
kunfei 5 years ago
parent 806c3227ca
commit dea372a03c
  1. 57
      app/src/main/java/io/legado/app/ui/readbook/config/BgTextConfigDialog.kt
  2. 14
      app/src/main/res/drawable/ic_image.xml
  3. 4
      app/src/main/res/layout/dialog_read_bg_text.xml
  4. 21
      app/src/main/res/layout/item_bg_image.xml
  5. 1
      app/src/main/res/values/strings.xml

@ -1,6 +1,9 @@
package io.legado.app.ui.readbook.config package io.legado.app.ui.readbook.config
import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.util.DisplayMetrics
@ -9,11 +12,20 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jaredrummler.android.colorpicker.ColorPickerDialog import com.jaredrummler.android.colorpicker.ColorPickerDialog
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.help.ImageLoader
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.help.permission.Permissions
import io.legado.app.help.permission.PermissionsCompat
import io.legado.app.ui.readbook.Help import io.legado.app.ui.readbook.Help
import io.legado.app.utils.getCompatColor
import kotlinx.android.synthetic.main.dialog_read_bg_text.* import kotlinx.android.synthetic.main.dialog_read_bg_text.*
import kotlinx.android.synthetic.main.item_bg_image.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
class BgTextConfigDialog : DialogFragment() { class BgTextConfigDialog : DialogFragment() {
@ -23,6 +35,9 @@ class BgTextConfigDialog : DialogFragment() {
const val BG_COLOR = 122 const val BG_COLOR = 122
} }
private val ResultSelectBg = 123
private lateinit var adapter: BgAdapter
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
@ -58,8 +73,23 @@ class BgTextConfigDialog : DialogFragment() {
ReadBookConfig.save() ReadBookConfig.save()
} }
@SuppressLint("InflateParams")
private fun initData() = with(ReadBookConfig.getConfig()) { private fun initData() = with(ReadBookConfig.getConfig()) {
sw_dark_status_icon.isChecked = statusIconDark() sw_dark_status_icon.isChecked = statusIconDark()
adapter = BgAdapter(requireContext())
recycler_view.layoutManager =
LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
recycler_view.adapter = adapter
val headerView = LayoutInflater.from(requireContext())
.inflate(R.layout.item_bg_image, recycler_view, false)
adapter.addHeaderView(headerView)
headerView.tv_name.text = getString(R.string.select_image)
headerView.iv_bg.setImageResource(R.drawable.ic_image)
headerView.iv_bg.setColorFilter(getCompatColor(R.color.tv_text_default))
headerView.onClick { selectImage() }
requireContext().assets.list("bg/")?.let {
adapter.setItems(it.toList())
}
} }
private fun initView() = with(ReadBookConfig.getConfig()) { private fun initView() = with(ReadBookConfig.getConfig()) {
@ -87,5 +117,32 @@ class BgTextConfigDialog : DialogFragment() {
} }
} }
private fun selectImage() {
PermissionsCompat.Builder(this)
.addPermissions(Permissions.READ_EXTERNAL_STORAGE, Permissions.WRITE_EXTERNAL_STORAGE)
.rationale(R.string.bg_image_per)
.onGranted {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, ResultSelectBg)
Unit
}
.request()
}
class BgAdapter(context: Context) :
SimpleRecyclerAdapter<String>(context, R.layout.item_bg_image) {
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) {
with(holder.itemView) {
ImageLoader.load(context, context.assets.open("bg/$item").readBytes())
.centerCrop()
.setAsFile(iv_bg)
tv_name.text = item
}
}
}
} }

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:fillColor="#000000"
android:pathData="M938.667 0H85.333A85.333 85.333 0 0 0 0 85.333v853.334A85.333 85.333 0 0 0 85.333 1024h853.334A85.333 85.333 0 0 0 1024 938.667V85.333A85.333 85.333 0 0 0 938.667 0zM85.333 42.667h853.334a42.667 42.667 0 0 1 42.666 42.666V640L746.667 405.333a21.333 21.333 0 0 0-30.08 0L480 641.92 321.707 483.627a21.333 21.333 0 0 0-30.08 0l-248.96 248.96V85.333a42.667 42.667 0 0 1 42.666-42.666z m-42.666 896v-145.92l264.106-263.894 452.48 452.48H85.333a42.667 42.667 0 0 1-42.666-42.666z m896 42.666H819.413L510.08 672l221.867-221.867L981.333 699.52v239.147a42.667 42.667 0 0 1-42.666 42.666z" />
<path
android:fillColor="#000000"
android:pathData="M298.667 405.333A106.667 106.667 0 1 0 192 298.667a106.667 106.667 0 0 0 106.667 106.666z m0-170.666a64 64 0 1 1-64 64 64 64 0 0 1 64-64z" />
</vector>

@ -77,12 +77,10 @@
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/bg_image" /> android:text="@string/bg_image" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/bgImgList" android:id="@+id/recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="100dp" /> android:layout_height="100dp" />

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_bg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:contentDescription="@string/bg_image" />
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>

@ -489,5 +489,6 @@
<string name="bg_image">背景图片</string> <string name="bg_image">背景图片</string>
<string name="bg_color">背景颜色</string> <string name="bg_color">背景颜色</string>
<string name="text_color">文字颜色</string> <string name="text_color">文字颜色</string>
<string name="select_image">选择图片</string>
</resources> </resources>

Loading…
Cancel
Save