Skip to content

Commit

Permalink
fix conflicts in ExTextView
Browse files Browse the repository at this point in the history
  • Loading branch information
avenwu committed Jan 19, 2016
2 parents c7cdf50 + 614a0b6 commit f570a2b
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;
import android.test.ApplicationTestCase;
import android.util.Log;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
Expand All @@ -10,4 +11,40 @@ public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}

final static char ASSIC_START = '!';
final static char ASSIC_END = '~';

final static char UNICODE_START = '!';
final static char UNICODE_END = '~';

final static long DIFF = UNICODE_START - ASSIC_START;

public void testEncodeFormat() {
String res = ",!@#%&*()?;";
String des = ",!@#%&*()?;";
StringBuilder builder = new StringBuilder();
for (int i = 0; i < res.length(); i++) {
char c = res.charAt(i);
Log.e("TEST_UNICODE", "" + c);
if (isEXPANDUNICODE(c)) {
c = unicode2Assic(c);
Log.e("TEST_UNICODE", "translated:" + c);
}
builder.append(c);
}
assertEquals(des, builder.toString());
}

boolean isBasicASSIC(char c) {
return c >= ASSIC_START && c <= ASSIC_END;
}

boolean isEXPANDUNICODE(char c) {
return c >= UNICODE_START && c <= UNICODE_END;
}

char unicode2Assic(char c) {
return (char) (c - DIFF);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.Spanned;
import android.widget.TextView;

/**
* Created by chaobin on 11/18/15.
*/
public class CustomTextViewActivity extends AppCompatActivity {

static final String HTML_IMG = "...<img src='icon'/><br>";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.textview_layout);

((TextView) findViewById(R.id.tv_test)).setText(Html.fromHtml("219473892740218937498127472349823178461982376,40123463218。,74632781964923817649237816498723164982371649782364897231...<img src='icon'>", new Html.ImageGetter() {
Html.ImageGetter mImageGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable mIndicator = getResources().getDrawable(R.drawable.ic_arrow);
if (mIndicator != null) {
mIndicator.setBounds(0, 0, mIndicator.getIntrinsicWidth(), mIndicator.getIntrinsicHeight
());
Drawable drawable = getResources().getDrawable(R.drawable.ic_arrow);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight
());
}
return mIndicator;
return "icon".equals(source) ? drawable : null;
}
}, null));
};
Spanned text = Html.fromHtml(getResources().getString(R.string.sample_text_2) + HTML_IMG, mImageGetter, null);
((TextView) findViewById(R.id.tv_simple_text)).setText(text);
}
}
27 changes: 24 additions & 3 deletions sample/src/main/res/layout/textview_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,31 @@
app:expand_collapse_default="true"
app:expand_indicator="@drawable/ic_arrow"/>

<TextView
android:id="@+id/tv_test"

<net.avenwu.support.widget.ExTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@android:color/white"
android:maxLines="2"
android:padding="10dp"
android:text="@string/sample_text_2"
android:textColor="@android:color/black"
android:textSize="14sp"
android:lineSpacingMultiplier="1.2"
app:expand_collapse_default="true"
app:expand_indicator="@drawable/ic_arrow"/>

<TextView
android:id="@+id/tv_simple_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:lineSpacingMultiplier="1.2"
android:background="@android:color/white"
android:padding="10dp"
android:text="@string/sample_text_2"
android:textColor="@android:color/black"
android:textSize="14sp"/>

</LinearLayout>
2 changes: 2 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@
<string name="sample_text_digt">219473892740218937498127472349823178461982376401234632,1874632781964923817649237816498723164982371649782364897231</string>
<string name="sample_text_eng">ABCDKJBJCSABCJKABSCKJABCKJASBCKJABSCJKASBBBBBBBACBKJVKGVGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHSACASCASCASC</string>
<string name="sample_text">作为中国IT职业在线教育第一品牌 , 极客学院(隶属于北京优亿致远无线技术有限公司)始终致力于“让学习更有效”, 帮助IT从业者高效提升职业技能。极客学院坚持以“最快”的更新速度提供“最新”的课程 , 让IT从业者更有效地学习,让每个人都能通过极客学院享受高质量的职业教育。极客学院推出了中国第一个 FlappyBird、Swift、Apple Watch等相关课程 , 几乎垄断了所有最新的技术和最热的应用课程的首发。</string>
<string name="sample_text_2">12345678dslkhcjsdklvhdsgvhjkdsgjvlhdsjkvhlsdkgvlsdkgvlsdgvdshhcdks,
。,12345678,12345cdjscjdschjkdgscqy8678,12345678,12345678123456781234567812345678,12345678,1234567812345678</string>
</resources>
40 changes: 40 additions & 0 deletions support/src/main/java/net/avenwu/support/util/ChartSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.avenwu.support.util;

import android.util.Log;

/**
* Created by chaobin on 11/22/15.
*/
public class ChartSet {

static final String TAG = ChartSet.class.getCanonicalName();
final static char ANSI_START = '!';
final static char ANSI_END = '~';

final static char UNICODE_START = '!';
final static char UNICODE_END = '~';

final static long DIFF = UNICODE_START - ANSI_START;

public static String convertDBCS(CharSequence text) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
Log.e(TAG, "" + c);
if (isCharNeedConvert(c)) {
c = convert(c);
Log.e(TAG, "translated:" + c);
}
builder.append(c);
}
return builder.toString();
}

