fun Bitmap.drawBg4BitmapGradient(colorStart:Int, colorEnd:Int): Bitmap {
val paint = Paint()
//设置线性渐变,点(x0,y0)向(x1,y1)渐变
var linearGradient = LinearGradient(0f,0f,0f, this.height.toFloat()
, colorStart, colorEnd, Shader.TileMode.CLAMP)
//设置着色器
paint.shader = linearGradient
paint.strokeWidth = this.width.toFloat()
val resultBitmap = Bitmap.createBitmap(this.width, this.height, this.config)
//设置画布
val canvas = Canvas(resultBitmap)
//绘制渐变矩形当背景
canvas.drawRect(0f, 0f, this.width.toFloat(), this.height.toFloat(), paint)
//绘制图像
canvas.drawBitmap(this, 0f, 0f, paint)
return resultBitmap
}
android bitmap更改渐变背景
最新推荐文章于 2024-05-08 20:30:19 发布