Java tutorial
/* * Copyright 2013 "" daiv * * 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.daiv.android.twitter.adapters; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Adapter; import com.daiv.android.twitter.R; import com.daiv.android.twitter.settings.AppSettings; import com.daiv.android.twitter.utils.ImageUtils; import org.lucasr.smoothie.SimpleItemLoader; import java.io.BufferedInputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import twitter4j.Status; import uk.co.senab.bitmapcache.BitmapLruCache; import uk.co.senab.bitmapcache.CacheableBitmapDrawable; public class ArrayListLoader extends SimpleItemLoader<String, CacheableBitmapDrawable> { final BitmapLruCache mCache; private Context context; private boolean circleImages; public ArrayListLoader(BitmapLruCache cache, Context context) { mCache = cache; this.context = context; AppSettings settings = AppSettings.getInstance(context); // if the layout is Test's, then they should have circle images circleImages = settings.roundContactImages; } @Override public CacheableBitmapDrawable loadItemFromMemory(String url) { return mCache.getFromMemoryCache(url); } @Override public String getItemParams(Adapter adapter, int position) { try { Status status = (Status) adapter.getItem(position); String url; if (!status.isRetweet()) { url = status.getUser().getBiggerProfileImageURL(); } else { url = status.getRetweetedStatus().getUser().getBiggerProfileImageURL(); } return url; } catch (Exception e) { // no items... Log.v("getting_url", "no url found"); return ""; } } @Override public CacheableBitmapDrawable loadItem(String url) { CacheableBitmapDrawable wrapper = mCache.get(url); if (wrapper == null) { try { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); InputStream is = new BufferedInputStream(conn.getInputStream()); Bitmap image = decodeSampledBitmapFromResourceMemOpt(is, 500, 500); try { is.close(); } catch (Exception e) { } try { conn.disconnect(); } catch (Exception e) { } if (circleImages) { image = ImageUtils.getCircle(image, context); } wrapper = mCache.put(url, image); } catch (Exception e) { } } return wrapper; } @Override public void displayItem(View itemView, CacheableBitmapDrawable result, boolean fromMemory) { try { final TimelineArrayAdapter.ViewHolder holder = (TimelineArrayAdapter.ViewHolder) itemView.getTag(); if (result == null) { return; } holder.profilePic.setImageDrawable(result); } catch (Exception e) { // happens sometimes when searching for users i guess } } public Bitmap decodeSampledBitmapFromResourceMemOpt(InputStream inputStream, int reqWidth, int reqHeight) { byte[] byteArr = new byte[0]; byte[] buffer = new byte[1024]; int len; int count = 0; try { while ((len = inputStream.read(buffer)) > -1) { if (len != 0) { if (count + len > byteArr.length) { byte[] newbuf = new byte[(count + len) * 2]; System.arraycopy(byteArr, 0, newbuf, 0, count); byteArr = newbuf; } System.arraycopy(buffer, 0, byteArr, count, len); count += len; } } final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(byteArr, 0, count, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inPurgeable = true; options.inInputShareable = true; options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; return BitmapFactory.decodeByteArray(byteArr, 0, count, options); } catch (Exception e) { e.printStackTrace(); return null; } } public static int calculateInSampleSize(BitmapFactory.Options opt, int reqWidth, int reqHeight) { // Raw height and width of image final int height = opt.outHeight; final int width = opt.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } }