Skip to content

Commit

Permalink
add HttpCacheDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinea committed Nov 18, 2013
1 parent a79f58f commit bda7e51
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 119 deletions.
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.trinea.android.demo"
android:versionCode="35"
android:versionName="2.9.11" >
android:versionName="2.10.11" >

<uses-sdk android:minSdkVersion="14" />

Expand Down
46 changes: 46 additions & 0 deletions res/layout/http_cache_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
android:id="@+id/http_cache_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/http_cache_url_default"
android:inputType="textUri" />

<Button
android:id="@+id/http_cache_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/http_cache_url"
android:layout_marginTop="@dimen/dp_8"
android:text="@string/http_cache_get" />

<TextView
android:id="@+id/http_cache_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/http_cache_get"
android:paddingLeft="@dimen/dp_4"
android:textStyle="bold" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/http_cache_info"
android:layout_marginTop="@dimen/dp_4"
android:paddingBottom="@dimen/dp_40" >

<TextView
android:id="@+id/http_cache_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/dp_4" />
</ScrollView>

<include layout="@layout/trinea_info" />

</RelativeLayout>
1 change: 1 addition & 0 deletions res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<string name="profile">个人主页:&#160;</string>
<string name="description">更多介绍:&#160;</string>
<string name="http_cache_get">获取内容</string>

<string name="desc_search_view">SearchView介绍及搜索提示实现</string>
<string name="desc_view_pager_multi_page">viewpager实现画廊效果</string>
Expand Down
6 changes: 6 additions & 0 deletions res/values/configs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="http_cache_url_default">http://www.trinea.cn/test-for-http-cache.html</string>

</resources>
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

<string name="profile">Profile:&#160;</string>
<string name="description">Description:&#160;</string>
<string name="http_cache_get">Get content</string>

<string name="desc_search_view">SearchView Desc And Implement Search Tip</string>
<string name="desc_view_pager_multi_page">Viewpager With Multi Page</string>
Expand Down
168 changes: 50 additions & 118 deletions src/cn/trinea/android/demo/HttpCacheDemo.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package cn.trinea.android.demo;

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

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;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import cn.trinea.android.common.entity.HttpResponse;
import cn.trinea.android.common.service.HttpCache;
import cn.trinea.android.common.service.HttpCache.HttpCacheListener;
import cn.trinea.android.common.util.StringUtils;

/**
* HttpCacheDemo
Expand All @@ -24,125 +21,60 @@
*/
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";
public static final String TAG_CACHE = "http_cache";

private RelativeLayout parentLayout;
private EditText httpUrlET;
private Button httpGetBT;
private TextView httpGetContentTV;
private TextView httpCacheInfoTV;

private HttpCache httpCache;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState, R.layout.image_cache_demo);
super.onCreate(savedInstanceState, R.layout.http_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);
httpCache = new HttpCache(context);
httpUrlET = (EditText)findViewById(R.id.http_cache_url);
httpGetBT = (Button)findViewById(R.id.http_cache_get);
httpGetContentTV = (TextView)findViewById(R.id.http_cache_content);
httpCacheInfoTV = (TextView)findViewById(R.id.http_cache_info);
httpGetBT.setOnClickListener(new OnClickListener() {

// 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);
}
@Override
public void onClick(View v) {
String url = httpUrlET.getText().toString();
url = StringUtils.isEmpty(url) ? httpUrlET.getHint().toString() : url;
httpCache.httpGet(url, new HttpCacheListener() {

protected void onPreGet() {
httpCacheInfoTV.setText("");
httpGetContentTV.setText("wating…");
}

// get image
if (!IMAGE_CACHE.get(imageUrl, imageView)) {
imageView.setImageResource(R.drawable.trinea);
layoutParams.height = IMAGEVIEW_DEFAULT_HEIGHT;
protected void onPostGet(HttpResponse httpResponse, boolean isInCache) {
if (httpResponse != null) {
StringBuilder sb = new StringBuilder(256);
sb.append("is in cache: ").append(isInCache).append("\r\n");
if (isInCache) {
sb.append("expires: ").append(new Date(httpResponse.getExpiredTime()).toGMTString())
.append("\r\n");
}
httpCacheInfoTV.setText(sb.toString());
httpGetContentTV.setText(httpResponse.getResponseBody());
} else {
httpCacheInfoTV.setText("");
httpGetContentTV.setText("response is null.");
}
}
});
}
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 bda7e51

Please sign in to comment.