2

I'm working on a way to hold the same aspect of my images (icons and images) in my "universal" app.

The problem with the resolutions/screens densities is causing me a lot of headaches.

Is there any way to work with this problem:?

Device: Tablet 10" / 1280x752 / Screen density MDPI

If I create one image for each density, in devices with large resolution and low screen density the images show small.

My solution is working with large images and scale down it to the images not show pixelized.

I'm working with official solution Android

This is a part of my activity

    @Override   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        widthScreen = size.x;
        heightScreen = size.y;      
        imgTest = (ImageView)findViewById(R.id.image);

        imgTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

           final Bitmap img = decodeSampledBitmapFromDrawable(Test.this,tipoprueba, widthScreen, heightScreen);
        imgTest.setImageBitmap(img);

                    }

        });

    }


public static Bitmap decodeSampledBitmapFromDrawable(Context activity,String res, int reqWidth, int reqHeight) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(activity.getResources(), activity.getResources().getIdentifier(res, "drawable", "com.example.docaper"), options);
        options.inSampleSize = Utils.calculateInSampleSizeInside(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inScaled = false;
        return BitmapFactory.decodeResource(activity.getResources(), activity.getResources().getIdentifier(res, "drawable", "com.example.docaper"), options);
    }



    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

        final int height = options.outHeight;
        final int width = options.outWidth;

        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
        }

When click in the image, I change the image.When I change several images a OutOfMemory can't allocate ... exception is showed and closes the app.

Any ideas?

Thanks a lot

Hanzo
  • 1,839
  • 4
  • 30
  • 51
  • possible duplicate of [High resolution Image - OutOfMemoryError](http://stackoverflow.com/questions/18385362/high-resolution-image-outofmemoryerror) – Pararth Jan 27 '15 at 09:47
  • I think the problem is not related to the image resizing or scaling, it's related to the showing many images. While displaying the images into the gridview or any other view, keep size small, while show images in full size only when user clicks the image to view, same as gallery app. Also use re-usable views. – jitain sharma Jan 27 '15 at 10:17

1 Answers1

1

The following has worked for me in similar situations: Android doesn't really free a bitmap after it drops out of scope. Leaving a high memory usage that quite often does not get cleaned.

Keep a handle to the bitmap you are setting

private Bitmap storedBitmap = null;

Change the OnClickListener() to:

imgTest.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

       final Bitmap img = decodeSampledBitmapFromDrawable(Test.this,tipoprueba, widthScreen, heightScreen);
       imgTest.setImageBitmap(img);

       if(storedBitmap != null){
           storedBitmap.recycle();
           storedBitmap = null;
       }
       storedBitmap = img;

    }

});
Chris Handy
  • 366
  • 1
  • 5
  • Do you mean `img = null` or `storedBitmap=null`? – Hanzo Jan 27 '15 at 11:41
  • `storedBitmap=null` just something I tend to do, always set a bitmap to null after it has been `recycle()` – Chris Handy Jan 27 '15 at 11:43
  • Can I set directly `img = null` instead of create a bitmap storedbitmap linked? – Hanzo Jan 27 '15 at 11:54
  • Regarding the first part of my question, How could scale the image (square) to the total height of the screen(landscape) keeping aspect ratio without using `android:layout_width="match_parent"`? I need keeping aspect ratio and at the same time, detect when the image, and not the background, is touched.Thx – Hanzo Jan 27 '15 at 11:59
  • To make it detect touches. Have the view `clickable=true` and `onClick=image_touched` in the code for the activity make sure you have `public void image_touched(View v){ //handle click here }` As for keeping it square best option is probably creating a subclass of `ImageView` and overwriting the `OnMeasure()` [Like this example](http://stackoverflow.com/questions/16506275/imageview-be-a-square-with-dynamic-width) – Chris Handy Jan 27 '15 at 12:18