2016-02-26 78 views
0

所以我想我的應用程序能夠在列表視圖中有效地顯示圖像,以防止OOM錯誤。我嘗試通過將圖像轉換爲位圖來實現此目的,然後將其設置爲framelayout中的背景,然後將此xml文件用作我的listview佈局。但是,它不顯示任何內容。試圖有效地加載位圖導致沒有圖像顯示

這是適配器類。

public class CustomAdapter extends BaseAdapter { 

String [] result; 
Context context; 
int [] imageId; 
String [] peopleId; 
private static LayoutInflater inflater=null; 
public CustomAdapter(MyMainActivity mainActivity, String[] prgmNameList, int[] prgmImages, String[] peopleImages) { 
    // TODO Auto-generated constructor stub 
    result=prgmNameList; 
    context= mainActivity; 
    imageId=prgmImages; 
    peopleId=peopleImages; 
    inflater = (LayoutInflater)context. 
      getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 
@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return result.length; 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public class Holder 
{ 
    FrameLayout frame; 
    ImageView pic; 
    TextView tv; 
    TextView imgPeople; 
    } 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    // TODO Auto-generated method stub 
    Holder holder=new Holder(); 
    View rowView; 
    rowView = inflater.inflate(R.layout.program_list_upcoming, null); 
    rowView.setRotation(5); 
    holder.frame=(FrameLayout) rowView.findViewById(R.id.frame); 
    holder.pic = (ImageView) rowView.findViewById(R.id.frameBackPic); 
    holder.imgPeople=(TextView) rowView.findViewById(R.id.peopleImage); 
    holder.tv=(TextView) rowView.findViewById(R.id.textView1); 
    holder.imgPeople.setText(peopleId[position]); 
    holder.frame =(FrameLayout) rowView.findViewById(R.id.frame); 
    holder.tv.setText(result[position]); 

    Bitmap bitmap = decodeSampledBitmapFromResource(Resources.getSystem(), imageId[position],400, 130); 
    BitmapDrawable myDrawable = new BitmapDrawable(this.context.getResources(), bitmap); 
    holder.pic.setImageBitmap(bitmap); 
    holder.pic.requestLayout(); 

    rowView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show(); 
     } 
    }); 
    return rowView; 
} 

//This is to convert the imageIDs into a array of small bitmaps 
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
                int reqWidth, int reqHeight) { 
    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 
    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 

//this adjusts the size of the bitmap as its needed accordign to the layout dimentsions 
public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     final int halfHeight = (int)(height/1.5); 
     final int halfWidth = (int)(width/1.5); 
     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 
    return inSampleSize; 
} 

}

這是XML文件,它

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#000000" 
    android:rotation="5"> 
<!-- First frame layout is to have the picture underneath everything--> 
<FrameLayout 
    android:id="@+id/frame" 
    android:layout_width="fill_parent" 
    android:layout_height="130dp" 
    android:background="#a8000000"> 

    <ImageView 
     android:id="@+id/frameBackPic" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="fitXY" /> 

    <!-- First frame layout is to have the bar--> 


    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="25dp" 
     android:layout_gravity="fill" 
     android:layout_marginTop="83dp" 
     android:background="#b8000000"> 

     <LinearLayout 
      android:id="@+id/buttonPanel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="bottom" 
      android:orientation="horizontal" 
      android:weightSum="14"> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentLeft="true" 
       android:layout_centerHorizontal="true" 
       android:layout_gravity="left" 
       android:layout_weight="10" 
       android:paddingLeft="10dp" 
       android:textColor="#d1d1d1" 
       android:textSize="18dp" /> 

      <TextView 
       android:id="@+id/distance" 
       android:layout_width="wrap_content" 
       android:layout_height="30dp" 
       android:layout_gravity="right" 
       android:layout_weight="1" 
       android:paddingLeft="10dp" /> 

      <TextView 
       android:id="@+id/peopleImage" 
       android:layout_width="fill_parent" 
       android:layout_height="30dp" 
       android:layout_gravity="right" 
       android:layout_weight="1" 
       android:paddingLeft="10dp" /> 

     </LinearLayout> 
    </FrameLayout> 


    <!-- Second frame layout is to have the translucent bar acr0ss--> 
    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:background="#a8000000"> 

     <TextView 
      android:id="@+id/description" 
      android:layout_width="wrap_content" 
      android:layout_height="17dp" 
      android:layout_below="@+id/buttonPanel" 
      android:layout_gravity="center_vertical" 
      android:layout_marginBottom="5dp" 
      android:background="#7e000000" 
      android:paddingLeft="15dp" /> 
    </FrameLayout> 
</FrameLayout> 

如何解決它,將不勝感激任何見解相關!

+1

這似乎是不成熟的優化。你在做什麼,而是造成了OOM? –

+1

這不是不成熟的優化,這是OOM的一種常見形式。他只是不是對的。或者做適配器的權利,他總是創建新的意見,並不會重複使用convertView –

+0

如果你想我的鏈接我刪除了我的答案,並把鏈接在這裏http://stackoverflow.com/questions/35664298/recyclerview-stutters-when-srolling-only -14圖像/ 35664641#35664641 –

回答

0

這裏有多個問題。

1)您沒有使用listview正確。你的getView應該重用convertView如果不是null,並且只有在它爲null時才創建一個新視圖。你不是。這使得你的代碼完全沒有效率,根本就不能回收。

2)您的解碼函數不解碼位圖。它唯一解碼bitmap- inJustDecodeBounds的大小設置爲true。所以你會一直返回null。

3)您不在任何形式的緩存中保存位圖。這意味着每次調用getView時都會解碼位圖,在每個滾動事件中每行將會執行一次。你實際上在做的比不試圖高效。

4)你正在用資源做這件事。在啓動時,資源已經充實到完整的位圖中。如果正確完成,這種優化只適用於圖像文件。

5)你正在從資源中繪製一個位圖。這是在啓動時爲你完成的。

如果您使用的是從網上下載的位圖文件或位圖,這是一個需要的優化。隨着你在做什麼,你實際上會降低性能並傷害你的應用程序。