Back to project page ListViewAnimations.
The source code is released under:
Apache License
If you think the Android project ListViewAnimations listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright 2014 Niek Haarman//from ww w .j a v a 2 s . c o m * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.haarman.listviewanimations.gridview; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.haarman.listviewanimations.R; import com.haarman.listviewanimations.util.BitmapCache; import com.nhaarman.listviewanimations.ArrayAdapter; public class GridViewAdapter extends ArrayAdapter<Integer> { private final Context mContext; private final BitmapCache mMemoryCache; public GridViewAdapter(final Context context) { mContext = context; mMemoryCache = new BitmapCache(); for (int i = 0; i < 1000; i++) { add(i); } } @Override public View getView(final int position, final View convertView, final ViewGroup parent) { 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(final int key, final Bitmap bitmap) { if (getBitmapFromMemCache(key) == null) { mMemoryCache.put(key, bitmap); } } private Bitmap getBitmapFromMemCache(final int key) { return mMemoryCache.get(key); } }