在android中加载背景图像需要时间

在android中加载背景图像需要时间,android,image,picasso,Android,Image,Picasso,我使用毕加索加载背景图像,并使用相对布局。加载背景图像需要时间,有些甚至没有加载。我的代码是 final int k = random.nextInt(20); ImageView imageView= (ImageView)findViewById(R.id.backgrd); TypedArray imgb = getResources().obtainTypedArray(R.array.backg); int resourc

我使用毕加索加载背景图像,并使用相对布局。加载背景图像需要时间,有些甚至没有加载。我的代码是

        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);
我也尝试过使用resize(),但它不起作用

<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"
        />

检查用于加载的可绘制图像的大小和分辨率

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

从您的上述评论可以清楚地看出,您是在使用毕加索加载图像,并且您的图像存储在本地

因此,根据上述场景,这里是解决方案

首先,停止使用毕加索,当图像是本地的,就不需要使用第三方库来加载它们

其次,在代码中,使用CardView作为主布局,而不是相对布局,如--


在cardview之后,将您的imageVIew“@+id/backgrd”作为第一个子项,如下所示

 <?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>

现在从类文件设置imageView的背景

希望这对你有用


快乐编码;)

您最简洁的问题是什么?检查带宽或图像是否存储在本地,然后使用UniversalImageLoader@RabidMutant我不明白你的问题。我不确定你的问题是(a)为什么picasa不起作用?(b) 谁慢?(c) 我怎样才能做得更快?(d) 如何在后台线程中执行此操作?或者(e)当picasa无法加载图像时,我应该怎么做?让您的问题更清楚、更具体。所以,你会得到更精确的解。我只有19张背景图片,它的总大小是106 kB。每个图像的大小为1KB到10KB。我需要再减少一些吗?还有.xml文件?事实上,我试着用位图代替毕加索,并在使用后回收了它。因此,OOM问题得到了解决,在这种背景下,我将其大小缩小了一点。它工作得很好。谢谢大家。我的bitmapif(imageView!=null)imageView.setImageBitmap(null)的代码片段;如果(位图!=null){bitmap.recycle();}bitmap=null;位图Bitmap=BitmapFactory.decodeStream(getResources().openRawResource(resourceId));设置图像位图(位图);
 <?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>