简体   繁体   中英

Can we use and draw view inside custom view using canvas on Android?

I would like to know if it possible to use and draw a view such as a TextView or Chronometer inside my custom View which uses the canvas to perform drawing.

My goal is to reuse the Chonometer view from Android inside my custom view and drawing it above all my canvas layers.

I searched like for the whole day, android sources, 3rd party libs but can't figure out.

Also I noticed that on View class there is no way to set bounds or positionning or something so I might not be able to do what I want.

Any help would be greatly appreciated: :)

Thanks.

Something like:

class CustomView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    private val chronometer: Chronometer = Chronometer(context)


    override fun onDraw(canvas: Canvas) {
        //Here I perform all my others drawing 


        //Does not work
        chronometer.draw(canvas)
    }


}

You can put a view inside a custom view, but you don't do it like that. Instead you inherit from ViewGroup instead of View. Then you implement onMeasure and onLayout to layout and measure yourself and your child view. Then you don't need to do anything to draw it- the child view will draw itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM