gpt4 book ai didi

java - 将图像加载到滚动 Activity 时出现内存不足错误

转载 作者:行者123 更新时间:2023-12-01 17:53:26 25 4
gpt4 key购买 nike

我想在滚动 Activity 中显示 90 张图像,前 20 张图像显示没有问题,但当我向下滚动时,应用程序立即关闭所以我只想显示前 20 个对象,然后当用户向下滚动时我会销毁第一行并创建一个新行,依此类推,这样机器就不必一次加载所有 90 个对象

这是我的代码:

ArrayList<Hero> heroes;
SearchView searchView;
GridAdapter gridAdapter;
String[] names = {"Akai","(and 90 more Strings)" };

Integer[] images = {R.drawable.akai , (and 90 more Images) };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_heroes);
CreateList();
GridView gridView = findViewById(R.id.gridView);
gridAdapter = new GridAdapter(this, heroes);
gridView.setAdapter(gridAdapter);
gridView.setNumColumns(4);
searchView = (SearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
gridAdapter.getFilter().filter(newText);
return false;
}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent toAtt = new Intent(getApplication(),HeroAttributes.class);
toAtt.putExtra("item",heroes.get(position).getButton());
startActivity(toAtt);
}
});
}
public void CreateList() {
heroes = new ArrayList<>();
for (int i=0 ; i<names.length ; i++) {
Hero hero = new Hero(images[i] , names[i]);
heroes.add(hero);
}
}

我的GridAdapter代码:

    public class GridAdapter extends BaseAdapter implements Filterable {

private Context myLayout;
private ArrayList<Hero> heroes;
private ArrayList<Hero> filterHeroes;
MyFilter filter;

public GridAdapter(Context context , ArrayList<Hero> heroes ) {
this.myLayout = context;
this.heroes = heroes;
this.filterHeroes = heroes;
}

@Override
public Filter getFilter() {
if (filter == null) {
filter = new MyFilter();
}
return filter;
}

@Override
public int getCount() {
return heroes.size();
}

@Override
public Object getItem(int position) {
return heroes.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup
parent) {

LayoutInflater inflater = LayoutInflater.from(myLayout);
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_items , parent ,
false);
}
TextView textView = (TextView) convertView.findViewById(R.id.txt);
ImageView imageView = (ImageView) convertView.findViewById(R.id.btn);
textView.setText(heroes.get(position).getTextView());
imageView.setImageResource(heroes.get(position).getButton());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent toAtt = new Intent(Heroes.this,HeroAttributes.class);
toAtt.putExtra("item",heroes.get(position));
startActivity(toAtt);
}
});
return convertView;
}

class MyFilter extends Filter {

@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
constraint = constraint.toString().toUpperCase();
ArrayList<Hero> heroes2 = new ArrayList<>();
for (int i=0 ; i<filterHeroes.size() ; i++){

if(filterHeroes.get(i).getTextView().toUpperCase().contains(constraint)){
heroes2.add(filterHeroes.get(i));
}
}
results.count = heroes2.size();
results.values = heroes2;
}
else {
results.count = filterHeroes.size();
results.values = filterHeroes;
}
return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults
results) {
heroes = (ArrayList<Hero>) results.values;
notifyDataSetChanged();
}
}
}

最佳答案

您的其中一张图像可能具有高分辨率。使用 Picasso 或 Glide 来解决这个问题,而不是直接使用图像,它们非常擅长缩放图像和调整图像大小。

毕加索:https://square.github.io/picasso/

滑翔:https://github.com/bumptech/glide

毕加索的例子是

Picasso.get().load(heroes.get(position).getButton()).into(imageView);

Glide的例子是

Glide.with(context).load(heroes.get(position).getButton()).into(imageView);

直接imageView.setImageResource(heroes.get(position).getButton());

希望对你有帮助!

关于java - 将图像加载到滚动 Activity 时出现内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60760042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com