1

I am trying to centre a bitmap on the android lockscreen, but it would appear that part of the screen is not visible, but I can't find any details of margins for the lockscreen.

I changed my routine to draw a centred small 10x10 red block and also a ruler with ticks which appears to show that I can not see below 30 or above 620, so my calculation to centre my bitmap does not work. This is the result of my test code.

ScreenShot

The lock icon is drawn by android and is centred correctly, but my red block is not correct.

I am a beginner as far as android is concerned, so I have included the test routine below.

 fun DrawRuler(context: Context)
 {
     val paint = Paint(Paint.ANTI_ALIAS_FLAG)
     paint.color = Color.WHITE

     var densityDpi = Resources.getSystem().displayMetrics.densityDpi
     var density = Resources.getSystem().displayMetrics.density
     var xdpi = Resources.getSystem().displayMetrics.xdpi
     var ydpi = Resources.getSystem().displayMetrics.ydpi

     var screenWidth_int = Resources.getSystem().displayMetrics.widthPixels
     var screenHeight_int = Resources.getSystem().displayMetrics.heightPixels
     var screenWidth = screenWidth_int.toFloat()

     var wallpaper = Bitmap.createBitmap(screenWidth_int   , screenHeight_int, Bitmap.Config.ARGB_8888)
     val canvas = Canvas(wallpaper)

     val textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
     textPaint.color = Color.WHITE
     textPaint.setTextSize(40f);

     canvas.drawText("densityDpi       = $densityDpi"       , 40f,   850f, textPaint)
     canvas.drawText("density          = $density"          , 40f,   900f, textPaint)
     canvas.drawText("xdpi             = $xdpi"             , 40f,   950f, textPaint)
     canvas.drawText("ydpi             = $ydpi"             , 40f,  1000f, textPaint)
     canvas.drawText("screenWidth_int  = $screenWidth_int"  , 40f,  1050f, textPaint)
     canvas.drawText("screenHeight_int = $screenHeight_int" , 40f,  1100f, textPaint)

     // Draw a red 10x10 block and attempt to centre it.
     val bitmap1 = Bitmap.createBitmap(10,10, Bitmap.Config.ARGB_8888)
     val image1 = Canvas(bitmap1)
     image1.drawColor(Color.RED);
     var redLeft = ((screenWidth-10F)/2F)
     canvas.drawBitmap(bitmap1 , redLeft, 1200f, Paint(Paint.ANTI_ALIAS_FLAG))
     canvas.drawText("left = $redLeft" , redLeft,  1150f, textPaint)

     // Draw ruler
     var rulerPos = 1158f;

     canvas.drawLine(0f, rulerPos, screenWidth, rulerPos, paint)

     var x = 0f;
     while (x <= screenWidth )
     {
         var thisDepth = 5f;
         if (x % 10 == 0F) thisDepth = 10f
         if (x % 50 == 0F) {
             thisDepth = 20f
             canvas.drawText(x.toString(), x, rulerPos + 30F, paint)
         }

         canvas.drawLine(x, rulerPos, x, rulerPos + thisDepth, paint)
         x = x + 5f
     }

     val wallpaperManager = WallpaperManager.getInstance(context)
     wallpaperManager.setBitmap(wallpaper,  null, false, WallpaperManager.FLAG_LOCK)
     wallpaper.recycle()

 }

Any ideas what I am doing wrong?

1 Answer 1

0

did some doc investingation, worth trying...

you are using THIS createBitmap(..) method, which does not care about DisplayMetrics and sets own density to DENSITY_DEFAULT. maybe worth playing this param manually or just using similar static method with DisplayMetrics at first

val bitmap1 = 
    Bitmap.createBitmap(Resources.getSystem().displayMetrics, 10, 10, Bitmap.Config.ARGB_8888)
Sign up to request clarification or add additional context in comments.

1 Comment

Tried as you suggest, but it did not seem to make any difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.