您的位置:首页 > 其它

GridViewActivity gridview显示时的动画

2015-10-15 14:13 429 查看
package com.duguang.baseanimation.ui.listivew.listviews;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;

import com.duguang.baseanimation.R;
import com.duguang.baseanimation.ui.base.BaseActivity;
import com.haarman.listviewanimations.ArrayAdapter;
import com.haarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter;

public class GridViewActivity extends BaseActivity {

@Override
public void setView() {
setContentView(R.layout.activity_listviews_gridview);

GridView gridView = (GridView) findViewById(R.id.activity_gridview_gv);
AlphaInAnimationAdapter alphaInAnimationAdapter =
new AlphaInAnimationAdapter(new MyAdapter(this, getItems()));
alphaInAnimationAdapter.setAbsListView(gridView);
gridView.setAdapter(alphaInAnimationAdapter);

//    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public void initView() {
// TODO Auto-generated method stub

}

@Override
public void setListener() {
// TODO Auto-generated method stub

}

private ArrayList<Integer> getItems() {
ArrayList<Integer> items = new ArrayList<Integer>();
for (int i = 0; i < 100; i++) {
items.add(i);
}
return items;
}

private static class MyAdapter extends ArrayAdapter<Integer> {

private Context mContext;
private LruCache<Integer, Bitmap> mMemoryCache;

public MyAdapter(Context context, List<Integer> list) {
super(list);
mContext = context;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory;
mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
@Override
protected int sizeOf(Integer key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
};
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ImageView imageView = (ImageView) convertView;

if (imageView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}

int imageResId;
switch (getItem(position) % 5) {
case 0:
imageResId = R.drawable.img_nature1;
break;
case 1:
imageResId = R.drawable.img_nature2;
break;
case 2:
imageResId = R.drawable.img_nature3;
break;
case 3:
imageResId = R.drawable.img_nature4;
break;
default:
imageResId = R.drawable.img_nature5;
}

Bitmap bitmap = getBitmapFromMemCache(imageResId);
if (bitmap == null) {
bitmap = BitmapFactory.decodeResource(mContext.getResources(), imageResId);
addBitmapToMemoryCache(imageResId, bitmap);
}
imageView.setImageBitmap(bitmap);

return imageView;
}

private void addBitmapToMemoryCache(int key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}

private Bitmap getBitmapFromMemCache(int key) {
return mMemoryCache.get(key);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: