Skip to content

Commit

Permalink
use cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglei committed Apr 4, 2014
1 parent c9d82a5 commit ab43fda
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 18 deletions.
Binary file added bin/XGifView.apk
Binary file not shown.
Binary file added bin/classes.dex
Binary file not shown.
Binary file modified bin/classes/com/tracyzhang/xgif/MainActivity.class
Binary file not shown.
Binary file modified bin/classes/com/tracyzhang/xgif/adapter/ChatAdapter$1.class
Binary file not shown.
Binary file not shown.
Binary file modified bin/classes/com/tracyzhang/xgif/adapter/ChatAdapter.class
Binary file not shown.
Binary file not shown.
Binary file added bin/res/crunch/drawable-hdpi/chat_bg.9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/res/crunch/drawable-hdpi/default_head.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/res/crunch/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/res/crunch/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/res/crunch/drawable-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/res/crunch/drawable-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/resources.ap_
Binary file not shown.
6 changes: 6 additions & 0 deletions src/com/tracyzhang/xgif/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ protected void onCreate(Bundle savedInstanceState) {
msgs.add("You can download the demo from https://github.com/TracyZhangLei");
msgs.add("[微笑][微笑][微笑]");
msgs.add("[咖啡][微笑][咖啡]");
msgs.add("[咖啡][微笑][咖啡]");
msgs.add("[咖啡][微笑][咖啡]");
msgs.add("[咖啡][微笑][咖啡]");
msgs.add("[咖啡][微笑][咖啡]");
msgs.add("[咖啡][微笑][咖啡]");
msgs.add("[咖啡][微笑][咖啡]");

msgs.add("中华人民共和国万岁![微笑]");
msgs.add("Long live the People's Republic of China![微笑]");
Expand Down
49 changes: 31 additions & 18 deletions src/com/tracyzhang/xgif/adapter/ChatAdapter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.tracyzhang.xgif.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -30,7 +32,7 @@ public class ChatAdapter extends BaseAdapter{

private Pattern EMOTION_URL = Pattern.compile("\\[(\\S+?)\\]");
private FaceManager fm;

private Map<String,SpannableString> cacheMap;//cache
private Context cxt;
private List<String> data;

Expand All @@ -39,6 +41,7 @@ public ChatAdapter(Context cxt, List<String> data) {
this.cxt = cxt;
this.data = data;
fm = FaceManager.getInstance();
cacheMap = new HashMap<String,SpannableString>();
}

@Override
Expand Down Expand Up @@ -77,26 +80,36 @@ class ViewHolder{
TextView msg;
}

private CharSequence convertNormalStringToSpannableString(String message , final TextView tv) {
SpannableString value = SpannableString.valueOf(message);
Matcher localMatcher = EMOTION_URL.matcher(value);
while (localMatcher.find()) {
String str2 = localMatcher.group(0);
int k = localMatcher.start();
int m = localMatcher.end();
if (m - k < 8) {
int face = fm.getFaceId(str2);
if(-1!=face){
AnimatedImageSpan localImageSpan = new AnimatedImageSpan(new AnimatedGifDrawable(cxt.getResources().openRawResource(face), new AnimatedGifDrawable.UpdateListener() {
@Override
public void update() {//update the textview
tv.postInvalidate();
}
}));
value.setSpan(localImageSpan, k, m, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
/**
* check the cacke first
* @param message
* @param tv
* @return
*/
private SpannableString convertNormalStringToSpannableString(String message , final TextView tv) {
SpannableString value = cacheMap.get(message);
if(null == value){
value = SpannableString.valueOf(message);
Matcher localMatcher = EMOTION_URL.matcher(value);
while (localMatcher.find()) {
String str2 = localMatcher.group(0);
int k = localMatcher.start();
int m = localMatcher.end();
if (m - k < 8) {
int face = fm.getFaceId(str2);
if(-1!=face){
AnimatedImageSpan localImageSpan = new AnimatedImageSpan(new AnimatedGifDrawable(cxt.getResources().openRawResource(face), new AnimatedGifDrawable.UpdateListener() {
@Override
public void update() {//update the textview
tv.postInvalidate();
}
}));
value.setSpan(localImageSpan, k, m, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
}
}
}
return value;
}

}

0 comments on commit ab43fda

Please sign in to comment.