ImageView.src in layout XML blocks main thread - android

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+)

Related

Background Image slow app down

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

loading large bitmaps at startup

I have an intro screen based on this sample but instead of setting colors for background I set an image for each one (I have 3 screens). I have created a drawable folder for each screen density (hdpi etc) and each picture so that I can support multiple sizes and resolutions. Is there anything else I can do or should I do minimize the loading time even more ? Intellij is throwing Skipped 56 frames! The application may be doing too much work on its main thread., however, the application is responsive. I was looking at official performance guide but I don't know if it's an overkill to implement it, is placing image assets into various drawable folders enough for this case ? Or should I do those calculations on top of it ? here is what I do now:
<ImageView
android:id="#+id/welcome_image_view"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="#drawable/love"
android:scaleType="centerCrop"
tools:ignore="ContentDescription"
android:layout_height="0dp"
app:layout_constraintRight_toRightOf="parent" />
I would guess it's the code you're using to load the images more than your layout file that's causing the frame skipping.
You really have 2 goals. 1st, minimize actual loading time. Users don't want to start at empty screens or place holders more than they have to. 2nd, move the work off of the UI thread. Any image loading but the smallest of images shouldn't be done on the UI thread.
For 1, make sure your images aren't any larger than they need to be for display on a device.
For 2, the doc you pointed to recommends Glide. (Most of the rest of that doc probably doesn't apply to your case. Much of it is for when you have a full res image but only need to display a thumbnail or other reduced resolution image). AsyncTask is a common way to do simple work off of the UI thread. (Thread and Runnable are other ways but I won't get into that here).

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.

Error Run app with image

My code run without any picture but when I add an image in background and run the app in my phone. The error says - the app has stopped.
My code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/apple_01">
My image's name is apple_01.
You may be getting a OutOfMemoryExeption try to lower the pixel size of the drawable, you can use photoshop or any other software to shrink the image size
You should try with another pic from file drawable such as
#drawable/abc_ab_share_pack_mtrl_alpha
if your app run correctly you should exchange the image

Why are ?Images Compressed when Displayed on Android Phones/Emulator?

Essentially I want an HD background on a layout, but instead of being a pristine image, it shows up extremely compressed.
Example:
Source Image:
Android Emulator version:
I thought maybe that it was just the emulator, but it looks that bad on the phone itself. I know these are high quality Samsung Galaxy S phones (these are the ones that came with Avatar preinstalled after all).
Does anyone know how to load images without compression? Source files are uncompressed pngs.
here's the code for what it's worth:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1" android:background="#drawable/background" android:id="#+id/blank">
</LinearLayout>
Figured it out: The problem is the image is automatically converted into RGB565 colorspace from AGB888. To prevent this, make sure the image has an alpha channel and is in a "raw" directory instead of the "drawable" directory.
The aapt may compress images when it builds the apk, but that compression is supposed to be lossless. The problem is probably somewhere else. Here are two possibilities.
If your image resource is being loaded from res/drawable on a device (or emulator) that is not mdpi, then it will be scaled as described here. One solution is to put the resource in the res/drawable-nodpi folder. Another is to provide several, density-specific images.
Another problem will arise if your background image does not match the size of the view. The view will automatically scale the image to fit the view size. You can prevent this by defining the background as an XML Bitmap with a gravity set to something that does not scale (such as center).

Categories

Resources