public static boolean isCharNeedConvert(char c) {
return c >= UNICODE_START && c <= UNICODE_END;
}

public static char convert(char c) {
return (char) (c - DIFF);
}
}
90 changes: 68 additions & 22 deletions support/src/main/java/net/avenwu/support/widget/ExTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
Expand All @@ -24,18 +25,22 @@
public class ExTextView extends TextView implements View.OnClickListener, ValueAnimator.AnimatorUpdateListener {

static final String TAG = ExTextView.class.getCanonicalName();
static final String HTML_IMG = "&nbsp;。。。&nbsp;<img src='icon'/>";
static final String HTML_IMG = "...<img src='icon'/>";
static final String HTML_NEW_LINE = "<br>";

int mMaxHeight;
int mCollapsedHeight;
int mMaxLine;
boolean isCollapsed;
boolean isLayout = false;
boolean isMeasured = false;

ValueAnimator mExpandAnimator;
CharSequence mCollapsedText;
CharSequence mFullText;

Drawable mIndicator;
OnClickListener mOuterListener;

public ExTextView(Context context) {
this(context, null);
Expand All @@ -52,43 +57,70 @@ public ExTextView(Context context, AttributeSet attrs) {
}
a.recycle();
reflectMaxLines();
setOnClickListener(this);
super.setOnClickListener(this);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mMaxHeight <= 0 || mCollapsedHeight <= 0) {
setMaxLines(Integer.MAX_VALUE);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mMaxHeight = getMeasuredHeight();
Log.d(TAG, "onMeasure");
if (!TextUtils.isEmpty(mFullText) && !isMeasured) {
Log.d(TAG, "onMeasure isCollapsed=" + isCollapsed);
if (isCollapsed) {
setMaxLines(Integer.MAX_VALUE);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mMaxHeight = getMeasuredHeight();

setMaxLines(mMaxLine);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mCollapsedHeight = getMeasuredHeight();
setMaxLines(mMaxLine);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mCollapsedHeight = getMeasuredHeight();
} else {
setMaxLines(mMaxLine);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mCollapsedHeight = getMeasuredHeight();

setMaxLines(Integer.MAX_VALUE);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mMaxHeight = getMeasuredHeight();
}
isMeasured = true;
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (!isLayout && isExpandable()) {
if (!TextUtils.isEmpty(mFullText) && (!isLayout && isExpandable())) {
Log.d(TAG, "onLayout reset text");
if (isCollapsed) {
if (mCollapsedText == null) {
int end = getLayout().getLineVisibleEnd(mMaxLine - 1);
CharSequence subString = mFullText.subSequence(0, end - 9);
Log.d(TAG, "substring=" + subString + HTML_IMG);
mCollapsedText = Html.fromHtml(subString + HTML_IMG, mImageGetter, null);
StringBuilder stringBuilder = new StringBuilder();
int start = 0;
// 由于中英文字符等排版问题断行具有不确定性,此处强行对缩略文本断行
for (int i = 0; i < mMaxLine; i++) {
int end = getLayout().getLineVisibleEnd(i);
String append;
if (i == mMaxLine - 1) {
end -= 3;
append = HTML_IMG;
} else {
append = HTML_NEW_LINE;
}
stringBuilder.append(mFullText.subSequence(start, end)).append(append);
start = end;
}
String subString = stringBuilder.toString();
Log.d(TAG, "substring=" + subString);
mCollapsedText = Html.fromHtml(subString, mImageGetter, null);
}
super.setText(mCollapsedText, reflectCurrentBufferType());
} else {
super.setText(mFullText, reflectCurrentBufferType());
}
isLayout = true;
requestLayout();
return;
} else {
super.onLayout(changed, left, top, right, bottom);
}
super.onLayout(changed, left, top, right, bottom);
}

private void reflectMaxLines() {
Expand Down Expand Up @@ -118,15 +150,25 @@ private BufferType reflectCurrentBufferType() {

@Override
public void setText(CharSequence text, BufferType type) {
mFullText = text;
mCollapsedText = null;
isLayout = false;

super.setText(text, type);
//view 复用的时候会重复绑定数据
if (TextUtils.isEmpty(text)) {
super.setText(text, type);
} else if (text.equals(mFullText)) {
super.setText(isCollapsed ? mCollapsedText : mFullText, reflectCurrentBufferType());
} else {
mFullText = text;
mCollapsedText = null;
isLayout = false;
isMeasured = false;
super.setText(mFullText, type);
}
}

@Override
public void onClick(View v) {
if (mOuterListener != null) {
mOuterListener.onClick(v);
}
if (!isExpandable()) {
return;
}
Expand Down Expand Up @@ -173,4 +215,8 @@ public Drawable getDrawable(String source) {
}
};

@Override
public void setOnClickListener(OnClickListener listener) {
mOuterListener = listener;
}
}

0 comments on commit f570a2b

Please sign in to comment.