gpt4 book ai didi

Android GridView 在某些设备(三星 Galaxy S4、S3)上崩溃或速度非常慢

转载 作者:行者123 更新时间:2023-12-02 08:57:32 24 4
gpt4 key购买 nike

我的应用程序中有一个 GridView,它在几乎所有设备上都非常棒,但是...我用它来测试一些 friend ,他们说我的应用程序在 GridView 上崩溃了..应用程序崩溃了或者他们必须滚动真的很仔细地在里面。有些 friend 说 GridView 还可以(还有我的 - HTC Wildfire、三星 Galaxy Mini),但我拥有三星 Galaxy S3、S4 的 friend 说它崩溃了。我的 GridView :

public static List<Item> items = new ArrayList<Item>();

...

GridView gridView = (GridView) findViewById(R.id.gridview);
items.clear();
//add all items to list

gridView.setAdapter(new ImageAdapter(this, items));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("id", position);
startActivity(i);

}
});

图像适配器:

public class ImageAdapter extends BaseAdapter {

private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;

public ImageAdapter(Context context, List<Item> items) {
inflater = LayoutInflater.from(context);
this.items = items;
}

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

@Override
public Object getItem(int i) {
return items.get(i);
}

@Override
public long getItemId(int i) {
return items.get(i).drawable;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;

if (v == null) {
v = inflater.inflate(R.layout.squareimageview, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}

picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);

Item item = (Item) items.get(i);

picture.setImageResource(item.getDrawable());
name.setText(item.name);

return v;

}

}

我的 SquareImageView 类,它将 GridView 元素的大小调整为 2 列,并在多个屏幕上看起来不错:

public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}

}

最佳答案

来自 setImageResource() 的文档。 (强调是我的)

This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

我强烈建议您使用Picasso用于显示图像的库。将库添加到项目后,在 getView() 中调用以下函数:

Picasso.with(context).load(item.getDrawable()).into(picture);

就是这样。

<小时/>
public class ImageAdapter extends BaseAdapter {

private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;

private Context context;

public ImageAdapter(Context context, List<Item> items) {
inflater = LayoutInflater.from(context);
this.items = items;
this.context = context;
}
.
.
.
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;

if (v == null) {
v = inflater.inflate(R.layout.squareimageview, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}

picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);

Item item = (Item) items.get(i);

Picasso.with(context).load(item.getDrawable()).into(picture);
name.setText(item.name);

return v;
}
}

关于Android GridView 在某些设备(三星 Galaxy S4、S3)上崩溃或速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20332170/

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