如何获取 ssekbar.progress 的值并将其用于另一个函数
How to get value of ssekbar.progress and use it on another function
我想从控制 RGB 的三个搜索栏中的 seekbar.progress 中获取值。
我有红色搜索栏、绿色搜索栏和蓝色搜索栏,当我拖动红色搜索栏和绿色和蓝色时,我想要它,
我想为使用 seekbar 值的图像视图设置背景。我想将此值用于设置三种颜色组合的背景。
我正在编写绘制函数以获取我的搜索栏的三个值并为我的图像视图背景设置颜色;
我的 SeekbarManger class 是这样的:
class SeekBarManager() {
}
fun seekBarManage(context: Context, seekBar: SeekBar) {
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) { }
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
MainActivity.getSeekBarProgress(context , seekBar)
}
})
}
fun paint(view: View, red: Int, green:Int, blue:Int){
fun View.getLocationOnScreen(): Point
{
val location = IntArray(2)
this.getLocationOnScreen(location)
return Point(location[0],location[1])
}
val location = view.getLocationOnScreen()
val absX = location.x
val absY = location.y
val bitmap = Bitmap.createBitmap(absX , absY , Bitmap.Config.RGB_565)
val canvas = Canvas(bitmap)
canvas.drawRGB(red , green , blue )
view.background = BitmapDrawable(view.resources , bitmap)
}
我的 MainActivity 是这样的:
open class MainActivity : AppCompatActivity() {
companion object {
fun getSeekBarProgress(context: Context , seekBar: SeekBar){
val result = seekBar.progress
Toast.makeText(context, "Progress is: $result%", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val seekRed = findViewById<SeekBar>(R.id.seekBar_red)
val seekGreen = findViewById<SeekBar>(R.id.seekBar_green)
val seekBlue = findViewById<SeekBar>(R.id.seekBar_blue)
val seekEndRed = seekBarManage(this , seekRed)
val seekEndGreen = seekBarManage(this , seekGreen)
val seekEndBlue = seekBarManage(this , seekBlue)
paint(color_output , seekEndRed , seekEndGreen , seekEndBlue)
}
}
您应该将搜索栏和视图对象传递给 SeekbarManager Class,然后将它们传递给 Companion 对象,如下所示:
open class MainActivity : AppCompatActivity() {
companion object {
fun getSeekBarProgress(
seekBarRed: SeekBar,
seekBarGreen: SeekBar,
seekBarBlue: SeekBar,
view: View
) {
paint(view, seekBarRed.progress, seekBarGreen.progress, seekBarBlue.progress)
}
private fun paint(view: View, red: Int, green: Int, blue: Int) {
fun View.getLocationOnScreen(): Point {
val location = IntArray(2)
this.getLocationOnScreen(location)
return Point(location[0], location[1])
}
val location = view.getLocationOnScreen()
val absX = location.x
val absY = location.y
val bitmap = Bitmap.createBitmap(absX, absY, Bitmap.Config.RGB_565)
val canvas = Canvas(bitmap)
canvas.drawRGB(red, green, blue)
view.background = BitmapDrawable(view.resources, bitmap)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val colorOutput = findViewById<View>(R.id.color_output)
val seekRed = findViewById<SeekBar>(R.id.seekBar_red)
val seekGreen = findViewById<SeekBar>(R.id.seekBar_green)
val seekBlue = findViewById<SeekBar>(R.id.seekBar_blue)
seekBarManage(seekRed, seekGreen, seekBlue, colorOutput)
}
}
和
class SeekBarManager() {
}
fun seekBarManage(
seekBarRed: SeekBar,
seekBarGreen: SeekBar,
seekBarBlue: SeekBar,
view: View
) {
seekBarRed.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
MainActivity.getSeekBarProgress(seekBarRed, seekBarGreen, seekBarBlue, view)
}
})
seekBarGreen.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
MainActivity.getSeekBarProgress(seekBarRed, seekBarGreen, seekBarBlue, view)
}
})
seekBarBlue.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
MainActivity.getSeekBarProgress(seekBarRed, seekBarGreen, seekBarBlue, view)
}
})
}
更好的方法是在 MainActivity 中完成所有这些操作,不需要 SeekBarManager Class,如下所示:
open class MainActivity : AppCompatActivity() {
lateinit var colorOutput: View
lateinit var seekRed: SeekBar
lateinit var seekGreen: SeekBar
lateinit var seekBlue: SeekBar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
colorOutput = findViewById<View>(R.id.color_output)
seekRed = findViewById<SeekBar>(R.id.seekBar_red)
seekGreen = findViewById<SeekBar>(R.id.seekBar_green)
seekBlue = findViewById<SeekBar>(R.id.seekBar_blue)
seekRed.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
paint()
}
})
seekGreen.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
paint()
}
})
seekBlue.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
paint()
}
})
}
private fun paint() {
val view = colorOutput
val red = seekRed.progress
val green = seekGreen.progress
val blue = seekBlue.progress
fun View.getLocationOnScreen(): Point {
val location = IntArray(2)
this.getLocationOnScreen(location)
return Point(location[0], location[1])
}
val location = view.getLocationOnScreen()
val absX = location.x
val absY = location.y
val bitmap = Bitmap.createBitmap(absX, absY, Bitmap.Config.RGB_565)
val canvas = Canvas(bitmap)
canvas.drawRGB(red, green, blue)
view.background = BitmapDrawable(view.resources, bitmap)
}
}
我想从控制 RGB 的三个搜索栏中的 seekbar.progress 中获取值。
我有红色搜索栏、绿色搜索栏和蓝色搜索栏,当我拖动红色搜索栏和绿色和蓝色时,我想要它,
我想为使用 seekbar 值的图像视图设置背景。我想将此值用于设置三种颜色组合的背景。
我正在编写绘制函数以获取我的搜索栏的三个值并为我的图像视图背景设置颜色;
我的 SeekbarManger class 是这样的:
class SeekBarManager() {
}
fun seekBarManage(context: Context, seekBar: SeekBar) {
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) { }
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
MainActivity.getSeekBarProgress(context , seekBar)
}
})
}
fun paint(view: View, red: Int, green:Int, blue:Int){
fun View.getLocationOnScreen(): Point
{
val location = IntArray(2)
this.getLocationOnScreen(location)
return Point(location[0],location[1])
}
val location = view.getLocationOnScreen()
val absX = location.x
val absY = location.y
val bitmap = Bitmap.createBitmap(absX , absY , Bitmap.Config.RGB_565)
val canvas = Canvas(bitmap)
canvas.drawRGB(red , green , blue )
view.background = BitmapDrawable(view.resources , bitmap)
}
我的 MainActivity 是这样的:
open class MainActivity : AppCompatActivity() {
companion object {
fun getSeekBarProgress(context: Context , seekBar: SeekBar){
val result = seekBar.progress
Toast.makeText(context, "Progress is: $result%", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val seekRed = findViewById<SeekBar>(R.id.seekBar_red)
val seekGreen = findViewById<SeekBar>(R.id.seekBar_green)
val seekBlue = findViewById<SeekBar>(R.id.seekBar_blue)
val seekEndRed = seekBarManage(this , seekRed)
val seekEndGreen = seekBarManage(this , seekGreen)
val seekEndBlue = seekBarManage(this , seekBlue)
paint(color_output , seekEndRed , seekEndGreen , seekEndBlue)
}
}
您应该将搜索栏和视图对象传递给 SeekbarManager Class,然后将它们传递给 Companion 对象,如下所示:
open class MainActivity : AppCompatActivity() {
companion object {
fun getSeekBarProgress(
seekBarRed: SeekBar,
seekBarGreen: SeekBar,
seekBarBlue: SeekBar,
view: View
) {
paint(view, seekBarRed.progress, seekBarGreen.progress, seekBarBlue.progress)
}
private fun paint(view: View, red: Int, green: Int, blue: Int) {
fun View.getLocationOnScreen(): Point {
val location = IntArray(2)
this.getLocationOnScreen(location)
return Point(location[0], location[1])
}
val location = view.getLocationOnScreen()
val absX = location.x
val absY = location.y
val bitmap = Bitmap.createBitmap(absX, absY, Bitmap.Config.RGB_565)
val canvas = Canvas(bitmap)
canvas.drawRGB(red, green, blue)
view.background = BitmapDrawable(view.resources, bitmap)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val colorOutput = findViewById<View>(R.id.color_output)
val seekRed = findViewById<SeekBar>(R.id.seekBar_red)
val seekGreen = findViewById<SeekBar>(R.id.seekBar_green)
val seekBlue = findViewById<SeekBar>(R.id.seekBar_blue)
seekBarManage(seekRed, seekGreen, seekBlue, colorOutput)
}
}
和
class SeekBarManager() {
}
fun seekBarManage(
seekBarRed: SeekBar,
seekBarGreen: SeekBar,
seekBarBlue: SeekBar,
view: View
) {
seekBarRed.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
MainActivity.getSeekBarProgress(seekBarRed, seekBarGreen, seekBarBlue, view)
}
})
seekBarGreen.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
MainActivity.getSeekBarProgress(seekBarRed, seekBarGreen, seekBarBlue, view)
}
})
seekBarBlue.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
MainActivity.getSeekBarProgress(seekBarRed, seekBarGreen, seekBarBlue, view)
}
})
}
更好的方法是在 MainActivity 中完成所有这些操作,不需要 SeekBarManager Class,如下所示:
open class MainActivity : AppCompatActivity() {
lateinit var colorOutput: View
lateinit var seekRed: SeekBar
lateinit var seekGreen: SeekBar
lateinit var seekBlue: SeekBar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
colorOutput = findViewById<View>(R.id.color_output)
seekRed = findViewById<SeekBar>(R.id.seekBar_red)
seekGreen = findViewById<SeekBar>(R.id.seekBar_green)
seekBlue = findViewById<SeekBar>(R.id.seekBar_blue)
seekRed.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
paint()
}
})
seekGreen.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
paint()
}
})
seekBlue.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
paint()
}
})
}
private fun paint() {
val view = colorOutput
val red = seekRed.progress
val green = seekGreen.progress
val blue = seekBlue.progress
fun View.getLocationOnScreen(): Point {
val location = IntArray(2)
this.getLocationOnScreen(location)
return Point(location[0], location[1])
}
val location = view.getLocationOnScreen()
val absX = location.x
val absY = location.y
val bitmap = Bitmap.createBitmap(absX, absY, Bitmap.Config.RGB_565)
val canvas = Canvas(bitmap)
canvas.drawRGB(red, green, blue)
view.background = BitmapDrawable(view.resources, bitmap)
}
}