Search code examples
androidandroid-layoutimage-processingpixel

Android using images created during runtime as backgrounds


so I have a regular drawable that I want to use with many variations. I plan to replace certain pixel colors with specific variations. To reduce the amount of image files required for the app, I plan to do such pixel-manipulation during runtime. Can I create a drawable/background resource to be used, during runtime? Or is that impossible on Android? Like I start with a .png file, and during runtime I modd it into a similar .png file. Can I use that new .png file as a background for a layout, while the program is still going?

Thanks


Solution

  • I don't think you need to create a new .png file everytime. Please have look at this question. The first answer provides a way to manipulate a bitmap object pixelwise during runtime. So you start with a .png file which is provided by your app and get a bitmap out of it by doing this:

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bitmap_resource);
    

    Then you can manipulate the bitmap and change the background again. If you want you can save it as a .png too by using:

    bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);
    

    Have a look at the documentation for Bitmap here.