Android: slow performance using large image in ImageView

I have an Activity where I have a background image in ImageView and nine transparent buttons on top of the image.

http://i.stack.imgur.com/GGQqd.png

When I press any of the buttons, I do a RelativeLayout at the top of the background.

http://i.stack.imgur.com/JvKQK.jpg

The problem is this: When I use a high resolution image as the background in the layout, the performance is very poor.

I use a 2048 x 1536 PNG image and its weight is about 500 thousand. When I use a smaller image, for example 1024 x 768 , the application works fine, but it looks awful.

Is there a limitation or way to use large images without losing performance?

+7
performance android image relativelayout imageview
source share
2 answers

This bitmap is huge . You mentioned 500 thousand. This is a compressed size. Images are uncompressed in memory when you use them. You are really looking at 2048 × 1536 × 4 = 12582912 = more than 12 MB of memory usage.

If you are on a device such as the Nexus 9 that has such a screen resolution, then you can assume that it also has memory, a graphics processor, and bus bandwidth to process an image of this size. However, if you use a device with a lower resolution (most devices, remember that even Full HD is only 65%), then this image is phenomenally wasteful for memory. Today you can buy low-resolution devices with a 240x320 screen, where a full-screen raster image is only 2.5% of the size of your image.

You will have to scale this image when downloading it. Use BitmapFactory.Options to set the desired size before loading the image.

In addition, you place text directly on top of it. Rendering text requires alpha transparency, which requires fetching the main image. If you can put text on an opaque background and place it on top, you will also save the load on the GPU, but I'm not really sure how much performance you are going to get. It may not be so much.

+10
source share

You can use Dave Morissey's “Predict Image Scaling” as a replacement for replacing a standard image.

XML layout:

 <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:id="@+id/photo"/> </RelativeLayout> 

Set image in code and disable touch:

 photo = (SubsamplingScaleImageView) view.findViewById(R.id.photo); photo.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); photo.setImage(ImageSource.uri(Uri.fromFile(new File("/somepath/file.png")))); 

build.gradle:

compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0'

+1
source share

All Articles