android: long loading time for background of imagebutton - android

I am designing an app of which there is an index page that link to several content layouts, and each layout has an imagebutton in the middle of the screen. The code for the ImageButton is as follows:
<ImageButton
android:id="#+id/image_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="#drawable/transparent_btn"
android:focusable="false"
android:gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/ice_cream400" />
The ice_cream400 is an png of 400*400px and around 148KB.
Question:
I always come across with problems about images.
The png are already put under the drawable-xhdpi folder as suggested from research as the system will shrink the size automatically.
I am running the app in real phone device and discover that from time to time loading the content layout takes a lot of time (around 2 sec). How could the performance be improved? Need fetching the dimension of the screen and scale down before import? (the imagebutton would occupy around 70% of the screen)
Thanks!

Related

why the upper part of recyclerview item is erased?

the upper part of recyclerview item is erased in some phones and is normal in other phones as the images below.
the first image is normal which from samsung phone (android 12 )
the second image has the problem which from huawi phone (android 8)
this is the code
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="70dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:layout_margin="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/itemImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:riv_oval="true"
/>
<View
android:layout_width="1dp"
android:layout_height="1dp"
android:id="#+id/viewSupporter2"
app:layout_constraintBottom_toBottomOf="#id/itemImage"
app:layout_constraintEnd_toEndOf="#id/itemImage"
app:layout_constraintStart_toStartOf="#id/itemImage"
app:layout_constraintTop_toTopOf="#id/itemImage"
/>
<TextView
android:id="#+id/diseasename"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="disease name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black"
app:layout_constraintBottom_toTopOf="#id/viewSupporter2"
app:layout_constraintStart_toEndOf="#id/itemImage"
/>
<TextView
android:id="#+id/diseasenickname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text= "disease nickname"
android:textColor="#color/teal_700"
app:layout_constraintTop_toBottomOf="#id/viewSupporter2"
app:layout_constraintStart_toEndOf="#id/itemImage"
tools:ignore="NotSibling" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
You're doing a basic mistake of having your cards at fixed height of 70dp. Change them to be wrap_content and tweak margins/paddings of your layout instead to achieve the size you need.
While it looks ok on device you designed it for you can see it cannot fit TextViews on the other one so they ends up being clipped (on top). Actually you can even break it on your Samsung if you go into device settings and increase font size.
You have to let your card view "grow" in those cases so unrestraining height it is the way to go. Although you might consider adding maxlines to your textviews so they don't get too out of hand with maximum font size.
The problem here is your second device is set to a larger system font size than the first one. The dp system is designed so that things look consistent on different devices, and you can see that the ImageView takes up pretty much the same amount of space on both - the space around it is the same, the layout looks the same.
But you have to account for the user changing their overall text size, which is what's happening on your second image. See how the text is just bigger? Not just in your list items - the diseases text is taller than the hamburger icon on this one, the tab headers are much closer together because the words are wider...
You have three options in this case
use dp instead of sp for the text sizes (so they're consistent and don't scale to the user's preference)
make sure your fixed layout has enough space for those larger text sizes
make sure it can expand as necessary to fit them
The first option is ok sometimes, when you have text that's already large enough for everyone, and you want to maintain some kind of visual consistency (like a fixed design where things are a certain size etc). Don't do this as a way to get around the limitations of having to provide accessible content, that scales so the user can read it.
The flexible layout approach is what Pawel's suggesting - basically design your layout so it can grow if necessary. You can use the minWidth and minHeight layout attributes for this (and layout_constraintHeight_min in ConstraintLayout) to design how you want it to look, but use things like wrap_content and constraints to allow it to grow beyond that if necessary. That way you get it looking how you like, but if the user needs bigger text, it compromises.
And don't worry if you don't like how it looks with large text - that's not important! What's important is that the user can read it, and everything's visible and accessible. Nothing cut off, nothing overlapping anything, everything clickable etc. The user is the one making the compromise between style and practicality, and they probably don't have a choice, so just making it usable in that situation is the main thing.
The middle option is the fixed layout that has enough space for the larger text settings. This is actually what's recommended in the Material Design spec - I can't link directly to the Two line version, but go here and scroll down - there are some similar to what you're doing:
Those measurements are all in dp - notice that the top line's position is defined by where its baseline is, the line the text "rests" on, relative to the top of the layout. You can do that with:
android:layout_height="wrap_content"
app:firstBaselineToTopHeight="32dp"
app:layout_constraintTop_toTopOf="parent"
And then the text below has its baseline relative to the bottom of the first
app:layout_constraintBaseline_toBaselineOf="#id/firstTextView"
app:layout_marginBaseline="20dp"
or you could constrain it to the top of the layout like the first one, with a value of 32 + 20dp.
Notice that this design leaves space for the text to grow vertically (up from the baseline, which is in a fixed position). If you use the Material Design type scale (which is a bunch of standard text styles and font sizes with names like Subtitle and Body1) then those list item specs should have enough room to hold the text even at the larger font settings. Both lines in that image can pretty much double in height and still fit
The nice thing about these specs is someone's done the work coming up with a design for you! And they're working with fixed sizes, so you get a nice consistent look to your lists

Putting drawables in front of one another causes dramatic used memory increase

Usual memory footprint of my app is around 20MB. However, when I put drawables one in front of the other (for the sake of animating the each individual element)...
There are actually two drawables in this picture: the green base and the pin (with its shadow)
...my memory usage goes up by ~40MB!The layout is using xhdpi drawables, which have around 2.1MB in total. The layout of these drawables is:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.2">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/serbia"
android:src="#drawable/serbia"
android:adjustViewBounds="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pin"
android:src="#drawable/pin"
android:adjustViewBounds="true"/>
</RelativeLayout>
What is causing such sudden memory increase? Is there a way to bypass such practice, but still be able to animate each part of the image separately?
Note that I still have to properly align these elements (the pin isn't in the center of the layout), because the right edge of the shadow is cropped to the shape of the base)
The pin and the shadow may be small but the view size is the same as the parent as you layout them so the bigger one (the parent) sets the size of all them three buffers for painting. So the biggest size multiplied by 3 (or four, I donno how actually the container paints itself) is the memory used. And by biggest I mean biggest in screen format, not in the file compressed.
Best thing to do could be to paint them yourself or stack them up using a FrameLayout and then setting x and y possition... Donno right now if u can set thrm in the xml directly but, if not, set them in code elsewhere.

How to make an image fit in an imageButton in android?

I need to make four imageButtons in my app. Whenever I insert a JPEG image into Android Studio and put it on the imageButton, the button just grows in size so that the image doesn't fit on the button.
I need the button to just stay the same size that I've made it and need the image to fit nicely and be centered on the button. I've tried everything. Does anyone know how to do this? Can this be done with any sized image of any format?
To make your image fill ImageButton then use - android:scaleType="fitXY"
<ImageButton
android:id="#+id/TextButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="0.5"
android:scaleType="fitXY"
android:src="#drawable/jpeg_image" />

Android - Images showing at different sizes on different devices

I am coming from the world of iOS so bear with me. I am getting a little confused about image scaling and why my images are appearing different sizes on different devices.
My app uses a large background image and in the middle are placed a number of other images in a stacked order. They are placed in a colored area which is part of the background image.
When viewing my layout in the Graphical View different phones will show the individual tile images at different sizes. Sometimes they will all be contained within the dark grey background block and sometimes they overflow. Sometimes they are too small.
I am creating my background image:
<ImageView
android:id="#+id/backgroundImageView"
android:layout_width="1000dp"
android:layout_height="1000dp"
android:layout_gravity="center"
android:contentDescription="Background Image"
android:scaleType="centerCrop"
android:src="#drawable/background2" />
I gave it specific height and widths, otherwise the background image is way too small, much smaller than its actual size.
Example of the two left individual images:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="-7dp" >
<ImageView
android:id="#+id/digit5top"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:src="#drawable/top" />
<ImageView
android:id="#+id/digit5bottom"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:layout_marginTop="38dp"
android:src="#drawable/bottom" />
</RelativeLayout>
How can I get my background image and individual tile images to appear the same size/proportion on all devices?

Supporting multiple Screen sizes with Button design

I have created a button in Photoshop. The problem is that when I test app from a big screen phone everything is good, but when I test in smaller phone then button is very big. How can I make good size button for every phone sizes?
I suppose by "I designed a button in Photoshop" you mean that you designed its background-image.
The size of a Button is actually not necessarily dependent on the size of the background image. You can control the size of your button via its properties that can be set in the layout.xml file (or in code).
In this simple example the Button will always use up exactly half of the parent layouts width (LinearLyout).
(Watch the "weightSum" and "weight" properties).
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:weightSum="2">
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button"
android:layout_weight="1" />
</LinearLayout>
Of course, there are many more ways and properties of how to adjust your layout to different screen sizes, this should just give you a brief example of how it could be done.
For more information, check out this tutorial from the Google-Android-Developer page:
Supporting Multiple Screen Sizes (especially Density-Independence should be interesting for you)
http://developer.android.com/guide/practices/screens_support.html

Categories

Resources