我正在使用必须在运行时将“图像颜色”更改为“渐变”的Android应用程序。我已经实现了在运行时更改单个图像上的两种不同颜色的功能,但是我面临的问题是它只更改了固定值(如固定颜色)这样的静态值,我已经添加了
for (x in 0 until width) {
for (y in 0 until height) {
if (bitMatrix.get(x, y)) {
val red = (56 - (56.0 - 14.0) / height * (y + 1)).toInt()
val green = (247 - (247.0 - 145.0) / height * (y + 1)).toInt()
val blue = (195 - (195.0 - 79.0) / height * (y + 1)).toInt()
val colorInt = argb(255, red, green, blue)
// Modify the color of the QR code, you can separately develop the color of the QR code and background
pixels[x * height + y] = if (bitMatrix.get(x, y)) colorInt else 16777215// 0x000000:0xffffff
} else {
pixels[x * height + y] = 0x00ffffff// background color
}
}
我想要的是它通过我添加的搜索栏上的RGB渐变来更改颜色 这就是我更改单个图像的两种颜色的方式:
使用1个函数创建渐变的代码,它是在创建渐变,但是是静态创建的,但是当我尝试在搜索栏上使用渐变功能时,它不起作用,并且只能用单色更改渐变
现在的代码
fun createQRGradientImage(url: String?, width: Int, height: Int): Bitmap? {
try {
if (url == null || "" == url || url.length < 1) {
return null
}
val hints: Hashtable<Any?, Any?> = Hashtable<Any?, Any?>()
hints[EncodeHintType.CHARACTER_SET] = "utf-8"
hints[EncodeHintType.MARGIN] = 0
// Image data conversion, using matrix conversion
val bitMatrix = QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height)
val width = bitMatrix.width
val height = bitMatrix.height
val pixels = IntArray(width * height)
// Gradient color draw from top to bottom
for (y in 0 until height) {
for (x in 0 until width) {
if (bitMatrix[x, y]) { // QR code color
val red = (56 - (56.0 - 14.0) / height * (y + 1)).toInt()
val green = (247 - (247.0 - 145.0) / height * (y + 1)).toInt()
val blue = (195 - (195.0 - 79.0) / height * (y + 1)).toInt()
val color = Color()
val colorInt = Color.argb(255, red, green, blue)
// Modify the color of the QR code, you can separately develop the color of the QR code and background
pixels[y * width + x] =
if (bitMatrix[x, y]) colorInt else 16777215 // 0x000000:0xffffff
} else {
pixels[y * width + x] = 0x00ffffff // background color
}
}
}
// Generate the format of the QR code image, using ARGB_8888
val bitmap = Bitmap.createBitmap(
width, height,
Bitmap.Config.ARGB_8888
)
bitmap.setPixels(pixels, 0, width, 0, 0, width, height)
return bitmap
// img_bitmap1.setImageBitmap(bitmap)
} catch (e: WriterException) {
e.printStackTrace()
}
return null
}
以上是此代码获得的图像
if (bitMatrix[x, y]) { // QR code color
val red = (56 - (56.0 - 14.0) / height * (y + 1)).toInt()
val green = (247 - (247.0 - 145.0) / height * (y + 1)).toInt()
val blue = (195 - (195.0 - 79.0) / height * (y + 1)).toInt()
val color = Color()
val colorInt = Color.argb(255, red, green, blue)
// Modify the color of the QR code, you can separately develop the color of the QR code and background
pixels[y * width + x] =
if (bitMatrix[x, y]) colorInt else 16777215 // 0x000000:0xffffff
} else {
pixels[y * width + x] = 0x00ffffff // background color
}
我想要的是上面的代码应在我创建的搜索栏类颜色上起作用 就像在搜索栏中一样,我有方法
fun pickColor(position: Float, canvasWidth: Int): Int {
val value = (position - paddingStart) / (canvasWidth - (paddingStart + paddingEnd))
when {
value <= 0.0 -> return colorSeeds[0]
value >= 1 -> return colorSeeds[colorSeeds.size - 1]
else -> {
var colorPosition = value * (colorSeeds.size - 1)
val i = colorPosition.toInt()
colorPosition -= i
val c0 = colorSeeds[i]
val c1 = colorSeeds[i + 1]
val red = mix((56 - (56.0 - 14.0) / height * (y + 1)).toInt(),Color.red(c1), colorPosition)
val green = mix((247 - (247.0 - 145.0) / height * (y + 1)).toInt(), Color.green(c1), colorPosition)
val blue = mix((195 - (195.0 - 79.0) / height * (y + 1)).toInt(), Color.blue(c1), colorPosition)
// return Color.rgb( red, green, blue)
return Color.argb(255, red, green, blue)
}
}
}