2
我的應用程序正常工作,但在將重新調整大小的圖像包含在RecyclerView後變得非常慢,爲什麼會發生這種情況?在RecyclerView中緩存應用程序後圖像
public class AlunoAdapter extends RecyclerView.Adapter<AlunoAdapter.AlunoViewHolder> {
private List<Aluno> alunos;
private Context context;
private static ItemClickListener clickListener;
private static LongItemClickListener longItemClickListener;
public AlunoAdapter(List<Aluno> Alunos, Context context) {
this.alunos = Alunos;
this.context = context;
}
@Override
public AlunoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.aluno_celula, parent, false);
return new AlunoViewHolder(view);
}
@Override
public void onBindViewHolder(AlunoViewHolder holder, int position) {
Aluno aluno = alunos.get(position);
holder.nome.setText(aluno.getNome());
if (aluno.getFoto() != null) {
Bitmap fotoAluno = BitmapFactory.decodeFile(aluno.getFoto());//
Bitmap fotoReduz = Bitmap.createScaledBitmap(fotoAluno, 100, 100, true);
holder.foto.setImageBitmap(fotoReduz);//
} else {
Drawable semFoto = context.getResources().getDrawable(R.drawable.ic_no_image3);
holder.foto.setImageDrawable(semFoto);
}
}
@Override
public int getItemCount() {
return alunos.size();
}
public void setClickListener(ItemClickListener itemClickListener) {
clickListener = itemClickListener;
}
public void setLongItemClickListenerClickListener(LongItemClickListener longItemClickListenerClickListener) {
longItemClickListener = longItemClickListenerClickListener;
}
public class AlunoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
final TextView nome;
final ImageView foto;//
public AlunoViewHolder(View view) {
super(view);
nome = (TextView) view.findViewById(R.id.nome);
foto = (ImageView) view.findViewById(R.id.fotinha);//
view.setOnClickListener(this);
view.setOnLongClickListener(this);//nao tinha
}
@Override
public void onClick(View view) {
if (clickListener != null)
clickListener.onItemClick(view, getAdapterPosition(), false);
}
@Override
public boolean onLongClick(View view) {
if (longItemClickListener != null)
longItemClickListener.onItemClick(view, getAdapterPosition());
return false;
}
}
public interface ItemClickListener {
void onItemClick(View view, int position, boolean b);
}
public interface LongItemClickListener {
boolean onItemClick(View view, int position);
}
}
我是Android編程的初學者,我必須執行某些錯誤。
能否請你測試[此功能的位圖的大小(https://developer.android.com/reference/android/graphics/ Bitmap.html#getByteCount())。 'fotoReduz.getByteCount()'。 – Knossos