-1

I have defined the custom drawable below:

class HexDrawable constructor(val text: String) : Drawable() {

    private val paint = Paint()

    init {
        paint.color = Color.WHITE
        paint.textSize = 22f
        paint.isAntiAlias = true
        paint.isFakeBoldText = true
        paint.setShadowLayer(3f, 0f, 0f, Color.BLACK)
        paint.style = Paint.Style.FILL

    }

    override fun draw(canvas: Canvas) {
        val width: Int = 120
        val height: Int = 120
        val radius: Float = Math.min(width, height).toFloat() / 2f

        // Draw a green circle in the center
        canvas.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), radius, paint)
    }

    override fun setAlpha(alpha: Int) {
        paint.alpha = alpha
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        paint.colorFilter = colorFilter
    }

    override fun getOpacity(): Int {
        return PixelFormat.OPAQUE
    }
}

I am not sure what units the drawable is using. I want to use eg 40dp for the width but the 120 that is specified in the code above turns out very small when it is drawn on the screen so I donlt think it is dp, maybe it is mm?

can someone please clarify what unit it is using by default? and how to use dp instead?

afgfdg
  • 21
  • 3
  • "can someone please clarify what unit it is using by default?" -- I believe that those are in pixels. "how to use dp instead?" -- ideally, whatever code is creating this `HexDrawable` would pass in the size to use in pixels. It, in turn could get that size from a dimension resource, which would handle all of the conversion from other units (e.g., `dp`) to pixels for you. – CommonsWare Apr 14 '21 at 23:15
  • this drawable is being used inside an imageview which has "Wrap_Content" for both the width and height, i'd like this custom drawable to be able to mandate the dp width and height.. is there a way to do that? – afgfdg Apr 14 '21 at 23:21

1 Answers1

1

By default Android uses "px" for view size.

Here is the converter function.

import android.content.Context
import android.content.res.Resources
import android.util.DisplayMetrics

/**
* Provides utilities for metrics.
*
* Original at:
* @see <a href="https://stackoverflow.com/a/9563438/8877070">stack overflow answer</a>
*
* @author kevin
*/
object MetricsUtil {

    /**
    * This method converts dp unit to equivalent pixels, depending on device density.
    * @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
    * @param context Context to get resources and device specific display metrics. If you don't have
    * access to Context, just pass null.
    * @return A float value to represent px equivalent to dp depending on device density
    */
    fun convertDpToPixel(dp: Float, context: Context?): Float {
        return if (context != null) {
            val resources = context.resources
            val metrics = resources.displayMetrics
            dp * (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
        } else {
            val metrics = Resources.getSystem().displayMetrics
            dp * (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
        }
    }

    /**
    * This method converts device specific pixels to density independent pixels.
    * @param px A value in px (pixels) unit. Which we need to convert into db
    * @param context Context to get resources and device specific display metrics. If you don't have
    * access to Context, just pass null.
    * @return A float value to represent dp equivalent to px value
    */
    fun convertPixelsToDp(px: Float, context: Context?): Float {
        return if (context != null) {
            val resources = context.resources
            val metrics = resources.displayMetrics
            px / (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
        } else {
            val metrics = Resources.getSystem().displayMetrics
            px / (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
        }
    }
}

So you can convert 120 to "dp" value by using above code. Here is other function for converting.

fun Int.toDp(): Int = (this / Resources.getSystem().displayMetrics.density).toInt()
fun Int.toPx(): Int = (this * Resources.getSystem().displayMetrics.density).toInt()
Wizard
  • 51
  • 6
  • Here is other function for converting. fun Int.toDp(): Int = (this / Resources.getSystem().displayMetrics.density).toInt() fun Int.toPx(): Int = (this * Resources.getSystem().displayMetrics.density).toInt() – Wizard Apr 14 '21 at 23:37