2016-07-28 55 views
1

我使用Picasso加载背景图像,并使用相对布局。背景图片需要时间加载,有些甚至没有加载。我这段代码是,花时间在android中加载背景图像

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

我试图用调整大小()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" 
     /> 
+0

什么是你的简洁问题? – RabidMutant

+0

检查带宽或者图像是否存储在本地,然后使用UniversalImageLoader –

+0

@RabidMutant我没有得到您的问题。 – Sanik

回答

1

这是因为图像的大小。 您应该使用Photoshop或其他图形工具缩小图像的大小。它将减少您的应用程序空间并更快地加载您的图像。

滑动库也是一个更好的选项来快速加载图像。但在你的情况下,尺寸有问题。

+0

我只有19个背景图片,它的总大小是106 kB。每个图像的大小为1 kB到10 kB。我需要减少一些吗? – Sanik

+0

好的,Sanik能分享你的完整代码。 – Akshay

+0

与.xml文件一起? – Sanik

0

从你上面的评论很明显,您使用的毕加索加载图像和您的图像存储在本地

所以,根据上述方案下面是解

首先停止使用毕加索,如果有图像是本地的,则不需要使用第三方库来加载它们。

其次,在你的代码,而不是到相对布局使用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后把你的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的背景从类文件

希望这项工作ü!

快乐编码;)

+0

其实我试过使用位图代替毕加索,并在使用后回收它。因此,OOM问题得到解决,在这种背景下,我缩小了一点。它工作正常。谢谢大家。我的一块代码位图 – Sanik

+0

if(imageView!= null) imageView.setImageBitmap(null);如果(位图!=空){ bitmap.recycle(); } bitmap = null; 位图位图= BitmapFactory.decodeStream(getResources()。openRawResource(resourceId)); imageView.setImageBitmap(bitmap); – Sanik