所以我想我的應用程序能夠在列表視圖中有效地顯示圖像,以防止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>
如何解決它,將不勝感激任何見解相關!
這似乎是不成熟的優化。你在做什麼,而是造成了OOM? –
這不是不成熟的優化,這是OOM的一種常見形式。他只是不是對的。或者做適配器的權利,他總是創建新的意見,並不會重複使用convertView –
如果你想我的鏈接我刪除了我的答案,並把鏈接在這裏http://stackoverflow.com/questions/35664298/recyclerview-stutters-when-srolling-only -14圖像/ 35664641#35664641 –