Skip to content

Commit

Permalink
modfiy ImageCache Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinea committed Feb 20, 2014
1 parent 01d7915 commit 91ed3f0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 51 deletions.
4 changes: 2 additions & 2 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.trinea.android.demo"
android:versionCode="41"
android:versionName="3.0.1" >
android:versionCode="42"
android:versionName="3.0.2" >

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

Expand Down
38 changes: 21 additions & 17 deletions src/cn/trinea/android/demo/ImageCacheDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
Expand All @@ -18,10 +18,8 @@
import android.widget.RelativeLayout.LayoutParams;
import cn.trinea.android.common.entity.FailedReason;
import cn.trinea.android.common.service.impl.ImageCache;
import cn.trinea.android.common.service.impl.ImageCache.CompressListener;
import cn.trinea.android.common.service.impl.ImageMemoryCache.OnImageCallbackListener;
import cn.trinea.android.common.service.impl.RemoveTypeLastUsedTimeFirst;
import cn.trinea.android.common.util.FileUtils;

/**
* ImageCacheDemo
Expand All @@ -36,11 +34,13 @@ public class ImageCacheDemo extends BaseActivity {
public static final int IMAGEVIEW_DEFAULT_HEIGHT = 400;
public static final String TAG_CACHE = "image_cache";
/** cache folder path which be used when saving images **/
public static final String DEFAULT_CACHE_FOLDER = new StringBuilder()
.append(Environment.getExternalStorageDirectory().getAbsolutePath())
.append(File.separator).append("Trinea").append(File.separator)
.append("AndroidDemo").append(File.separator)
.append("ImageCache").toString();
public static final String DEFAULT_CACHE_FOLDER = new StringBuilder().append(Environment.getExternalStorageDirectory()
.getAbsolutePath())
.append(File.separator).append("Trinea")
.append(File.separator)
.append("AndroidDemo")
.append(File.separator)
.append("ImageCache").toString();
private RelativeLayout parentLayout;

