mirror of https://github.com/tasks/tasks
Caching images in AsyncImageView
parent
599e7b7561
commit
2a01d8460d
Binary file not shown.
|
Before Width: | Height: | Size: 282 B After Width: | Height: | Size: 508 B |
Binary file not shown.
|
Before Width: | Height: | Size: 8.5 KiB After Width: | Height: | Size: 5.8 KiB |
@ -0,0 +1,127 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- See the file "LICENSE" for the full license governing this code. -->
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:astrid="http://schemas.android.com/apk/res/com.timsu.astrid"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:minHeight="57dip"
|
||||||
|
android:paddingTop="2dip"
|
||||||
|
android:paddingBottom="2dip"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:id="@+id/importance_legacy"
|
||||||
|
android:layout_width="4dip"
|
||||||
|
android:layout_height="fill_parent"
|
||||||
|
android:visibility="gone" />
|
||||||
|
|
||||||
|
<!-- indent -->
|
||||||
|
<View android:id="@+id/indent"
|
||||||
|
android:layout_width="0dip"
|
||||||
|
android:layout_height="fill_parent"/>
|
||||||
|
|
||||||
|
<!-- grabber -->
|
||||||
|
<ImageView android:id="@+id/grabber"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="fill_parent"
|
||||||
|
android:scaleType="center"
|
||||||
|
android:src="@drawable/grabber"/>
|
||||||
|
|
||||||
|
<RelativeLayout android:id="@+id/rowBody"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="fill_parent"
|
||||||
|
android:background="@android:drawable/list_selector_background"
|
||||||
|
android:layout_marginTop="6dip"
|
||||||
|
android:layout_marginBottom="6dip"
|
||||||
|
android:paddingLeft="5dip">
|
||||||
|
|
||||||
|
|
||||||
|
<com.todoroo.astrid.ui.CheckableImageView
|
||||||
|
android:id="@+id/completeBox"
|
||||||
|
android:layout_width="45dip"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:minHeight="30dip"
|
||||||
|
android:scaleType="center"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:layout_marginLeft="2dip"
|
||||||
|
android:button="@drawable/btn_check" />
|
||||||
|
<!-- assignee photo -->
|
||||||
|
|
||||||
|
<com.todoroo.astrid.helper.AsyncImageView
|
||||||
|
android:id="@+id/picture"
|
||||||
|
android:layout_width="35dip"
|
||||||
|
android:layout_height="35dip"
|
||||||
|
android:layout_marginLeft="10dip"
|
||||||
|
astrid:defaultSrc="@drawable/icn_default_person_image"
|
||||||
|
android:scaleType="fitCenter"
|
||||||
|
android:visibility="gone" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/task_row"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_toRightOf="@id/completeBox"
|
||||||
|
android:layout_centerVertical="true"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:paddingLeft="5dip" >
|
||||||
|
|
||||||
|
<!-- task name -->
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/title"
|
||||||
|
style="@style/TextAppearance.TAd_ItemTitle"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="100"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:maxLines="2" />
|
||||||
|
|
||||||
|
<!-- due date -->
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/taskActionContainer"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:minHeight="40dip"
|
||||||
|
android:layout_marginRight="4dip"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:gravity="right|center_vertical">
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/dueDate"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentRight="true"
|
||||||
|
android:layout_marginRight="4dip"
|
||||||
|
android:gravity="center_vertical|right" />
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/taskActionIcon"
|
||||||
|
android:layout_width="20dip"
|
||||||
|
android:layout_height="20dip"
|
||||||
|
android:scaleType="fitCenter"
|
||||||
|
android:visibility="gone"/>
|
||||||
|
</LinearLayout>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<!-- details line 1 -->
|
||||||
|
<TextView android:id="@+id/details1"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_below="@id/task_row"
|
||||||
|
android:layout_alignParentLeft="true"
|
||||||
|
android:layout_marginTop="-1dip"
|
||||||
|
style="@style/TextAppearance.TAd_ItemDetails"
|
||||||
|
android:visibility="gone" />
|
||||||
|
|
||||||
|
<!-- details line 2 -->
|
||||||
|
<TextView android:id="@+id/details2"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_below="@id/details1"
|
||||||
|
android:layout_alignParentLeft="true"
|
||||||
|
style="@style/TextAppearance.TAd_ItemDetails"
|
||||||
|
android:visibility="gone" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@ -0,0 +1,67 @@
|
|||||||
|
package com.todoroo.astrid.helper;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Subclass of greendroid.widget.AsyncImageView, so that we can cache the image locally when user is offline
|
||||||
|
* IMPORTANT: cannot load a cached image by setting the url in an xml file. --ImageDiskCache object is created after object is loaded from xml
|
||||||
|
*/
|
||||||
|
public class AsyncImageView extends greendroid.widget.AsyncImageView {
|
||||||
|
|
||||||
|
private final ImageDiskCache imageDiskCache;
|
||||||
|
private Bitmap cacheImage;
|
||||||
|
private String cacheURL = ""; //$NON-NLS-1$
|
||||||
|
public AsyncImageView(Context context) {
|
||||||
|
super(context);
|
||||||
|
|
||||||
|
imageDiskCache = ImageDiskCache.getInstance();
|
||||||
|
}
|
||||||
|
public AsyncImageView(Context context, AttributeSet set) {
|
||||||
|
super(context, set);
|
||||||
|
imageDiskCache = ImageDiskCache.getInstance();
|
||||||
|
}
|
||||||
|
public AsyncImageView(Context context, AttributeSet set, int defStyle) {
|
||||||
|
super(context, set, defStyle);
|
||||||
|
imageDiskCache = ImageDiskCache.getInstance();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void setUrl(String url) {
|
||||||
|
if (cacheImage != null && cacheURL.equals(url) && !TextUtils.isEmpty(url)) {
|
||||||
|
setImageBitmap(cacheImage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if(imageDiskCache != null && imageDiskCache.contains(url)) {
|
||||||
|
try {
|
||||||
|
cacheImage = imageDiskCache.get(url);
|
||||||
|
setImageBitmap(cacheImage);
|
||||||
|
cacheURL = url;
|
||||||
|
return;
|
||||||
|
} catch (IOException e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.setUrl(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Bitmap getImageBitmap() {
|
||||||
|
setDrawingCacheEnabled(true);
|
||||||
|
|
||||||
|
// this is the important code :)
|
||||||
|
// Without it the view will have a dimension of 0,0 and the bitmap will be null
|
||||||
|
measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
|
||||||
|
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
|
||||||
|
layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
|
||||||
|
|
||||||
|
buildDrawingCache(true);
|
||||||
|
Bitmap b = Bitmap.createBitmap(getDrawingCache());
|
||||||
|
setDrawingCacheEnabled(false); // clear drawing cache
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue