I want to get value from seekbar.progress of three seekbar for control RGB.
I have Red seekbar , Green seekBar and Blue seekbar and I want that when I drag red seekbar and green and blue ,
I want to set Background for an image view that use value of seekbar . I want use this values for set background that Combination of three color .
I'm write the paint function for get three values of my seekbar and set a color for my image view background;
My SeekbarManger class is this :
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)
}
and my MainActivity is This:
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)
}
}
You should pass seekbars and view objects to SeekbarManager Class and then pass them to Companion object like below :
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)
}
}
and
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)
}
})
}
and better way is to just do all these in MainActivity and there is no need for SeekBarManager Class, like below:
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)
}
}
I have this piece of code, this is my first use of Glide, I still do not understand how to do it differently
try {
Glide.with(this#TestBitmapActivity)
.asBitmap()
.load("https://firebasestorage.googleapis.com/v0/b/mymodeapp-3f9a7.appspot.com/o/heroes%2Fman%2Fbg_hero.png?alt=media&token=2182a224-bb65-4f06-90d9-8e5c5d9b07d9")
.into(object : CustomTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
bg_imageview_test.setImageBitmap(resource)
Glide.with(this#TestBitmapActivity)
.asBitmap()
.load("https://firebasestorage.googleapis.com/v0/b/mymodeapp-3f9a7.appspot.com/o/heroes%2Fman%2Fhero_man.png?alt=media&token=6358e564-1fe0-41ff-bc87-0e28d3fe1c1e")
.into(object : CustomTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
val bitmap1:Bitmap = resource
Glide.with(this#TestBitmapActivity)
.asBitmap()
.load("https://firebasestorage.googleapis.com/v0/b/mymodeapp-3f9a7.appspot.com/o/heroes%2Fman%2Fhero_sword.png?alt=media&token=f3d003f8-b65f-4e69-afe3-ea9653c3a925")
.into(object : CustomTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
val bitmap2:Bitmap = resource
Glide.with(this#TestBitmapActivity)
.asBitmap()
.load("https://firebasestorage.googleapis.com/v0/b/mymodeapp-3f9a7.appspot.com/o/heroes%2Fman%2Fhero_wear.png?alt=media&token=4f88b869-347e-4bf8-842e-4a728b8d743f")
.into(object : CustomTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
val bitmap3:Bitmap = resource
val bitmapa = overlay(bitmap1,bitmap2,bitmap3)
setAva(bitmapa!!)
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
}
catch (e: Exception) {
Log.d("Test","Erorr?")
}
and the whole problem is that these threads are nested in each other and it would be better if I used For, how to do it in Kotlin?
I suggest that you first write the code that downloads one bitmap. Then you can copy/paste the exact same code to download a different bitmap instead. Assuming that the result of downloading each bitmap is independent, you don't need to nest anything. Also, the changes you make after copy/pasting the first bitmap should give you an idea of how to turn this into a function that takes a few parameters to specify which bitmap to download.
I wanted to load image from url into bottomnavigationview's menu item icon. same like instagram we wanted to show profile image in one of our menu which might change while switching from different available profiles.
Currently, I am trying below code but it does not load image every time.
val menu = mViewDataBinding?.bnvNavigation?.menu
val menuItem = menu?.findItem(R.id.menu_profile)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
menuItem?.setIconTintList(null)
menuItem?.setIconTintMode(null)
}
Glide.with(this)
.asBitmap()
.load(profilePic)
.apply(
RequestOptions.circleCropTransform()
.placeholder(R.drawable.user_profile_icon_updated)
)
.into(object :
CustomTarget<Bitmap>(UIHelper.dpToPx(this, 24), UIHelper.dpToPx(this, 24)) {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
menuItem?.icon = BitmapDrawable(resources, resource)
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
Above code is working as expected, it just a matter of passing application context to Glide, Glide.with(App.instance)
App is my application class
val menu = mViewDataBinding?.bnvNavigation?.menu
val menuItem = menu?.findItem(R.id.menu_profile)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
menuItem?.setIconTintList(null)
menuItem?.setIconTintMode(null)
}
Glide.with(App.instance)
.asBitmap()
.load(profilePic)
.placeholder(getProfilePlaceholderFilled())
.error(getProfilePlaceholderFilled())
.apply(
RequestOptions.circleCropTransform().placeholder(getProfilePlaceholderFilled())
)
.into(object :
CustomTarget<Bitmap>(UIHelper.dpToPx(this, 80), UIHelper.dpToPx(this, 80)) {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?
) {
menuItem?.icon = BitmapDrawable(
resources, resource
)
}
override fun onLoadFailed(errorDrawable: Drawable?) {
menuItem?.icon = errorDrawable
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
i recommended you to use chip navigation and create custom bottom navigation its very better than default android bottom navigation and more easy...
if you interest to learn you can use this tutorial https://www.youtube.com/watch?v=DQtdOSN21lQ&feature=emb_logo and the library https://github.com/ismaeldivita/chip-navigation-bar
if you have any question tag me to answer you
On the left is the list displayed in API level 19 and on the right is API 28. For API 19 I am unable to load flag Images using Picasso. https://i.stack.imgur.com/PU7QJ.png
This is how I am doing it right now.
interface ImageFetcher {
fun loadImage(url: String, callback: (bitmap: Bitmap) -> Unit)
}
class RemoteImageFetcher(val context: Context) : ImageFetcher {
override fun loadImage(url: String, callback: (bitmap: Bitmap) -> Unit) {
val target = object : com.squareup.picasso.Target {
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom?) {
callback.invoke(bitmap)
}
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {}
}
Picasso.get().load(url).into(target)
}
I am working on a side project and in notification i wanted to show some default place Holder(drawable) in ImageView.
So i tried this:
Glide.with(appContext)
.load(iconDrawable)
.into(object : CustomTarget<Drawable>() {
override fun onLoadCleared(placeholder: Drawable?) {
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
val resBitmap : Bitmap = Utils.drawableToBitmap(resource)
remoteViews.setImageViewBitmap(R.id.primaryIcon, resBitmap))
}
})
Above code is working perfectly but as you can notice i don't want to handle creation and conversion of Drawable to Bitmap .So i tried to use asBitmap().
Glide.with(appContext)
.asBitmap()
.load(iconDrawable)
.into(object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) {
}
override fun onResourceReady(resource: Bitmap, transition: Transition<in Drawable>?) {
remoteViews.setImageViewBitmap(R.id.primaryIcon, resource))
}
})
I expected to work but bitmap it empty and drawable is not loading to the view.
even i tried using placeholder(),error() nothing worked
Glide.with(appContext)
.asBitmap()
.load(iconDrawable)
.placeholder(iconDrawable)
.error(iconDrawable)
.into(object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) {
}
override fun onResourceReady(resource: Bitmap, transition: Transition<in Drawable>?) {
remoteViews.setImageViewBitmap(R.id.primaryIcon, resource))
}
})
NOTE:Above case arises even if i use NotificationTarget
// try this, ...
remoteView.setImageBitmap(R.id.yourImageView, bitmap);