花时间在android中加载背景图像

时间:2016-07-28 05:21:10

标签: android image picasso

我正在使用Picasso加载背景图片,而我正在使用相对布局。背景图像需要时间来加载,有些甚至没有加载。我的代码是,

        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()Picasso Taking time to load images,但它无效。

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

从您的上述评论中可以清楚地看到,您正在使用Picasso加载图片并且您的图片存储在本地

因此,根据上述情况,这里是解决方案

首先停止使用Picasso,b&#39; se当图像是本地图像时,无需使用第三方库加载它们。

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

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

在cardview之后,将您的图片视为&#34; @ + id / backgrd&#34; 作为第一个孩子,如下所示

 <?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的背景

希望这对你有用!!

快乐编码;)