//XRTextView类

package rong.android.test;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class XRTextView extends TextView{
private final String namespace = "rong.android.TextView";
private String text;
private float textSize;
private float paddingLeft;
private float paddingRight;
private float marginLeft;
private float marginRight;
private int textColor;
private JSONArray colorIndex;
private Paint paint1 = new Paint();
private Paint paintColor = new Paint();
private float textShowWidth;
private float Spacing = 0;
private float LineSpacing = 1.3f;//行与行的间距

public XRTextView(Context context, AttributeSet attrs) {
super(context, attrs);
text = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/android", "text");
textSize = attrs.getAttributeIntValue(namespace, "textSize", 25);//字体大小
textColor = attrs.getAttributeIntValue(namespace, "textColor",Color.BLUE);//字体颜色
paddingLeft = attrs.getAttributeIntValue(namespace, "paddingLeft", 0);
paddingRight = attrs.getAttributeIntValue(namespace, "paddingRight", 0);
marginLeft = attrs.getAttributeIntValue(namespace, "marginLeft", 0);
marginRight = attrs.getAttributeIntValue(namespace, "marginRight", 0);
paint1.setTextSize(textSize);
paint1.setColor(textColor);
paint1.setAntiAlias(true);
paintColor.setAntiAlias(true);
paintColor.setTextSize(textSize);
paintColor.setColor(Color.BLUE);
}
public XRTextView(Context context, float textSize, int textColor, float paddingLeft, float paddingRight, float marginLeft, float marginRight){
super(context);
this.textSize = textSize;
this.textColor = textColor;
this.paddingLeft = paddingLeft;
this.paddingRight = paddingRight;
this.marginLeft = marginLeft;
this.marginRight = marginRight;
paint1.setTextSize(textSize);
paint1.setColor(textColor);
paint1.setAntiAlias(true);
paintColor.setAntiAlias(true);
paintColor.setTextSize(textSize);
paintColor.setColor(Color.BLUE);
}


public JSONArray getColorIndex() {
return colorIndex;
}

public void setColorIndex(JSONArray colorIndex) {
this.colorIndex = colorIndex;
}
/**
* 传入一个索引,判断当前字是否被高亮
* @param index
* @return
* @throws JSONException
*/
public boolean isColor(int index) throws JSONException{
if(colorIndex == null){
return false;
}
for(int i = 0 ; i < colorIndex.length() ; i ++){
JSONArray array = colorIndex.getJSONArray(i);
int start = array.getInt(0);
int end = array.getInt(1)-1;
if(index >= start && index <= end){
return true;
}

}


return false;
}

@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
View view=(View)this.getParent();
textShowWidth=view.getMeasuredWidth()-paddingLeft - paddingRight - marginLeft - marginRight;
int lineCount = 0;

text = this.getText().toString();//.replaceAll("\n", "\r\n");
if(text==null)return;
char[] textCharArray = text.toCharArray();
// 已绘的宽度
float drawedWidth = 0;
float charWidth;
for (int i = 0; i < textCharArray.length; i++) {
charWidth = paint1.measureText(textCharArray, i, 1);

if(textCharArray[i]=='\n'){
lineCount++;
drawedWidth = 0;
continue;
}
if (textShowWidth - drawedWidth < charWidth) {
lineCount++;
drawedWidth = 0;
}
boolean color = false;
try {
color = isColor(i);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

if(color){

canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth,
(lineCount + 1) * textSize * LineSpacing, paintColor);
}else{

canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth,
(lineCount + 1) * textSize * LineSpacing, paint1);
}
if(textCharArray[i] > 127 && textCharArray[i] != '、' && textCharArray[i] != ',' && textCharArray[i] != '。' && textCharArray[i] != ':' && textCharArray[i] != '!'){
drawedWidth += charWidth + Spacing;

}else{
drawedWidth += charWidth;

}
}
setHeight((int) ((lineCount + 1) * (int) textSize * LineSpacing + 10));
}
public float getSpacing() {
return Spacing;
}
public void setSpacing(float spacing) {
Spacing = spacing;
}
public float getMYLineSpacing() {
return LineSpacing;
}
public void setMYLineSpacing(float lineSpacing) {
LineSpacing = lineSpacing;
}
public float getMYTextSize() {
return textSize;
}
public void setMYTextSize(float textSize) {
this.textSize = textSize;
paint1.setTextSize(textSize);
paintColor.setTextSize(textSize);
}


}

//MainActivity类

package rong.android.test;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {
private XRTextView xrtextview = null;
private TextView textview = null;
private String content = "abcdefgABCDEF我要你lfwjkdfl;skjf asljkflskjfls;kjfsljfwfisdlfjsllkjsdfjlskjf546132s1f3sd4f31s3dffslfksjdfljlsadkjflsajdf sdfjklsajdflsa;jdfls 的!@#$%^&*()_";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xrtextview = (XRTextView) this.findViewById(R.id.mytextview_tv);
xrtextview.setText(content);
textview = (TextView) this.findViewById(R.id.mytextview_tv1);
textview.setText(content);
}

}

//布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<rong.android.test.XRTextView
android:id="@+id/mytextview_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/mytextview_tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black" />

</LinearLayout>

//源码可以去我的百度网盘下载

http://t.cn/zjSVQ0Z

更多相关文章

  1. android 根据TextView宽度 自动缩小字体大小 以适应其宽度
  2. Android(安卓)加载图片并存放在缓存中
  3. Android(安卓)单独控件实现不同字体大小、不同颜色
  4. Android(安卓)2-10 RecyclerView
  5. android style
  6. Android预定义样式
  7. android gridview 行高设置
  8. android style
  9. ANDROID 【GRIDVIEW】 DETAILED ANNOTATION

随机推荐

  1. php吧字符串直接转换成数组处理
  2. 为什么我对JSON对象的AJAX调用会返回其特
  3. PHP print_r 转换/还原为数组
  4. 如何在没有任何扩展名的php中保存图像
  5. thinkphp框架里怎么用linux的crontab写ph
  6. MySQL或PHP动态地将行转换为列
  7. 如何搜索/汇总我的PHP结果?
  8. PHP实现简单的学生信息管理系统(web版)
  9. 即使system()中的命令完成,PHP system()也会挂
  10. 一套PHP做app接口的解决方案