Background Image slow app down - android

Currently I am trying to set at the top of the activity an background image.
Specifications of the image:
Height: 290dp
Width: match_parent
ScaleType: cropCenter
The background image is stored in #drawables. The image has a size of 750 x 600 px.
It's 41,3 KB large.
The XML of my activity looks like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6">
<ImageView
android:layout_height="290dp"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/background_rocket">
</ImageView>
<ListView
android:id="#+id/list"
android:paddingTop="240dp"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:divider="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:descendantFocusability="blocksDescendants">
</ListView>
</RelativeLayout>
The result of the running application:
That's exactly what I want, but it's horrible slow (when I am running it on my S8). When I'm scrolling down, it stucks. When I am running it on the Android Studio Emulator it works fine (I think a bit slower as normal, but much better as on my real phone).
I already downloaded the plugin: Drawable Importer.
But it didn't help.
Notice: The custom ListView Adapter should be no problem, because when I remove the ImageView it runs smooth.

Add ImageView as ListView's header .
Try this .
ListView listView = (ListView) findViewById(R.id.your_listview);
View headerView = LayoutInflater.from(this).inflate(R.layout.your_image,null);
listView.addHeaderView(headerView);

If your image with 750x600 resolution is in the drawable-xxxhdpi folder, the image will be scaled on your Samsung S8. Because your device has 1440x2960 pixels resolution.
Scaling process will increase the creation time process naturally and also will consume about 5 mb from Memory, not 45 kb.
Scaling small images to larger, causes memory issues(etc. OOM Exceptions) on some bigger screen devices.
And also your images on the ListView items will be scaled too. This scaling process may be the cause of laggy scrolling issue.
Note: Always use RecyclerView for better performance and more flexibility.
For image loading you can use Glide or Picasso libraries for more performance and better caching mechanisms.

Try to load image with any image loader like Universal Image Loader, Picasso or any other image loading library

Related

ImageView.src in layout XML blocks main thread

I wanted a picture background. When I set the background property of the root element to the picture, it was distorted due to aspect ratio difference. After reading StackOverflow answers, I changed it to,
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/wallpaper1"/>
The image is a 300KB full HD image. It looks good, but the problem is that when I start the activity, there is a stutter and a warning message.
I/Choreographer: Skipped 38 frames! The application may be doing too much work on its main thread.
When I removed the ImageView, there was no such problem. Should I not use 'src' in the layout XML, and load the image on my own using a background thread?
Try to convert image in to .webp format to reduce image size, and use lossless encoding type to keep image quality.
In Android Studio
1. right click on image asset
2. click Convert to WebP
3. Choose use lossless encoding and tap (API 18+)

Two similar images (same res, size, etc) added in ScrollView, one makes it laggy, the other does not at all

I'm putting 10 different images with texts in a ScrollView, vertically. It made the app laggy depending on what images are shown in the device screen. The xml code is like this:
<ScrollView>
<LinearLayout>
<!-- 10 times -->
<LinearLayout>
<ImageView />
<TextView />
</LinearLayout>
.
.
.
</LinearLayout>
</ScrollView>
So I tried replacing all 10 images with 1 image resource only, the app became smooth, no lag at all. I tried replacing all 10 images again with another image, this time, it became very laggy again. Then I tried, replacing 9 images with the first (smooth) image and 1 image in the middle with the second (laggy) image, and the app is lagging only when it's showing the second image.
I checked these 2 images' properties though, and found out that they have the same width/height (1280*720 pixels), h/v res (96dpi), bit depth (24) and both at about 40kb only. So I'm wondering why one is so smooth to use, but the other one makes it very laggy. These are the 2 images:
smooth to use pic
very laggy pic
How can that be?
EDIT: I copied all images and pasted it to all drawable folders (xxxhdpi,xxhdpi, etc) and now it's not laggy anymore. Hmm..?
The thing that slows down is to load a large bitmap and then display it in a much smaller space, it is far more efficiency to load it already scaled down.
Here it explains : https://developer.android.com/topic/performance/graphics/load-bitmap.html
It is the well-known system of loading thumbnails instead of the whole image so used when internet speeds were slower than the current ones.
If you are downloading the images from a database it will be more efficient if the two versions of the image, small and large, are previously stored in the database.

Android: background image not showing up on a device but showing up on every emulator

I have this weird issue. Samsung Note3 is not displaying background image of one of my activities:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fullscreen_bg"
tools:context="com.myapp.views.activities.LoginActivity">
</RelativeLayout>
fullscreen_bg image is present in each drawable folder. Every emulator I've run my app on was displaying this image correctly but on this phone model it's not. Any advice?
What is the resolution of your image?
I think is because the resolution of the image. Try to scale down the image to the half just to do a test.
Also if you are using different drawable-XXX, try to use only "drawable" without sufixes.

Avoid image scaling in ImageView

I am using an Image view with these XML attributes
<ImageView
android:id="#+id/widget_list_bitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:scaleType="center"
android:src="#drawable/place_holder" >
</ImageView>
and at runtime I create a let's say 400x400 bitmap, save it to a file and set the ImageView src using a file URI
remoteViews.setUri(R.id.widget_list_bitmap, "setImageURI", myFileUri);
(this is to avoid RemoteViews issues with large bitmaps).
Everything works fine except that the displayed image is scaled down and is smaller than 400x400, more like 250 x 250 (possibly 400/density, density = 1.5).
Is there a way to force an ImageView to really not scaling images it fetches from URI?
You could manually download it and when you load it with BitmapFactory set inScaled to false. Then you would use setImageViewBitmap instead of setUri or setImageViewUri.
Update:
Unfortunately I don't really see a way around this using setUri. An alternative to using setUri to get around the 1MB limit is to use setImageViewBitmap but slice up the bitmap and display it on multiple views, maybe a 2x2 or 3x3 grid of ImageViews or something like that. You'd have to split it into multiple updates using partiallyUpdateAppWidget, but I think it should work. The hitch is that this requires API 11.

Why android resizes image?

I'm trying to change my application data source to the sdcard so that it doesn't take too much internal memory.
However, I had an Image that was dynamically loaded from the resource and displayed in an ImageView, now I load the image from the sdcard whenever I need it.
I had been using imageBox.setImageResource(imageID) and I changed to imageBox.setImageDrawable(imageDrawable).
The problem is that now Android resizes my image and it doesn't fit where it should.
Why is it resizing the image this way? And how can I stop it?
Thanks in advance :)
Add the tag android:scaleType="fitXY" to your ImageView's layout XML.
Ex.
<ImageView
... several settings ...
android:scaleType="fitXY"/>
Edit:
If your image is simply too big to fit inside the ImageView - it will have to do some kind of scaling. There are some other options as described in the API:
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Try the following for ImageView:
android:layout_height="fill_parent"
android:layout_width="fill_parent"
No Scale Type.
The ImageView Should be within Fixed Size (in dp) Parent View.

Categories

Resources