[英]android: showing 156 images using gridview inside viewpager
我想使用18頁的Viewpager顯示156個縮略圖,每個頁面有9個縮略圖。 我已經實現如下:
public void init()
{
PageCount = (int) Math.ceil(mThumbIds.length / PAGE_SIZE);
mLists = new ArrayList<GridView>();
for (int i = 0; i < PageCount; i++)
{
GridView gv = new GridView(this);
gv.setAdapter(new MyGridViewAdapter(this, mStrs, mThumbIds, i, icon_dimension));
gv.setGravity(Gravity.CENTER);
gv.setClickable(true);
gv.setFocusable(true);
gv.setNumColumns(NUMOFCOLUMN);
gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gv.setHorizontalSpacing(gridview_horizontal_padding);
gv.setVerticalSpacing(gridview_vertical_padding);
mLists.add(gv);
}
page_number.setText("1/"+PageCount);
}
public class MyGridViewAdapter extends BaseAdapter
{
private Animation zoom = null;
// view
int icon_width, icon_height;
Typeface tf;
private Context mContext;
private List<String> mLists;
public static final int PAGE_SIZE = 9; // showing how many items in 1 page
public MyGridViewAdapter(Context pContext, String[] pStrs, Integer[] image_ref, int page, int width)
{
this.mContext = pContext;
mLists = new ArrayList<String>();
int i = page * PAGE_SIZE;
int end = i + PAGE_SIZE;
while ((i < image_ref.length) && (i < end))
{
mLists.add(pStrs[i]);
i++;
}
icon_width = width;
icon_height = icon_width;
tf = Typeface.createFromAsset(mContext.getAssets(),"fonts/HomegirlKiddo.ttf");
}
@Override
public int getCount()
{
return mLists.size();
}
@Override
public Object getItem(int position)
{
return mLists.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menugrid, parent, false);
holder = new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id.imageicon);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
final int myNum = Integer.parseInt(mLists.get(position)) -1;
ImageView imageView = holder.img;
try
{
imageView.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(), mThumbIds[myNum], icon_width, icon_width));
}
catch (OutOfMemoryError e)
{
((Index_Gridview)mContext).custom_toast("System Out-of-Error! Restarting...");
((Index_Gridview)mContext).restart_app();
}
imageView.setOnClickListener(new OnImageClickListener(myNum));
return convertView;
}
private Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
private int calculateInSampleSize (BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
class ViewHolder
{
ImageView img;
}
// References to our images in res > drawable
public Integer[] mThumbIds = new Integer[]
{
R.drawable.t_aaa,
R.drawable.t_bbb,
....listing of 156 image references here
}
我已經嘗試通過'decodeSampledBitmapFromResource'導入縮略圖。 原始縮略圖各約為75KB。
它很容易遇到OOM。 如何增強代碼以減輕OOM錯誤?
謝謝!!
您可以嘗試使用RAM和磁盤緩存來重用您的位圖。
這是android支持v4中LRUCache的鏈接:
http://developer.android.com/reference/android/support/v4/util/LruCache.html
這是Jake Wharton對DiskLRUCache項目的鏈接
你必須自己做一些記憶管理 - 毫無疑問。 您可以回收屏幕外頁面並在必要時重新加載它們。 LRUCache非常好,但它會將所有內存用於圖像。
您的畫廊不是那么大,您可以考慮將其保存在本機內存中。 你需要30MB的hdpi 800x480屏幕到150MB的高清屏幕。 使用JNI和C調用,您可以向android詢問安裝在硬件中的整個內存(512 - 1GB)。 這樣的內存不是Java堆的一部分,因此您不必擔心應用程序中其他內容缺少內存。
當然,這不是完美的解決方案,因為占用的內存比系統提供的內存多,這將導致分配失敗。 你必須照顧它。
鏈接: http : //code.google.com/p/native-buffer/
NativeBuffer是一種緩存,它使用C分配的內存來存儲圖像數據。 它比重新加載圖像或在SD卡上緩存它們要快得多。
https://code.google.com/p/android-query/wiki/AsyncAPI?tm=6在一個示例中使用我知道https://play.google.com/store/apps/details?id=com.app .ive它可以處理任意數量的圖像....
我認為重新發明輪子並不好,希望它可以幫助你實現你想要的。
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.