2016-07-28 38 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