加入自訂圖片功能

This commit is contained in:
Raymond Yang 2023-08-03 13:33:36 +08:00
parent 2d37a734e6
commit b227e8c61f
4 changed files with 112 additions and 7 deletions

View File

@ -48,5 +48,10 @@ dependencies {
androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
// EasyWindow
implementation("com.github.getActivity:EasyWindow:10.2") implementation("com.github.getActivity:EasyWindow:10.2")
// Glide
implementation("com.github.bumptech.glide:glide:4.11.0")
} }

View File

@ -5,6 +5,12 @@
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.TYPE_APPLICATION_OVERLAY" /> <uses-permission android:name="android.permission.TYPE_APPLICATION_OVERLAY" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<application <application
android:name=".MyApp" android:name=".MyApp"
android:allowBackup="true" android:allowBackup="true"

View File

@ -1,9 +1,19 @@
package com.ray650128.floatingwindow.ui package com.ray650128.floatingwindow.ui
import android.Manifest
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.View import android.view.View
import android.widget.AdapterView import android.widget.AdapterView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.ray650128.floatingwindow.databinding.ActivitySettingBinding import com.ray650128.floatingwindow.databinding.ActivitySettingBinding
import com.ray650128.floatingwindow.dp import com.ray650128.floatingwindow.dp
import com.ray650128.floatingwindow.utils.DensityUtil import com.ray650128.floatingwindow.utils.DensityUtil
@ -23,6 +33,35 @@ class SettingActivity : AppCompatActivity() {
private var windowIcon = FloatingWindowHelperUtils.IconType.CHICKEN private var windowIcon = FloatingWindowHelperUtils.IconType.CHICKEN
private val requestPermission =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
permissions.entries.forEach {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
if (it.key == Manifest.permission.READ_EXTERNAL_STORAGE && it.value) {
pickImage()
}
} else {
if (it.key == Manifest.permission.READ_MEDIA_IMAGES && it.value) {
pickImage()
}
}
Log.e("DEBUG", "${it.key} = ${it.value}")
}
}
private val galleryLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data = result.data
imageUri = data?.data
Glide.with(this).load(imageUri).into(binding.imgPreview)
FloatingWindowHelperUtils.setIcon(windowIcon, imageUri)
//workingCheckItemAdapter.setImageUri(currentPosition, currentUri)
//checkPassButton()
}
}
private var imageUri: Uri? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = ActivitySettingBinding.inflate(layoutInflater) binding = ActivitySettingBinding.inflate(layoutInflater)
@ -32,6 +71,9 @@ class SettingActivity : AppCompatActivity() {
yOffset = PreferenceUtil.yOffset yOffset = PreferenceUtil.yOffset
windowGravity = FloatingWindowHelperUtils.GravityType.fromInt(PreferenceUtil.gravity) windowGravity = FloatingWindowHelperUtils.GravityType.fromInt(PreferenceUtil.gravity)
windowIcon = FloatingWindowHelperUtils.IconType.fromInt(PreferenceUtil.icon) windowIcon = FloatingWindowHelperUtils.IconType.fromInt(PreferenceUtil.icon)
imageUri = if (PreferenceUtil.iconPath.isNullOrEmpty()) null else {
Uri.parse(PreferenceUtil.iconPath)
}
binding.apply { binding.apply {
spGravity.apply { spGravity.apply {
@ -51,7 +93,14 @@ class SettingActivity : AppCompatActivity() {
onItemSelectedListener = object : AdapterView.OnItemSelectedListener { onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
windowIcon = FloatingWindowHelperUtils.IconType.fromIndex(position) windowIcon = FloatingWindowHelperUtils.IconType.fromIndex(position)
FloatingWindowHelperUtils.setIcon(windowIcon) FloatingWindowHelperUtils.setIcon(windowIcon, imageUri)
if (windowIcon == FloatingWindowHelperUtils.IconType.CUSTOM) {
imgPreview.isVisible = true
btnPickPhoto.isVisible = true
} else {
imgPreview.isVisible = false
btnPickPhoto.isVisible = false
}
} }
override fun onNothingSelected(parent: AdapterView<*>?) {} override fun onNothingSelected(parent: AdapterView<*>?) {}
@ -138,11 +187,24 @@ class SettingActivity : AppCompatActivity() {
} }
}*/ }*/
if (imageUri != null) {
Glide.with(this@SettingActivity).load(imageUri).into(imgPreview)
}
btnPickPhoto.setOnClickListener {
requestPermission.launch(
arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_MEDIA_IMAGES
)
)
}
btnOk.setOnClickListener { btnOk.setOnClickListener {
PreferenceUtil.xOffset = xOffset PreferenceUtil.xOffset = xOffset
PreferenceUtil.yOffset = yOffset PreferenceUtil.yOffset = yOffset
PreferenceUtil.gravity = windowGravity.type PreferenceUtil.gravity = windowGravity.type
PreferenceUtil.icon = windowIcon.value PreferenceUtil.icon = windowIcon.value
PreferenceUtil.iconPath = imageUri?.toString()
finish() finish()
} }
@ -155,10 +217,16 @@ class SettingActivity : AppCompatActivity() {
) )
) )
FloatingWindowHelperUtils.setIcon( FloatingWindowHelperUtils.setIcon(
FloatingWindowHelperUtils.IconType.fromInt( FloatingWindowHelperUtils.IconType.fromInt(PreferenceUtil.icon),
PreferenceUtil.icon)) Uri.parse(PreferenceUtil.iconPath)
)
finish() finish()
} }
} }
} }
private fun pickImage() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
galleryLauncher.launch(intent)
}
} }

