我将通过recyclerview在我的CardView中显示图像。有15个图像,总共15毫克大小。当我在genymotion模拟器(自定义手机4.1.1 Api16规范)上运行应用程序时,我得到了内存不足错误。 错误在这一行:
foodViewHolder.txtImage.setImageResource(food.getConverted_image());
这是我的适配器
package com.example.android.dezcook;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import org.w3c.dom.Text;
import java.io.IOException;
import java.util.List;
/**
* Created by Android on 6/11/2016.
*/
public class Food_Adapter extends RecyclerView.Adapter<Food_Adapter.foodViewHolder>{
private List<Food> foodList;
private Context context;
public Food_Adapter(List<Food> foodList,Context context)
{
this.foodList=foodList;
this.context=context;
}
@Override
public foodViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView= LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.card_layout,viewGroup,false);
return new foodViewHolder(itemView);
}
@Override
public int getItemCount() {
return foodList.size();
}
@Override
public void onBindViewHolder(foodViewHolder foodViewHolder, int i) {
Food food=foodList.get(i);
foodViewHolder.txtName.setText(food.getTxtName());
foodViewHolder.txtItems.setText(food.getItems());
foodViewHolder.txtId.setText(Integer.toString(food.getTxtid()));
foodViewHolder.txtImage.setImageResource(food.getConverted_image());
}
//food.getTxtImage()
public static class foodViewHolder extends RecyclerView.ViewHolder {
protected TextView txtName;
protected TextView txtItems;
protected ImageView txtImage;
protected TextView txtId;
public foodViewHolder(View v) {
super(v);
txtName=(TextView)v.findViewById(R.id.txtName);
txtItems=(TextView)v.findViewById(R.id.txtItems);
txtImage=(ImageView)v.findViewById(R.id.card_thumbnail);
txtId=(TextView)v.findViewById(R.id.txtid);
}
}
}
我出示卡片的方式:
public void show_card()
{
RecyclerView reclist=(RecyclerView)findViewById(R.id.cardList);
reclist.setHasFixedSize(true);
LinearLayoutManager llm=new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
reclist.setLayoutManager(llm);
Food_Adapter fa=new Food_Adapter(Configure_bank(),this);
reclist.setAdapter(fa);
}
和我的卡片布局xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:onClick="show_details"
android:id="@+id/cardview"
android:background="@color/colorlighter"
card_view:cardCornerRadius="5dp"
tools:showIn="@layout/activity_second">
<RelativeLayout
android:background="@color/colorlighter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
android:layout_margin="0dp"
>
<RelativeLayout
android:background="@drawable/xml_rounded_corner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
>
<TextView
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="10dp"
android:id="@+id/txtName"
android:background="@drawable/xml_header_background"
android:textStyle="bold"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemcontainer"
android:layout_toRightOf="@+id/card_thumbnail"
android:layout_below="@+id/header"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="مواد مورد نیاز:"
android:textStyle="bold"
android:layout_alignParentRight="true"
android:textColor="@color/cardview_dark_background"
android:id="@+id/itemid"
/>
<TextView
android:layout_below="@+id/itemid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtItems"
android:layout_alignParentRight="true"
android:textSize="16dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:lineSpacingExtra="5dp"
android:textColor="@color/cardview_dark_background"
/>
</RelativeLayout>
<ImageView
android:id="@+id/card_thumbnail"
android:layout_below="@+id/header"
android:layout_width="120dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:paddingBottom="5dp"
android:layout_alignParentLeft="true"
android:layout_height="120dp"
android:src="@drawable/z8"
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="@+id/txtid"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:1)
使用毕加索库,在 gradle依赖中添加{> compile 'com.squareup.picasso:picasso:2.5.2'
Picasso
.with(getActivity())
.load(mostPopular)
.get("your_image")
.fit()
.into("your_imageview");
答案 1 :(得分:0)
你真的不需要这么大小的形象。只需使用Picasso来加载和调整图像大小,例如:
public void onBindViewHolder(final ImageViewHolder holder, int position) {
Picasso.with(context)
.load(itemList.get(position).getLink())
.error(R.drawable.error_image)
.placeholder(R.drawable.progress_animation)
.into(holder.image);
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
int targetWidth = parentWidth / 2;
float ratio = (float) bitmap.getHeight() / (float) bitmap.getWidth();
float heightFloat = ((float) targetWidth) * ratio;
final android.view.ViewGroup.MarginLayoutParams layoutParams
= (ViewGroup.MarginLayoutParams) holder.image.getLayoutParams();
layoutParams.height = (int) heightFloat;
layoutParams.width = (int) targetWidth;
holder.image.setLayoutParams(layoutParams);
holder.image.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
因此,您可以摆脱OutOfMemory问题。