@Override
Expand Down Expand Up @@ -121,24 +121,23 @@ protected void onDestroy() {
* callback function after get image successfully, run on ui thread
*
* @param imageUrl imageUrl
* @param imageDrawable drawable
* @param loadedImage bitmap
* @param view view need the image
* @param isInCache whether already in cache or got realtime
*/
@Override
public void onGetSuccess(String imageUrl, Drawable imageDrawable, View view, boolean isInCache) {
if (view != null && imageDrawable != null) {
public void onGetSuccess(String imageUrl, Bitmap loadedImage, View view, boolean isInCache) {
if (view != null && loadedImage != null) {
ImageView imageView = (ImageView)view;
imageView.setImageDrawable(imageDrawable);
imageView.setImageBitmap(loadedImage);
// 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();
imageParams.height = imageParams.width * loadedImage.getHeight() / loadedImage.getWidth();
imageView.setScaleType(ScaleType.FIT_XY);
}
}
Expand All @@ -158,12 +157,12 @@ public void onPreGet(String imageUrl, View view) {
* callback function after get image failed, run on ui thread
*
* @param imageUrl imageUrl
* @param imageDrawable drawable
* @param loadedImage bitmap
* @param view view need the image
* @param failedReason failed reason for get image
*/
@Override
public void onGetFailed(String imageUrl, Drawable imageDrawable, View view, FailedReason failedReason) {
public void onGetFailed(String imageUrl, Bitmap loadedImage, View view, FailedReason failedReason) {
Log.e(TAG_CACHE,
new StringBuilder(128).append("get image ").append(imageUrl).append(" error, failed type is: ")
.append(failedReason.getFailedType()).append(", failed reason is: ")
Expand All @@ -178,11 +177,16 @@ public void onGetNotInCache(String imageUrl, View view) {
}
};
IMAGE_CACHE.setOnImageCallbackListener(imageCallBack);
IMAGE_CACHE.setCacheFullRemoveType(new RemoveTypeLastUsedTimeFirst<Drawable>());
IMAGE_CACHE.setCacheFullRemoveType(new RemoveTypeLastUsedTimeFirst<Bitmap>());

IMAGE_CACHE.setHttpReadTimeOut(10000);
IMAGE_CACHE.setOpenWaitingQueue(true);
IMAGE_CACHE.setValidTime(-1);
/**
* close connection, default is connect keep-alive to reuse connection. if image is from different server, you
* can set this
*/
// IMAGE_CACHE.setRequestProperty("Connection", "false");
}

public static AlphaAnimation getInAlphaAnimation(long durationMillis) {
Expand Down
20 changes: 13 additions & 7 deletions src/cn/trinea/android/demo/ImageSDCardCacheDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
*/
public class ImageSDCardCacheDemo extends BaseActivity {

public static final String TAG_CACHE = "image_sdcard_cache";
public static final String TAG_CACHE = "image_sdcard_cache";
/** cache folder path which be used when saving images **/
public static final String DEFAULT_CACHE_FOLDER = new StringBuilder()
.append(Environment.getExternalStorageDirectory().getAbsolutePath())
.append(File.separator).append("Trinea").append(File.separator)
.append("AndroidDemo").append(File.separator)
.append("ImageSDCardCache").toString();

public static final String DEFAULT_CACHE_FOLDER = new StringBuilder().append(Environment.getExternalStorageDirectory()
.getAbsolutePath())
.append(File.separator).append("Trinea")
.append(File.separator).append("AndroidDemo")
.append(File.separator)
.append("ImageSDCardCache").toString();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState, R.layout.image_sdcard_cache_demo);
Expand Down Expand Up @@ -142,6 +143,11 @@ public void onGetFailed(String imageUrl, String imagePath, View view, FailedReas
IMAGE_SD_CACHE.setHttpReadTimeOut(10000);
IMAGE_SD_CACHE.setOpenWaitingQueue(true);
IMAGE_SD_CACHE.setValidTime(-1);
/**
* close connection, default is connect keep-alive to reuse connection. if image is from different server, you
* can set this
*/
// IMAGE_SD_CACHE.setRequestProperty("Connection", "false");
}

public static AlphaAnimation getInAlphaAnimation(long durationMillis) {
Expand Down
43 changes: 18 additions & 25 deletions src/cn/trinea/android/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MainActivity extends BaseActivity {

public static final String TAG = "DemoList";

private static final String[] mStrings = { "HttpCache Demo", "ImageSDCardCache Demo", "ImageCache Demo",
private static final String[] mStrings = { "HttpCache Demo", "ImageCache Demo", "ImageSDCardCache 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 @@ -42,43 +42,36 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == total - 11) {
Intent intent = new Intent(MainActivity.this, HttpCacheDemo.class);
startActivity(intent);
startActivity(HttpCacheDemo.class);
} else if (position == total - 10) {
Intent intent = new Intent(MainActivity.this, ImageSDCardCacheDemo.class);
startActivity(intent);
startActivity(ImageCacheDemo.class);
} else if (position == total - 9) {
Intent intent = new Intent(MainActivity.this, ImageCacheDemo.class);
startActivity(intent);
startActivity(ImageSDCardCacheDemo.class);
} else if (position == total - 8) {
Intent intent = new Intent(MainActivity.this, DropDownListViewDemo.class);
startActivity(intent);
startActivity(DropDownListViewDemo.class);
} else if (position == total - 7) {
Intent intent = new Intent(MainActivity.this, BorderScrollViewDemo.class);
startActivity(intent);
startActivity(BorderScrollViewDemo.class);
} else if (position == total - 6) {
Intent intent = new Intent(MainActivity.this, DownloadManagerDemo.class);
startActivity(intent);
startActivity(DownloadManagerDemo.class);
} else if (position == total - 5) {
Intent intent = new Intent(MainActivity.this, SearchViewDemo.class);
startActivity(intent);
startActivity(SearchViewDemo.class);
} else if (position == total - 4) {
Intent intent = new Intent(MainActivity.this, ViewPagerMulTiFragmentDemo.class);
startActivity(intent);
startActivity(ViewPagerMulTiFragmentDemo.class);
} else if (position == total - 3) {
Intent intent = new Intent(MainActivity.this, SlideOnePageGalleryDemo.class);
startActivity(intent);
startActivity(SlideOnePageGalleryDemo.class);
} else if (position == total - 2) {
Intent intent = new Intent(MainActivity.this, ViewPagerDemo.class);
startActivity(intent);
startActivity(ViewPagerDemo.class);
} else if (position == total - 1) {
Intent intent = new Intent(MainActivity.this, ServiceDemo.class);
startActivity(intent);
startActivity(ServiceDemo.class);
} else if (position == total) {
Intent intent = new Intent(MainActivity.this, BroadcastReceiverDemo.class);
startActivity(intent);
startActivity(BroadcastReceiverDemo.class);
}
}
});
}

private void startActivity(Class<?> cls) {
Intent intent = new Intent(MainActivity.this, cls);
startActivity(intent);
}
}

0 comments on commit 91ed3f0

Please sign in to comment.