Note that there are some explanatory texts on larger screens.

plurals
  1. POUpscaling big images
    primarykey
    data
    text
    <p>I have created live wallpaper recently. It does displays two images next to each other. Each image does take half of width of screen. When moving the screens (moving to sides on home screen) images does move too.</p> <p>I wanted it to look as good as possible, so <strong>all my background images does have resolution 1320x958</strong>. I know the height isn't right ideal, but that was the best I was able to get from most of the images I used.</p> <p>On wallpaper start, images are scaled to screen height and appropriate width is calculated. It does run good on devices with smaller displays (ldpi, mdpi).<br> The problem are devices with big screens, where there is a need to upscale (not downscale) my images, suddenly I get <code>java.lang.OutOfMemoryError</code>. I got crash report from Samsung S3, I dont understand how could this happen on devices with big screens (which have more memory available) when older devices with smaller screens (and less memory) doesn't have any problem.</p> <p>Here is my code:</p> <pre><code>private void InitializeBackgrounds() { Resources res = getApplicationContext().getResources(); // Load and scale backgrounds int bg_height = height; int bg_width = (int)((float)bg_height/958.0f*1320.0f); try { // Load random image from array of images Bitmap original = BitmapFactory.decodeResource(res, left_files[r.nextInt(left_files.length)]); // Scale image to new size spurs = Bitmap.createScaledBitmap(original, bg_width, bg_height, true); // Free up memory immediately original.recycle(); // Do the same for right image original = BitmapFactory.decodeResource(res, right_files[r.nextInt(right_files.length)]); heat = Bitmap.createScaledBitmap(original, bg_width, bg_height, true); original.recycle(); } catch (OutOfMemoryError e){ throw new OutOfMemoryError("OutOfMemoryException;InitializeBackgrounds(); model: " + Build.MODEL + "; LocalizedMessage: "+e.getLocalizedMessage()+"; Message: "+e.getMessage()); } } </code></pre> <p>Later, when drawing, loaded images are cropped to fit the width of screen. There is created effect of moving images when moving screens. That is why I have loaded image that is bigger than screen in memory.</p> <pre><code>private void drawWallpaper(Canvas c) { // ... try { bg_left = Bitmap.createBitmap(spurs, x_shift, 0, width/2, height); bg_right = Bitmap.createBitmap(heat, x_shift, 0, width/2, height); } catch (OutOfMemoryError e){ throw new OutOfMemoryError("OutOfMemoryException;DrawWallpaper(); model: " + Build.MODEL + "; LocalizedMessage: "+e.getLocalizedMessage()+"; Message: "+e.getMessage()); } // Draw backgrounds c.drawBitmap(bg_left, 0, 0, null); c.drawBitmap(bg_right, width/2, 0, null); //... } </code></pre> <p>Could you help me optimize my code?</p> <hr> <p><strong>UPDATE</strong></p> <p>I tried to draw bitmap using transformation matrix, but its slow. Here is how I tried it:</p> <pre><code>Matrix m = new Matrix(); m.reset(); RectF rect_source = new RectF(x_shift, 0, source_width, original_height); RectF rect_destination = new RectF(0, 0, width/2, height); m.setRectToRect(rect_source, rect_destination, Matrix.ScaleToFit.CENTER); c.drawBitmap(BitmapFactory.decodeResource(res, resource_left), m, null); </code></pre> <p>It doesn't fit my screen as it should (I must made error in computing sizes). Is it right way? May it be quicker when fix computations (will crop lower area)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload