Skip to content

Commit 5e165e9

Browse files
feat(*): 添加几个图片相关的扩展函数
1 parent d604698 commit 5e165e9

File tree

4 files changed

+171
-4
lines changed

4 files changed

+171
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.pengxh.kt.lite.extensions
2+
3+
import android.content.Context
4+
import android.graphics.Bitmap
5+
import android.graphics.drawable.Drawable
6+
import android.renderscript.Allocation
7+
import android.renderscript.Element
8+
import android.renderscript.RenderScript
9+
import android.renderscript.ScriptIntrinsicBlur
10+
import androidx.core.graphics.drawable.toBitmap
11+
import kotlin.math.roundToInt
12+
13+
14+
private const val BITMAP_SCALE = 0.4f
15+
16+
fun Drawable.toBlurBitmap(context: Context, radius: Float): Bitmap {
17+
val bitmap = this.toBitmap()
18+
19+
// 计算图片缩小后的长宽
20+
val width = (bitmap.width * BITMAP_SCALE).roundToInt()
21+
val height = (bitmap.height * BITMAP_SCALE).roundToInt()
22+
23+
// 将缩小后的图片做为预渲染的图片。
24+
val inputBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false)
25+
// 创建一张渲染后的输出图片。
26+
val outputBitmap = Bitmap.createBitmap(inputBitmap)
27+
28+
// 创建RenderScript内核对象
29+
val rs = RenderScript.create(context)
30+
// 创建一个模糊效果的RenderScript的工具对象
31+
val blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
32+
val tmpIn = Allocation.createFromBitmap(rs, inputBitmap)
33+
val tmpOut = Allocation.createFromBitmap(rs, outputBitmap)
34+
blurScript.setRadius(radius)
35+
blurScript.setInput(tmpIn)
36+
blurScript.forEach(tmpOut)
37+
tmpOut.copyTo(outputBitmap)
38+
return outputBitmap
39+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.pengxh.kt.lite.extensions
2+
3+
import android.graphics.Bitmap
4+
import android.graphics.Color
5+
import android.graphics.drawable.BitmapDrawable
6+
import android.graphics.drawable.ColorDrawable
7+
import android.graphics.drawable.Drawable
8+
import android.graphics.drawable.TransitionDrawable
9+
import android.widget.ImageView
10+
11+
12+
fun ImageView.switchBackground(blurBitmap: Bitmap) {
13+
var transitionDrawable: TransitionDrawable? = null
14+
val lastDrawable: Drawable
15+
when (this.drawable) {
16+
is TransitionDrawable -> {
17+
transitionDrawable = this.drawable as TransitionDrawable
18+
lastDrawable = transitionDrawable.findDrawableByLayerId(
19+
transitionDrawable.getId(1)
20+
)
21+
}
22+
23+
is BitmapDrawable -> {
24+
lastDrawable = this.drawable
25+
}
26+
27+
else -> {
28+
lastDrawable = ColorDrawable(Color.TRANSPARENT)
29+
}
30+
}
31+
32+
if (transitionDrawable == null) {
33+
transitionDrawable = TransitionDrawable(
34+
arrayOf(lastDrawable, BitmapDrawable(resources, blurBitmap))
35+
)
36+
transitionDrawable.setId(0, 0)
37+
transitionDrawable.setId(1, 1)
38+
transitionDrawable.isCrossFadeEnabled = true
39+
this.setImageDrawable(transitionDrawable)
40+
} else {
41+
transitionDrawable.setDrawableByLayerId(transitionDrawable.getId(0), lastDrawable)
42+
transitionDrawable.setDrawableByLayerId(
43+
transitionDrawable.getId(1),
44+
BitmapDrawable(resources, blurBitmap)
45+
)
46+
}
47+
transitionDrawable.startTransition(1000)
48+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.pengxh.kt.lite.utils
2+
3+
import android.view.View
4+
import androidx.recyclerview.widget.LinearSnapHelper
5+
import androidx.recyclerview.widget.RecyclerView
6+
import com.pengxh.kt.lite.extensions.dp2px
7+
import kotlin.math.abs
8+
import kotlin.math.max
9+
10+
11+
class GalleryScaleHelper {
12+
13+
private val kTag = "GalleryScaleHelper"
14+
private val snapHelper by lazy { LinearSnapHelper() }
15+
16+
// 卡片的padding, 卡片间的距离等于2倍的pagePadding
17+
private val pagePadding = 15
18+
19+
// 左边卡片显示大小
20+
private val leftCardShowWidth = 15
21+
22+
// 卡片宽度
23+
private var cardWidth = 0
24+
private var currentItemOffset = 0
25+
26+
//当前卡片的index
27+
private var currentItemPos = 0
28+
29+
// 两边视图缩放比例
30+
private val scale = 0.9f
31+
32+
fun attachToRecyclerView(recyclerView: RecyclerView) {
33+
recyclerView.post {
34+
val galleryWidth = recyclerView.width
35+
cardWidth = galleryWidth - 2 * (pagePadding + leftCardShowWidth).toFloat()
36+
.dp2px(recyclerView.context)
37+
}
38+
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
39+
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
40+
super.onScrolled(recyclerView, dx, dy)
41+
// dx>0则表示右滑, dx<0表示左滑, dy<0表示上滑, dy>0表示下滑
42+
if (dx != 0) {
43+
currentItemOffset += dx
44+
45+
currentItemPos = currentItemOffset / cardWidth
46+
47+
val offset = currentItemOffset - currentItemPos * cardWidth
48+
val percent = max(abs(offset) * 1.0 / cardWidth, 0.0001).toFloat()
49+
50+
var leftView: View? = null
51+
var rightView: View? = null
52+
53+
recyclerView.layoutManager?.apply {
54+
if (currentItemPos > 0) {
55+
leftView = findViewByPosition(currentItemPos - 1)
56+
}
57+
val currentView = findViewByPosition(currentItemPos)
58+
recyclerView.adapter?.apply {
59+
if (currentItemPos < itemCount - 1) {
60+
rightView = findViewByPosition(currentItemPos + 1)
61+
}
62+
}
63+
64+
leftView?.apply {
65+
scaleY = (1 - scale) * percent + scale
66+
}
67+
currentView?.apply {
68+
scaleY = (scale - 1) * percent + 1
69+
}
70+
rightView?.apply {
71+
scaleY = (1 - scale) * percent + scale
72+
}
73+
}
74+
}
75+
}
76+
})
77+
snapHelper.attachToRecyclerView(recyclerView)
78+
}
79+
80+
fun getCurrentIndex(): Int {
81+
return currentItemPos
82+
}
83+
}

lite/src/main/java/com/pengxh/kt/lite/widget/audio/AudioPlayerView.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class AudioPlayerView(context: Context, attrs: AttributeSet) : AppCompatTextView
4646

4747
private fun initMediaPlayer() {
4848
mediaPlayer = MediaPlayer()
49+
//此时MediaPlayer进入Idle状态,不能进行任何操作,setDataSource会让播放器从Idle进入Loaded状态
4950
mediaPlayer?.apply {
5051
val inputStream = FileInputStream(audioSource)
5152
setDataSource(inputStream.fd)
@@ -67,10 +68,6 @@ class AudioPlayerView(context: Context, attrs: AttributeSet) : AppCompatTextView
6768
"$m:$s"
6869
}
6970
}
70-
71-
setOnCompletionListener {
72-
stopAnimation()
73-
}
7471
}
7572
}
7673

0 commit comments

Comments
 (0)