1

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"
        />
Community
  • 1
  • 1
Sanik
  • 35
  • 12
  • What is your concisely worder question? – RabidMutant Jul 28 '16 at 05:25
  • Check bandwidth or if the images are stored locally, then use UniversalImageLoader – Geet Choubey Jul 28 '16 at 05:26
  • @RabidMutant I didn't get your question. – Sanik Jul 28 '16 at 05:29
  • I was not sure if your problem was (a) why doesn't picasa work? (b) who is it slow? (c) how can I do it faster? (d) how do I do it in a background thread? or (e) what should I do when picasa fails to load an image? – RabidMutant Jul 28 '16 at 05:35
  • Make your question more clear and specific. So, u will get more precise solution. – Hiren Dixit Jul 28 '16 at 05:38
  • @RabidMutant My Picasso is working fine but the thing is it is not loading the images immediately. First the foreground images appear and then the background image appears which is not expected. – Sanik Jul 28 '16 at 05:39
  • @HIRENDIXIT I think I am quite clear on my question. I am using picasso to load the background images. But it is not loading along with the foreground images. First the foreground image appears and then after sometime the background image appears. I have provided the code also. – Sanik Jul 28 '16 at 05:42
  • @KNeerajLal Images are stored locally in the disk. So what is the connection with the internet. Still it is 10 Mbps – Sanik Jul 28 '16 at 05:57
  • Why are you using Picasso if the images are stored locally? – K Neeraj Lal Jul 28 '16 at 06:00
  • @KNeerajLal Then what should I use? I have tried using setBackgorunndResources(), but it was giving me OOM error after several runs. – Sanik Jul 28 '16 at 06:03
  • You should try to fix the OOM rather than throwing in a new dependency into your project. – K Neeraj Lal Jul 28 '16 at 06:13

2 Answers2

1

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

Akshay
  • 1,702
  • 1
  • 9
  • 16
0

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

  • Actually I tried using Bitmap instead of Picasso and recycled it after using. Hence the OOM issue is solved and for that background I reduced its size a little. It is working fine. Thank everyone. My piece of code for bitmap – Sanik Jul 28 '16 at 15:28
  • if (imageView != null) imageView.setImageBitmap(null); if (bitmap != null) { bitmap.recycle(); } bitmap = null; Bitmap bitmap= BitmapFactory.decodeStream(getResources().openRawResource(resourceId)); imageView.setImageBitmap(bitmap); – Sanik Jul 28 '16 at 15:28