Skip to content

Commit

Permalink
add HttpCacheDemo.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinea committed Nov 18, 2013
1 parent 9bdd28b commit a79f58f
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 3 deletions.
6 changes: 6 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
android:label="@string/imagecache_demo_activity_title"
android:theme="@android:style/Theme.Holo.Light" >
</activity>
<activity
android:name=".HttpCacheDemo"
android:configChanges="orientation|keyboardHidden"
android:label="@string/imagecache_demo_activity_title"
android:theme="@android:style/Theme.Holo.Light" >
</activity>
<activity
android:name=".ImageCacheDemo"
android:configChanges="orientation|keyboardHidden"
Expand Down
9 changes: 6 additions & 3 deletions src/cn/trinea/android/demo/DemoList.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class DemoList extends BaseActivity {

public static final String TAG = "DemoList";

private static final String[] mStrings = { "ImageSDCardCache Demo", "ImageCache Demo", "DropDownListView Demo",
"onBottom onTop ScrollView Demo", "DownloadManager Demo", "SearchView Demo",
private static final String[] mStrings = { "HttpCache Demo", "ImageSDCardCache Demo", "ImageCache Demo",
"DropDownListView Demo", "onBottom onTop ScrollView Demo", "DownloadManager Demo", "SearchView Demo",
"ViewPager Multi Fragment Demo", "Slide One Page Gallery Demo", "ViewPager Demo", "Service Demo",
"BroadcastReceiver Demo" };

Expand All @@ -45,7 +45,10 @@ public void onCreate(Bundle savedInstanceState) {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == total - 10) {
if (position == total - 11) {
Intent intent = new Intent(DemoList.this, HttpCacheDemo.class);
startActivity(intent);
} else if (position == total - 10) {
Intent intent = new Intent(DemoList.this, ImageSDCardCacheDemo.class);
startActivity(intent);
} else if (position == total - 9) {
Expand Down
148 changes: 148 additions & 0 deletions src/cn/trinea/android/demo/HttpCacheDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package cn.trinea.android.demo;

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

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import cn.trinea.android.common.service.impl.ImageCache;
import cn.trinea.android.common.service.impl.ImageMemoryCache.OnImageCallbackListener;
import cn.trinea.android.common.service.impl.RemoveTypeLastUsedTimeFirst;

/**
* HttpCacheDemo
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-11-18
*/
public class HttpCacheDemo extends BaseActivity {

/** column number **/
public static final int COLUMNS = 2;
/** imageView default height **/
public static final int IMAGEVIEW_DEFAULT_HEIGHT = 400;
public static final String TAG_CACHE = "image_cache";

private RelativeLayout parentLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState, R.layout.image_cache_demo);

Context context = getApplicationContext();
parentLayout = (RelativeLayout)findViewById(R.id.image_cache_parent_layout);
initImageUrlList();
IMAGE_CACHE.initData(this, TAG_CACHE);
IMAGE_CACHE.setContext(context);

int count = 0, viewId = 0x7F24FFF0;
int verticalSpacing, horizontalSpacing;
verticalSpacing = horizontalSpacing = getResources().getDimensionPixelSize(R.dimen.dp_4);
Display display = getWindowManager().getDefaultDisplay();
int imageWidth = (display.getWidth() - (COLUMNS + 1) * horizontalSpacing) / COLUMNS;
for (String imageUrl : imageUrlList) {
ImageView imageView = new ImageView(context);
imageView.setId(++viewId);
imageView.setScaleType(ScaleType.CENTER);
imageView.setBackgroundResource(R.drawable.image_border);
parentLayout.addView(imageView);

// set imageView layout params
LayoutParams layoutParams = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
layoutParams.width = imageWidth;
layoutParams.topMargin = verticalSpacing;
layoutParams.rightMargin = horizontalSpacing;
int column = count % COLUMNS;
int row = count / COLUMNS;
if (row > 0) {
layoutParams.addRule(RelativeLayout.BELOW, viewId - COLUMNS);
}
if (column > 0) {
layoutParams.addRule(RelativeLayout.RIGHT_OF, viewId - 1);
}

// get image
if (!IMAGE_CACHE.get(imageUrl, imageView)) {
imageView.setImageResource(R.drawable.trinea);
layoutParams.height = IMAGEVIEW_DEFAULT_HEIGHT;
}
count++;
}
}

@Override
protected void onDestroy() {
IMAGE_CACHE.saveDataToDb(this, TAG_CACHE);
super.onDestroy();
}

/** icon cache **/
public static final ImageCache IMAGE_CACHE = new ImageCache(128, 512);

static {
/** init icon cache **/
OnImageCallbackListener imageCallBack = new OnImageCallbackListener() {

private static final long serialVersionUID = 1L;

@Override
public void onImageLoaded(String imageUrl, Drawable imageDrawable, View view, boolean isInCache) {
if (view != null && imageDrawable != null) {
ImageView imageView = (ImageView)view;
imageView.setImageDrawable(imageDrawable);
// first time show with animation
if (!isInCache) {
imageView.startAnimation(getInAlphaAnimation(2000));
}

// auto set height accroding to rate between height and weight
LayoutParams imageParams = (LayoutParams)imageView.getLayoutParams();
imageParams.height = imageParams.width * imageDrawable.getIntrinsicHeight()
/ imageDrawable.getIntrinsicWidth();
imageView.setScaleType(ScaleType.FIT_XY);
}
}
};
IMAGE_CACHE.setOnImageCallbackListener(imageCallBack);
IMAGE_CACHE.setCacheFullRemoveType(new RemoveTypeLastUsedTimeFirst<Drawable>());

IMAGE_CACHE.setHttpReadTimeOut(10000);
IMAGE_CACHE.setOpenWaitingQueue(true);
IMAGE_CACHE.setValidTime(-1);
}

public static AlphaAnimation getInAlphaAnimation(long durationMillis) {
AlphaAnimation inAlphaAnimation = new AlphaAnimation(0, 1);
inAlphaAnimation.setDuration(durationMillis);
return inAlphaAnimation;
}

private List<String> imageUrlList;

private void initImageUrlList() {
imageUrlList = new ArrayList<String>();
imageUrlList.add("http://farm8.staticflickr.com/7403/9146300103_03423db0cc.jpg");
imageUrlList.add("http://farm4.staticflickr.com/3755/9148527824_6c156185ea.jpg");
imageUrlList.add("http://farm8.staticflickr.com/7409/9148527822_36fa37d7ca_z.jpg");
imageUrlList.add("http://farm8.staticflickr.com/7403/9146300103_03423db0cc.jpg");
imageUrlList.add("http://farm8.staticflickr.com/7318/9148527808_e804baef0b.jpg");
imageUrlList.add("http://farm3.staticflickr.com/2857/9148527928_3063544889.jpg");
imageUrlList.add("http://farm8.staticflickr.com/7318/9146300275_5fe995d123.jpg");
imageUrlList.add("http://farm8.staticflickr.com/7351/9148527976_8a4e75ae87.jpg");
imageUrlList.add("http://farm4.staticflickr.com/3679/9146300263_5c2191232a_o.jpg");
imageUrlList.add("http://farm3.staticflickr.com/2863/9148527892_31f9377351_o.jpg");
imageUrlList.add("http://farm3.staticflickr.com/2888/9148527996_f05118d7de_o.jpg");
imageUrlList.add("http://farm8.staticflickr.com/7310/9148528008_8e8f51997a.jpg");
imageUrlList.add("http://farm3.staticflickr.com/2849/9148528108_dfcda19507.jpg");
imageUrlList.add("http://farm4.staticflickr.com/3739/9148528022_e9bf03058f.jpg");
imageUrlList.add("http://farm4.staticflickr.com/3696/9146300409_dfa9d7c603.jpg");
imageUrlList.add("http://farm8.staticflickr.com/7288/9146300469_bd3420c75b_z.jpg");
}
}

0 comments on commit a79f58f

Please sign in to comment.