Update the usage of QQFaceManager

This commit is contained in:
cgspine
2019-09-20 12:41:42 +08:00
parent 8df44312d7
commit 142fb4bb9d
9 changed files with 133 additions and 104 deletions
@@ -0,0 +1,61 @@
/*
* Tencent is pleased to support the open source community by making QMUI_Android available.
*
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qmuiteam.qmui.qqface;
import android.graphics.drawable.Drawable;
public class QMUINoQQFaceManager implements IQMUIQQFaceManager{
@Override
public boolean maybeSoftBankEmoji(char c) {
return false;
}
@Override
public int getSoftbankEmojiResource(char c) {
return 0;
}
@Override
public boolean maybeEmoji(int codePoint) {
return false;
}
@Override
public int getEmojiResource(int codePoint) {
return 0;
}
@Override
public int getDoubleUnicodeEmoji(int currentCodePoint, int nextCodePoint) {
return 0;
}
@Override
public int getQQfaceResource(CharSequence text) {
return 0;
}
@Override
public Drawable getSpecialBoundsDrawable(CharSequence text) {
return null;
}
@Override
public int getSpecialDrawableMaxHeight() {
return 0;
}
}
@@ -23,11 +23,15 @@ import android.util.LruCache;
import com.qmuiteam.qmui.span.QMUITouchableSpan;
import com.qmuiteam.qmui.util.QMUILangHelper;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
/**
* {@link QMUIQQFaceView} 的内容解析器,将文本内容解析成 {@link QMUIQQFaceView} 想要的数据格式。
@@ -38,24 +42,31 @@ import java.util.List;
public class QMUIQQFaceCompiler {
private static final int SPAN_COLUMN = 2;
private static final Map<IQMUIQQFaceManager, QMUIQQFaceCompiler> sInstanceMap = new HashMap<>(4);
private static IQMUIQQFaceManager sDefaultQQFaceManager = new QMUINoQQFaceManager();
private volatile static QMUIQQFaceCompiler sInstance;
public static void setDefaultQQFaceManager(@NonNull IQMUIQQFaceManager defaultQQFaceManager) {
sDefaultQQFaceManager = defaultQQFaceManager;
}
// cache
private LruCache<CharSequence, ElementList> mCache;
private IQMUIQQFaceManager mQQFaceManager;
// 有多线程保护,如果内容非常多,可以扔到后台去(虽然暂时没有这个必要,但不避免有人想这么做)
@MainThread
public static QMUIQQFaceCompiler getDefaultInstance(){
return getInstance(sDefaultQQFaceManager);
}
@MainThread
public static QMUIQQFaceCompiler getInstance(IQMUIQQFaceManager manager) {
if (sInstance == null) {
synchronized (QMUIQQFaceCompiler.class) {
if (sInstance == null) {
sInstance = new QMUIQQFaceCompiler(manager);
}
}
QMUIQQFaceCompiler instance = sInstanceMap.get(manager);
if (instance != null) {
return instance;
}
return sInstance;
instance = new QMUIQQFaceCompiler(manager);
sInstanceMap.put(manager, instance);
return instance;
}
private QMUIQQFaceCompiler(IQMUIQQFaceManager manager) {
@@ -108,9 +119,9 @@ public class QMUIQQFaceCompiler {
public int compare(QMUITouchableSpan o1, QMUITouchableSpan o2) {
int start1 = spannable.getSpanStart(o1);
int start2 = spannable.getSpanStart(o2);
if(start1 > start2){
if (start1 > start2) {
return 1;
}else if(start1 == start2){
} else if (start1 == start2) {
return 0;
}
return -1;
@@ -98,7 +98,6 @@ public class QMUIQQFaceView extends View {
private int mMaxWidth = Integer.MAX_VALUE;
private PressCancelAction mPendingPressCancelAction = null;
private boolean mJumpHandleMeasureAndDraw = false;
private Runnable mDelayTextSetter = null;
private boolean mIncludePad = true;
private Typeface mTypeface = null;
private int mParagraphSpace = 0; // 段间距
@@ -145,12 +144,7 @@ public class QMUIQQFaceView extends View {
mSpecialDrawablePadding = array.getDimensionPixelSize(R.styleable.QMUIQQFaceView_qmui_special_drawable_padding, 0);
final String text = array.getString(R.styleable.QMUIQQFaceView_android_text);
if (!QMUILangHelper.isNullOrEmpty(text)) {
mDelayTextSetter = new Runnable() {
@Override
public void run() {
setText(text);
}
};
mOriginText = text;
}
mMoreActionText = array.getString(R.styleable.QMUIQQFaceView_qmui_more_action_text);
mMoreActionColor = array.getColor(R.styleable.QMUIQQFaceView_qmui_more_action_color, mTextColor);
@@ -165,6 +159,7 @@ public class QMUIQQFaceView extends View {
mSpanBgPaint = new Paint();
mSpanBgPaint.setAntiAlias(true);
mSpanBgPaint.setStyle(Paint.Style.FILL);
setCompiler(QMUIQQFaceCompiler.getDefaultInstance());
}
public void setOpenQQFace(boolean openQQFace) {
@@ -276,9 +271,9 @@ public class QMUIQQFaceView extends View {
}
public void setCompiler(QMUIQQFaceCompiler compiler) {
mCompiler = compiler;
if (mDelayTextSetter != null) {
mDelayTextSetter.run();
if(mCompiler != compiler){
mCompiler = compiler;
setText(mOriginText, false);
}
}
@@ -357,10 +352,10 @@ public class QMUIQQFaceView extends View {
}
}
public void setIncludeFontPadding(boolean includepad) {
if (mIncludePad != includepad) {
public void setIncludeFontPadding(boolean includePad) {
if (mIncludePad != includePad) {
needReCalculateFontHeight = true;
mIncludePad = includepad;
mIncludePad = includePad;
requestLayout();
invalidate();
}
@@ -462,11 +457,14 @@ public class QMUIQQFaceView extends View {
}
public void setText(CharSequence charSequence) {
mDelayTextSetter = null;
CharSequence oldText = mOriginText;
if (mOriginText != null && mOriginText.equals(charSequence)) {
setText(charSequence, true);
}
private void setText(CharSequence charSequence, boolean compareOldText) {
if(compareOldText && QMUILangHelper.objectEquals(charSequence, mOriginText)){
return;
}
mOriginText = charSequence;
setContentDescription(charSequence);
if (mOpenQQFace && mCompiler == null) {
@@ -474,11 +472,9 @@ public class QMUIQQFaceView extends View {
}
if (QMUILangHelper.isNullOrEmpty(mOriginText)) {
if (!QMUILangHelper.isNullOrEmpty(oldText)) {
mElementList = null;
requestLayout();
invalidate();
}
mElementList = null;
requestLayout();
invalidate();
return;
}
@@ -22,6 +22,7 @@ import android.content.Context;
import android.content.res.Resources;
import com.qmuiteam.qmui.arch.QMUISwipeBackActivityManager;
import com.qmuiteam.qmui.qqface.QMUIQQFaceCompiler;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmuidemo.manager.QDSkinManager;
import com.qmuiteam.qmuidemo.manager.QDUpgradeManager;
@@ -50,6 +51,7 @@ public class QDApplication extends Application {
QDUpgradeManager.getInstance(this).check();
QMUISwipeBackActivityManager.init(this);
QMUIQQFaceCompiler.setDefaultQQFaceManager(QDQQFaceManager.getInstance());
QDSkinManager.install(this);
}
@@ -44,7 +44,7 @@ import butterknife.ButterKnife;
* @date 2017-06-08
*/
@Widget(group = Group.Other, name = "性能观测")
@Widget(group = Group.Other, name = "性能观测[微笑]")
public class QDQQFacePerformanceTestFragment extends BaseFragment {
@BindView(R.id.topbar) QMUITopBarLayout mTopBar;
@BindView(R.id.tabSegment) QMUITabSegment mTabSegment;
@@ -19,6 +19,7 @@ package com.qmuiteam.qmuidemo.fragment.components.qqface;
import android.view.LayoutInflater;
import android.view.View;
import com.qmuiteam.qmui.qqface.QMUIQQFaceView;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmuidemo.manager.QDDataManager;
import com.qmuiteam.qmuidemo.base.BaseFragment;
@@ -37,19 +38,19 @@ import butterknife.ButterKnife;
@Widget(group = Group.Other, name = "QQ表情使用展示")
public class QDQQFaceUsageFragment extends BaseFragment {
@BindView(R.id.topbar) QMUITopBar mTopBar;
@BindView(R.id.qqface1) QDQQFaceView mQQFace1;
@BindView(R.id.qqface2) QDQQFaceView mQQFace2;
@BindView(R.id.qqface3) QDQQFaceView mQQFace3;
@BindView(R.id.qqface4) QDQQFaceView mQQFace4;
@BindView(R.id.qqface5) QDQQFaceView mQQFace5;
@BindView(R.id.qqface6) QDQQFaceView mQQFace6;
@BindView(R.id.qqface7) QDQQFaceView mQQFace7;
@BindView(R.id.qqface8) QDQQFaceView mQQFace8;
@BindView(R.id.qqface9) QDQQFaceView mQQFace9;
@BindView(R.id.qqface10) QDQQFaceView mQQFace10;
@BindView(R.id.qqface11) QDQQFaceView mQQFace11;
@BindView(R.id.qqface12) QDQQFaceView mQQFace12;
@BindView(R.id.qqface13) QDQQFaceView mQQFace13;
@BindView(R.id.qqface1) QMUIQQFaceView mQQFace1;
@BindView(R.id.qqface2) QMUIQQFaceView mQQFace2;
@BindView(R.id.qqface3) QMUIQQFaceView mQQFace3;
@BindView(R.id.qqface4) QMUIQQFaceView mQQFace4;
@BindView(R.id.qqface5) QMUIQQFaceView mQQFace5;
@BindView(R.id.qqface6) QMUIQQFaceView mQQFace6;
@BindView(R.id.qqface7) QMUIQQFaceView mQQFace7;
@BindView(R.id.qqface8) QMUIQQFaceView mQQFace8;
@BindView(R.id.qqface9) QMUIQQFaceView mQQFace9;
@BindView(R.id.qqface10) QMUIQQFaceView mQQFace10;
@BindView(R.id.qqface11) QMUIQQFaceView mQQFace11;
@BindView(R.id.qqface12) QMUIQQFaceView mQQFace12;
@BindView(R.id.qqface13) QMUIQQFaceView mQQFace13;
@Override
protected View onCreateView() {
@@ -1,40 +0,0 @@
/*
* Tencent is pleased to support the open source community by making QMUI_Android available.
*
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qmuiteam.qmuidemo.fragment.components.qqface;
import android.content.Context;
import android.util.AttributeSet;
import com.qmuiteam.qmui.qqface.QMUIQQFaceCompiler;
import com.qmuiteam.qmui.qqface.QMUIQQFaceView;
import com.qmuiteam.qmuidemo.QDQQFaceManager;
/**
* @author cginechen
* @date 2016-12-24
*/
public class QDQQFaceView extends QMUIQQFaceView {
public QDQQFaceView(Context context) {
this(context, null);
}
public QDQQFaceView(Context context, AttributeSet attrs) {
super(context, attrs);
setCompiler(QMUIQQFaceCompiler.getInstance(QDQQFaceManager.getInstance()));
}
}
@@ -44,8 +44,6 @@ public class QDQQFacePagerView extends QDQQFaceBasePagerView {
QMUIQQFaceView qmuiqqFaceView;
if (convertView == null || !(convertView instanceof QMUIQQFaceView)) {
qmuiqqFaceView = new QMUIQQFaceView(getContext());
qmuiqqFaceView.setCompiler(QMUIQQFaceCompiler.getInstance(
QDQQFaceManager.getInstance()));
int padding = QMUIDisplayHelper.dp2px(getContext(), 16);
ViewCompat.setBackground(qmuiqqFaceView, QMUIResHelper.getAttrDrawable(
getContext(), R.attr.qmui_s_list_item_bg_with_border_bottom));
@@ -41,7 +41,7 @@
<TextView
style="@style/QDCommonTitle"
android:text="单行表情,末尾省略"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -53,7 +53,7 @@
style="@style/QDCommonTitle"
android:layout_marginTop="20dp"
android:text="最多显示3行,末尾省略"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -65,7 +65,7 @@
style="@style/QDCommonTitle"
android:layout_marginTop="20dp"
android:text="单行,开端省略"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -78,7 +78,7 @@
android:layout_marginTop="20dp"
android:text="最多显示3行,开端省略"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -90,7 +90,7 @@
style="@style/QDCommonTitle"
android:layout_marginTop="20dp"
android:text="单行,中间省略"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -103,7 +103,7 @@
android:layout_marginTop="20dp"
android:text="最多显示3行,中间省略"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -116,7 +116,7 @@
android:layout_marginTop="20dp"
android:text="全是表情也能优雅的省略"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -124,7 +124,7 @@
android:singleLine="true"
android:ellipsize="start"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -133,7 +133,7 @@
android:layout_marginTop="10dp"
android:ellipsize="middle"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -142,7 +142,7 @@
android:singleLine="true"
android:ellipsize="end"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -151,7 +151,7 @@
android:maxLines="3"
android:ellipsize="start"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -160,7 +160,7 @@
android:layout_marginTop="10dp"
android:ellipsize="middle"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -169,7 +169,7 @@
android:layout_marginTop="10dp"
android:ellipsize="end"/>
<com.qmuiteam.qmuidemo.fragment.components.qqface.QDQQFaceView
<com.qmuiteam.qmui.qqface.QMUIQQFaceView
android:id="@+id/qqface13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"