Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

* [android] fix wrong draw on first download iconfont #625

Merged
merged 1 commit into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
*/
package com.taobao.weex.ui.component;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Layout;
import android.view.ViewGroup;

Expand All @@ -29,6 +33,9 @@
import com.taobao.weex.dom.WXDomObject;
import com.taobao.weex.ui.ComponentCreator;
import com.taobao.weex.ui.view.WXTextView;
import com.taobao.weex.utils.FontDO;
import com.taobao.weex.utils.TypefaceUtil;
import com.taobao.weex.utils.WXLogUtils;

import java.lang.reflect.InvocationTargetException;

Expand All @@ -42,6 +49,8 @@ public class WXText extends WXComponent<WXTextView> {
* The default text size
**/
public static final int sDEFAULT_SIZE = 32;
private BroadcastReceiver mTypefaceObserver;
private String mFontFamily;

public static class Creator implements ComponentCreator {

Expand Down Expand Up @@ -102,12 +111,14 @@ protected boolean setProperty(String key, Object param) {
case Constants.Name.FONT_STYLE:
case Constants.Name.COLOR:
case Constants.Name.TEXT_DECORATION:
case Constants.Name.FONT_FAMILY:
case Constants.Name.TEXT_ALIGN:
case Constants.Name.TEXT_OVERFLOW:
case Constants.Name.LINE_HEIGHT:
case Constants.Name.VALUE:
return true;
case Constants.Name.FONT_FAMILY:
registerTypefaceObserver(param.toString());
return true;
default:
return super.setProperty(key, param);
}
Expand Down Expand Up @@ -145,4 +156,43 @@ protected Object convertEmptyProperty(String propName, Object originalValue) {
}
return super.convertEmptyProperty(propName, originalValue);
}

@Override
public void destroy() {
super.destroy();
if (getContext() != null && mTypefaceObserver != null) {
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mTypefaceObserver);
}
}

private void registerTypefaceObserver(String desiredFontFamily) {
if (getContext() == null) {
WXLogUtils.w("WXText", "Content is null on register typeface observer");
}
mFontFamily = desiredFontFamily;
mTypefaceObserver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String fontFamily = intent.getStringExtra("fontFamily");
if (!mFontFamily.equals(fontFamily)) {
return;
}

FontDO fontDO = TypefaceUtil.getFontDO(fontFamily);
if (fontDO != null && fontDO.getTypeface() != null) {
Layout layout = getHostView().getTextLayout();
if (layout != null) {
layout.getPaint().setTypeface(fontDO.getTypeface());
WXLogUtils.d("WXText", "Apply font family " + fontFamily + " to paint");
} else {
WXLogUtils.w("WXText", "Layout not created");
}
getHostView().invalidate();
}
WXLogUtils.d("WXText", "Font family " + fontFamily + " is available");
}
};

LocalBroadcastManager.getInstance(getContext()).registerReceiver(mTypefaceObserver, new IntentFilter(TypefaceUtil.ACTION_TYPE_FACE_AVAILABLE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
*/
package com.taobao.weex.utils;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.net.Uri;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;

import com.taobao.weex.WXEnvironment;
Expand All @@ -43,6 +45,8 @@ public class TypefaceUtil {
private final static String TAG = "TypefaceUtil";
private final static Map<String, FontDO> sCacheMap = new HashMap<>(); //Key: fontFamilyName

public static final String ACTION_TYPE_FACE_AVAILABLE = "type_face_available";

public static void putFontDO(FontDO fontDO) {
if (fontDO != null && !TextUtils.isEmpty(fontDO.getFontFamilyName())) {
sCacheMap.put(fontDO.getFontFamilyName(), fontDO);
Expand Down Expand Up @@ -223,6 +227,10 @@ private static boolean loadLocalFontFile(String path, String fontFamily) {
if(WXEnvironment.isApkDebugable()) {
WXLogUtils.d(TAG, "load local font file success");
}

Intent intent = new Intent(ACTION_TYPE_FACE_AVAILABLE);
intent.putExtra("fontFamily", fontFamily);
LocalBroadcastManager.getInstance(WXEnvironment.getApplication()).sendBroadcast(intent);
return true;
}
} else {
Expand Down