我在Android中有一个基本的GridView以及它的自定义布局。我想知道,如何让我的应用程序在显示图像时更加优化?它与它们的大小有关(我想我会坚持使用450x450px)还是我实施像Picasso这样的东西来管理图像?
GridView适配器
public final class GridViewAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;
private int i;
private String imageText;
private boolean hasDescription;
public GridViewAdapter(Context context, ArrayList<String> arrayListName, ArrayList<String> arrayListDescription) {
mInflater = LayoutInflater.from(context);
if (arrayListDescription != null){
i = 0;
while(arrayListName.size()>i) {
imageText = arrayListName.get(i).toLowerCase().replace(" ","");
mItems.add(new Item(arrayListName.get(i), arrayListDescription.get(i), context.getResources().getIdentifier(imageText, "drawable", context.getPackageName())));
i++;
hasDescription = true;
}
}
else{
i = 0;
while(arrayListName.size()>i) {
imageText = arrayListName.get(i).toLowerCase().replace(" ","");
mItems.add(new Item(arrayListName.get(i), "",context.getResources().getIdentifier(imageText, "drawable", context.getPackageName())));
i++;
hasDescription = false;
}
}
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Item getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return mItems.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
TextView description;
if (v == null) {
v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
v.setTag(R.id.description, v.findViewById(R.id.description));
}
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
description = (TextView) v.getTag(R.id.description);
Item item = getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
if (!hasDescription){
description.setPadding(0,0,0,0);
}
else{
description.setText(item.description);
}
return v;
}
private static class Item {
public final String name;
public final String description;
public final int drawableId;
Item(String name, String description, int drawableId) {
this.name = name;
this.description = description;
this.drawableId = drawableId;
}
}
}
方形图像视图
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());
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<com.example.package.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="@android:color/white"
android:textSize="15sp"
android:background="#55000000"/>
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="5dp"
android:textColor="@android:color/white"
android:textStyle="italic"
android:textSize="12.5sp"
android:background="#55000000"/>
</LinearLayout>
</FrameLayout>
目前有一些图像,我的内存使用量约为50-100mb。也许有办法将它们缓存到SD卡?你们建议使用另一个线程来加载图像吗?
谢谢!