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.
Java Source Code
/*
* Copyright 2014 Niek Haarman//www.java2s.com
*
* 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.googlecards;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.haarman.listviewanimations.R;
import com.haarman.listviewanimations.util.BitmapCache;
import com.nhaarman.listviewanimations.ArrayAdapter;
publicclass GoogleCardsAdapter extends ArrayAdapter<Integer> {
privatefinal Context mContext;
privatefinal BitmapCache mMemoryCache;
GoogleCardsAdapter(final Context context) {
mContext = context;
mMemoryCache = new BitmapCache();
}
@Override
public View getView(finalint position, final View convertView, final ViewGroup parent) {
ViewHolder viewHolder;
View view = convertView;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.activity_googlecards_card, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) view.findViewById(R.id.activity_googlecards_card_textview);
view.setTag(viewHolder);
viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textView.setText(mContext.getString(R.string.card_number, getItem(position) + 1));
setImageView(viewHolder, position);
return view;
}
privatevoid setImageView(final ViewHolder viewHolder, finalint position) {
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);
}
viewHolder.imageView.setImageBitmap(bitmap);
}
privatevoid addBitmapToMemoryCache(finalint key, final Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
private Bitmap getBitmapFromMemCache(finalint key) {
return mMemoryCache.get(key);
}
@SuppressWarnings({"PackageVisibleField", "InstanceVariableNamingConvention"})
privatestaticclass ViewHolder {
TextView textView;
ImageView imageView;
}
}