Scale & rotate Bitmap using Matrix in Android

前端 未结 8 1744
名媛妹妹
名媛妹妹 2020-12-12 19:26

I\'m trying to scale and rotate in single operation before creting the final bitmap but the preRotate, postConcat doesn\'t seem to work.

Bitmap bmp = ... ori         


        
8条回答
  •  臣服心动
    2020-12-12 20:03

    All of the previous answer assume that this change to the bitmap is being made in a view. However in my case I was making the change to be saved out. Figured I would answer it for those in a similar boat.

    There are two ways to do translation. Below dx is the translation in the X axis, and dy is the translation in the Y axis. The other variables should be self explanatory.

    1 - Translation within the image (without rotation)

    val newBitmap = Bitmap.createBitmap(originalBitmap, dx, dy, newWidth, newHeight, matrix, false)
    

    2 - Complex matrix

     matrix.postTranslate(dx, dy)
    
     val newBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888)
    
     val canvas = Canvas(newBitmap)
     canvas.drawBitmap(originalBitmap, matrix, null)
    

提交回复
热议问题