View File

@ -1,5 +1,9 @@
package com.ray650128.floatingwindow.utils package com.ray650128.floatingwindow.utils
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.provider.MediaStore
import android.view.Gravity import android.view.Gravity
import android.view.MotionEvent import android.view.MotionEvent
import android.view.WindowManager import android.view.WindowManager
@ -30,7 +34,18 @@ object FloatingWindowHelperUtils {
}) })
setXOffset(xOffset) setXOffset(xOffset)
setYOffset(yOffset) setYOffset(yOffset)
setImageDrawable(android.R.id.icon, windowIcon.value) if (windowIcon == IconType.CUSTOM) {
if (PreferenceUtil.iconPath.isNullOrEmpty()) {
setImageDrawable(android.R.id.icon, windowIcon.value)
} else {
val uri = Uri.parse(PreferenceUtil.iconPath)
val bitmap = BitmapFactory.decodeStream(MyApp.appContext.contentResolver.openInputStream(uri))
val drawable = BitmapDrawable(MyApp.appContext.resources, bitmap)
setImageDrawable(android.R.id.icon, drawable)
}
} else {
setImageDrawable(android.R.id.icon, windowIcon.value)
}
setOnTouchListener { window, view, event -> setOnTouchListener { window, view, event ->
var touch = false var touch = false
when (event.action) { when (event.action) {
@ -99,8 +114,18 @@ object FloatingWindowHelperUtils {
window.update() window.update()
} }
fun setIcon(value: IconType) { fun setIcon(value: IconType, inputUri: Uri? = null) {
window.setImageDrawable(android.R.id.icon, value.value) if (value == IconType.CUSTOM) {
if (inputUri == null) {
window.setImageDrawable(android.R.id.icon, value.value)
} else {
val bitmap = BitmapFactory.decodeStream(MyApp.appContext.contentResolver.openInputStream(inputUri))
val drawable = BitmapDrawable(MyApp.appContext.resources, bitmap)
window.setImageDrawable(android.R.id.icon, drawable)
}
} else {
window.setImageDrawable(android.R.id.icon, value.value)
}
window.update() window.update()
} }
@ -126,7 +151,8 @@ object FloatingWindowHelperUtils {
GURA(R.drawable.ic_gura, 7), GURA(R.drawable.ic_gura, 7),
RUSHIA2(R.drawable.ic_uruha_rushia2, 8), RUSHIA2(R.drawable.ic_uruha_rushia2, 8),
RUSHIA3(R.drawable.ic_uruha_rushia3, 9), RUSHIA3(R.drawable.ic_uruha_rushia3, 9),
HOSHIMACHI_SUISEI(R.drawable.ic_hoshimachii_suisei, 10); HOSHIMACHI_SUISEI(R.drawable.ic_hoshimachii_suisei, 10),
CUSTOM(R.mipmap.ic_launcher, 11);
companion object { companion object {
fun fromInt(value: Int) = IconType.values().first { it.value == value } fun fromInt(value: Int) = IconType.values().first { it.value == value }