Sanik
Sanik

Reputation: 35

Taking time to load background images in android

I am using Picasso to load the background images and I am using relative layout. The background images are taking time to load and some are even not loading. My piece of code is,

        final int k = random.nextInt(20);
        ImageView imageView= (ImageView)findViewById(R.id.backgrd);
        TypedArray imgb = getResources().obtainTypedArray(R.array.backg);
        int resourceId = imgb.getResourceId(k, 0);

 Picasso.with(getApplicationContext()).
                load(resourceId).
                fit().
                noPlaceholder().
                into(imageView);

I tried to use resize() Picasso Taking time to load images also, but it is not working.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative1"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


        <ImageView
            android:id="@+id/backgrd"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            />

     <ImageView
        android:id="@+id/img1"
        android:paddingTop="90dp"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        />

Upvotes: 1

Views: 802

Answers (2)

Akshay
Akshay

Reputation: 1762

Check size and resolution of image drawable you are using to load.

xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
Generalised Dpi values for screens:

ldpi Resources for low-density (ldpi) screens (~120dpi)
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).
Therefore generalised size of your resources (assuming they are full screen):

ldpi
Vertical = 426 * 120 / 160 = 319.5px
Horizontal = 320 * 120 / 160 = 240px
mdpi
Vertical = 470 * 160 / 160 = 470px
Horizontal = 320 * 160 / 160 = 320px
hdpi
Vertical = 640 * 240 / 160 = 960px
Horizontal = 480 * 240 / 160 = 720px
xhdpi
Vertical = 960 * 320 / 160 = 1920px
Horizontal = 720 * 320 / 160 = 1440px

px = dp*dpi/160

https://stackoverflow.com/a/16352208/5567283

Upvotes: 1

Hiren Dixit
Hiren Dixit

Reputation: 117

From your above comment it is clear that you are using Picasso to load the images and your images are stored locally

So according to above scenario here is the solution

First of all stop using Picasso, b'se when ever the images are local there is no need to use 3rd party library to load them.

Second, in your code instead to Relative Layout use CardView as Main layout Like --

 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/VechicleCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="@dimen/margin_small_five"
    app:cardElevation="@dimen/card_elevation_two"
    app:cardUseCompatPadding="true">


</android.support.v7.widget.CardView>

After cardview put your imageVIew "@+id/backgrd" as the first child as below

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="0dp"
    app:cardMaxElevation="0dp">

    <ImageView
        android:src="@drawable/your_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>

    <... your layout

    .../>


</android.support.v7.widget.CardView>

Now set the Background of the imageView from the Class file

Hope this work for u !!

Happy Coding ;)

Upvotes: 0

Related Questions