Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementation of BitmapFactory.Options for Android
    primarykey
    data
    text
    <p>I have 60 images that I use for my app. Each image is only (approx) 25KB in size. I use single letter naming convention for upper case, and double letters for lower case.</p> <blockquote> <p>a.png</p> <p>aa.png</p> </blockquote> <p>In my layout I have 10 ImageViews, and I am programatically setting the image bitmap according to a word which I pull from a database.</p> <p>My problem is, I may not have implemented the down sampling correctly. I add all 10 ImageViews to an ImageView array, and then find them to set their bitmap value based on the character array of the word from the database. Here is my method to set the Image View:</p> <pre><code>for(int j = 0; j &lt; myWord.length(); j++){ char[] chars = myWord.toCharArray(); if(Character.isUpperCase(chars[j])){ int imgID = getResources().getIdentifier(String.valueOf(chars[j]).toLowerCase(), "drawable", getPackageName()); letters[j].setTag(String.valueOf(chars[j]).toUpperCase()); letters[j].setImageBitmap(CalculateSize.decodeSampledBitmapFromResource(getResources(), imgID, 75, 75)); }else if(Character.isLowerCase(chars[j])){ int imgID = getResources().getIdentifier(String.valueOf(chars[j]) + String.valueOf(chars[j]), "drawable", getPackageName()); letters[j].setTag(chars[j] + chars[j]); letters[j].setImageBitmap(CalculateSize.decodeSampledBitmapFromResource(getResources(),imgID, 75, 75)); } </code></pre> <p>Here are my methods to resize. I got it working for a 3 letter word with an <code>inSampleSize = 4</code> setting. However, I have been unable to get it working again no matter what I set it to:</p> <pre><code> public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){ // Height and Width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 12; if (height &gt; reqHeight || width &gt; reqWidth) { // Calculate ratios of height and width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose smallest ratio as inSampleSize value. inSampleSize = heightRatio &lt; widthRatio ? heightRatio : widthRatio; } return inSampleSize; } public static Bitmap decodeSampledBitmapFromResource(Resources res, int resID, int reqWidth, int reqHeight){ final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resID, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resID, options); } } </code></pre>
    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