我创建了一个简单的图库,它显示了存储在res/drawable
文件夹中的图像,但是网格视图非常迟缓且缓慢。如何进一步优化网格视图,使运动平稳。
public class ImageGalleryActivity extends Activity {
String strCategoryName = null;
GridView gridView;
Utils utils;
private int columnWidth;
// private GridViewImageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
Intent intent;
intent = getIntent();
if (intent != null) {
strCategoryName = intent.getStringExtra("category");
}
gridView = (GridView) findViewById(R.id.gridView_cards);
utils = new Utils(this);
// Intializing Grid View
InitilizeGridLayout();
// gridView.setAdapter(new GridCustomAdapter(this));
// adapter = new GridViewImageAdapter(ImageGalleryActivity.this,
// defaultImages, columnWidth, this);
List<Item> items = new ArrayList<Item>();
if(strCategoryName.equals("test"))
{
items.add(new Item(R.drawable.i1));
items.add(new Item(R.drawable.i2));
items.add(new Item(R.drawable.i3));
items.add(new Item(R.drawable.i4));
items.add(new Item(R.drawable.i5));
items.add(new Item(R.drawable.i6));
items.add(new Item(R.drawable.i7));
items.add(new Item(R.drawable.i8));
items.add(new Item(R.drawable.i9));
items.add(new Item(R.drawable.i10));
items.add(new Item(R.drawable.i11));
items.add(new Item(R.drawable.i12));
items.add(new Item(R.drawable.i13));
items.add(new Item(R.drawable.i14));
items.add(new Item(R.drawable.i15));
items.add(new Item(R.drawable.i16));
items.add(new Item(R.drawable.i17));
items.add(new Item(R.drawable.i18));
items.add(new Item(R.drawable.i19));
items.add(new Item(R.drawable.i20));
items.add(new Item(R.drawable.i21));
items.add(new Item(R.drawable.i22));
items.add(new Item(R.drawable.i23));
items.add(new Item(R.drawable.i24));
items.add(new Item(R.drawable.i25));
}
else if(strCategoryName.equals("test")){
items.add(new Item(R.drawable.i1));
items.add(new Item(R.drawable.i2));
items.add(new Item(R.drawable.i3));
items.add(new Item(R.drawable.i4));
items.add(new Item(R.drawable.i5));
items.add(new Item(R.drawable.i6));
items.add(new Item(R.drawable.i7));
items.add(new Item(R.drawable.i8));
items.add(new Item(R.drawable.i9));
items.add(new Item(R.drawable.i10));
items.add(new Item(R.drawable.i11));
items.add(new Item(R.drawable.i12));
items.add(new Item(R.drawable.i13));
items.add(new Item(R.drawable.i14));
items.add(new Item(R.drawable.i15));
items.add(new Item(R.drawable.i16));
items.add(new Item(R.drawable.i17));
items.add(new Item(R.drawable.i18));
}else{
items.add(new Item(R.drawable.i1));
items.add(new Item(R.drawable.i2));
items.add(new Item(R.drawable.i3));
items.add(new Item(R.drawable.i4));
items.add(new Item(R.drawable.i5));
items.add(new Item(R.drawable.i6));
items.add(new Item(R.drawable.i7));
items.add(new Item(R.drawable.i8));
items.add(new Item(R.drawable.i9));
items.add(new Item(R.drawable.i10));
items.add(new Item(R.drawable.i11));
items.add(new Item(R.drawable.i12));
items.add(new Item(R.drawable.i13));
items.add(new Item(R.drawable.i14));
items.add(new Item(R.drawable.i15));
items.add(new Item(R.drawable.i16));
items.add(new Item(R.drawable.i17));
items.add(new Item(R.drawable.i18));
}
gridView.setAdapter(new MyAdapter(this, items, columnWidth, ImageGalleryActivity.this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.image_gallery, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void InitilizeGridLayout() {
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
AppConstant.GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding,
(int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);
}
}
class MyAdapter extends BaseAdapter {
private List<Item> items;
private Context context;
int columnWidth;
Activity _activity;
public MyAdapter(Context context, List<Item> items, int columnWidth, Activity activity) {
this.context = context;
this.items = items;
this.columnWidth = columnWidth;
this._activity = activity;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.frame_layout_square_image_view, null, false);
holder.picture = (ImageView) convertView.findViewById(R.id.picture);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.picture.setImageBitmap((decodeImage(
items.get(position).drawableId, columnWidth)));
holder.picture.setOnClickListener(new OnImageClickListener(items.get(position).drawableId));
return convertView;
}
class OnImageClickListener implements OnClickListener {
int _drawableId;
// constructor
public OnImageClickListener(int drawableId) {
this._drawableId = drawableId;
}
@Override
public void onClick(View v) {
// on selecting grid view image
// launch full screen activity
Intent i = new Intent(_activity, ImageEditor.class);
i.putExtra("drawableId", _drawableId);
_activity.startActivity(i);
}
}
class ViewHolder {
ImageView picture;
}
public Bitmap decodeImage(int resourceId, int imageSize) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeResource(context.getResources(), resourceId, o);
// The new size we want to scale to
final int REQUIRED_SIZE = imageSize; // you are free to modify size
// as your requirement
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE
&& o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeResource(context.getResources(),
resourceId, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
}
class Item {
final int drawableId;
Item(int drawableId) {
this.drawableId = drawableId;
}
}
答案 0 :(得分:2)
尝试gridView.setSmoothScrollbarEnabled(true);它将使用更精确的计算方法,该方法基于屏幕上可见项目的像素高度。 http://developer.android.com/reference/android/widget/GridView.html 希望这可能有所帮助。
答案 1 :(得分:0)
首先我认为这一行:
holder.picture.setOnClickListener(new OnImageClickListener(items.get(position).drawableId));
很重,你在每次getView调用时都会分配一个匿名类。如果你真的想确定你可以跟踪你的内存分配(http://developer.android.com/tools/debugging/ddms.html#alloc)。
您可以轻松完成的是只分配一次的侦听器成员:
private OnClickListener myImageClickListener = new OnImageClickListener();
然后在你的getView中
holder.picture.setOnClickListener(myImageClickListener);
拥有更多&#34;泛型&#34;你可以(在你的getView中)监听:
holder.picture = (ImageView) convertView.findViewById(R.id.picture);
holder.picture.setTag(items.get(position).drawableId);
然后:
@Override
public void onClick(View v) {
int drawableId = Integer.valueOf(v.getTag());
}
这可能已经缓解了痛苦:)
答案 2 :(得分:0)
我遇到了与你相同的问题,滚动体验非常不平滑。我同意Mate Krizanac - 基本上你想要做的是在一个不是主UI线程的线程上加载任何图像。因此,您可以使用Picasso或ImageLoader类,或者使用AsyncTask将图像加载到ViewHolder中。另一种优化方法是将图像缩小一点。网上有很多代码可以帮助你。希望这可以帮助。如果您需要实际代码来解释,请告诉我。