Files
yusheng-android/BaseModule/src/main/java/com/xscm/moduleutil/widget/FakeNinePatchDrawable.kt
2025-11-07 09:22:39 +08:00

96 lines
3.6 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xscm.modulemain
import android.graphics.*
import android.graphics.drawable.Drawable
/**
* 这是设置图片设置拉伸区域
*/
class FakeNinePatchDrawable(
private val bitmap: Bitmap,
// 定义拉伸区域的坐标(相对图片的百分比,范围 0~1
private val left: Float, // 左边界(左侧不拉伸区域的宽度比例)
private val top: Float, // 上边界(上侧不拉伸区域的高度比例)
private val right: Float, // 右边界(右侧不拉伸区域的宽度比例)
private val bottom: Float // 下边界(下侧不拉伸区域的高度比例)
) : Drawable() {
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val rect = Rect() // 绘制区域
override fun draw(canvas: Canvas) {
val bounds = bounds // 视图的实际尺寸(需要填充的区域)
val w = bitmap.width
val h = bitmap.height
// 计算分割点(像素值)
val splitLeft = (w * left).toInt()
val splitTop = (h * top).toInt()
val splitRight = (w * right).toInt()
val splitBottom = (h * bottom).toInt()
// 1. 绘制四个角(不拉伸)
// 左上
rect.set(0, 0, splitLeft, splitTop)
canvas.drawBitmap(bitmap, Rect(0, 0, splitLeft, splitTop), rect, paint)
// 右上
rect.set(bounds.width() - (w - splitRight), 0, bounds.width(), splitTop)
canvas.drawBitmap(bitmap, Rect(splitRight, 0, w, splitTop), rect, paint)
// 左下
rect.set(0, bounds.height() - (h - splitBottom), splitLeft, bounds.height())
canvas.drawBitmap(bitmap, Rect(0, splitBottom, splitLeft, h), rect, paint)
// 右下
rect.set(
bounds.width() - (w - splitRight),
bounds.height() - (h - splitBottom),
bounds.width(),
bounds.height()
)
canvas.drawBitmap(bitmap, Rect(splitRight, splitBottom, w, h), rect, paint)
// 2. 绘制四条边(单向拉伸)
// 上边(水平拉伸)
rect.set(splitLeft, 0, bounds.width() - (w - splitRight), splitTop)
canvas.drawBitmap(bitmap, Rect(splitLeft, 0, splitRight, splitTop), rect, paint)
// 下边(水平拉伸)
rect.set(
splitLeft,
bounds.height() - (h - splitBottom),
bounds.width() - (w - splitRight),
bounds.height()
)
canvas.drawBitmap(bitmap, Rect(splitLeft, splitBottom, splitRight, h), rect, paint)
// 左边(垂直拉伸)
rect.set(0, splitTop, splitLeft, bounds.height() - (h - splitBottom))
canvas.drawBitmap(bitmap, Rect(0, splitTop, splitLeft, splitBottom), rect, paint)
// 右边(垂直拉伸)
rect.set(
bounds.width() - (w - splitRight),
splitTop,
bounds.width(),
bounds.height() - (h - splitBottom)
)
canvas.drawBitmap(bitmap, Rect(splitRight, splitTop, w, splitBottom), rect, paint)
// 3. 绘制中间区域(双向拉伸)
rect.set(
splitLeft,
splitTop,
bounds.width() - (w - splitRight),
bounds.height() - (h - splitBottom)
)
canvas.drawBitmap(bitmap, Rect(splitLeft, splitTop, splitRight, splitBottom), rect, paint)
}
override fun setAlpha(alpha: Int) {
paint.alpha = alpha
invalidateSelf()
}
override fun getOpacity(): Int = PixelFormat.TRANSLUCENT
override fun getAlpha(): Int = paint.alpha
override fun setColorFilter(colorFilter: ColorFilter?) {
paint.colorFilter = colorFilter
invalidateSelf()
}
}