Java 在颜色搜索栏上创建渐变
我正在开发Android应用程序,在运行时必须将图像颜色更改为渐变色。我已经实现了在运行时更改单个图像上的两种不同颜色,但我面临的问题是,它只更改静态值,如我添加的固定颜色Java 在颜色搜索栏上创建渐变,java,xml,android-layout,kotlin,qr-code,Java,Xml,Android Layout,Kotlin,Qr Code,我正在开发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) /
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梯度超过Seekbar我已经添加
这就是我如何更改单个图像的两种图像颜色:
使用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
}
我想要的是上面的代码应该对我创建的Seek bar类颜色起作用
就像在Seekbar我有办法
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)
}
}
}
fun pickColor(位置:Float,画布宽度:Int):Int{
val值=(位置-paddingStart)/(画布宽度-(paddingStart+paddingEnd))
什么时候{
返回值colorSeeds[0]
值>=1->返回colorSeeds[colorSeeds.size-1]
其他->{
var colorPosition=value*(colorSeeds.size-1)
val i=colorPosition.toInt()
色位-=i
val c0=彩色种子[i]
val c1=彩色种子[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)
//返回颜色.rgb(红色、绿色、蓝色)
返回颜色.argb(255,红色,绿色,蓝色)
}
}
}
我不明白为什么你有两个函数而不是一个(?)。在任何情况下,它们似乎都不会在各自的循环中改变它们绘制的颜色。你为什么不编写一个函数来生成整个图像,并在第一种颜色和第二种颜色之间进行线性插值呢?@Michael我更新了这个问题,我不明白为什么你有两个函数而不是一个(?)。在任何情况下,它们似乎都不会在各自的循环中改变它们绘制的颜色。为什么不编写一个函数来生成整个图像,并在第一种和第二种颜色之间进行线性插值呢?@Michael我更新了这个问题
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)
}
}
}