产品分类

新闻资讯

联系我们

IT技术问答

当前位置:主页 > IT技术问答 >

android如果重写onDraw实现一个类似TextView可以显示表情和链接的控件(一)

2019-01-31 10:03

android如果重写onDraw实现一个类似TextView可以显示表情和链接的控件(一)

先看效果图:



IntroView有这样一个方法来加载字符串:
public class IntroView extends TextView {

	private ArrayList titleList;
	private int displayWidth = 0;
	private float displayHeight = 0;
	private float curLen = 0;
	private Bitmap starBmp;
	private Bitmap selectedBmp;
	private float posX = 0;
	private float posY = 0;
	private LinkInfo curInfo;//当前点击的Link对象
	private OnClickLinkListener Listener;
	
	private String mFaceType = MSG_FACE_TYPE;
	public static final String MSG_FACE_TYPE = "msgtype";
	public static final String STATUS_FACE_TYPE = "statustype";
	
	public IntroView(Context context) {
		super(context);
	}

	public IntroView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public IntroView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	
	public void setTitleList(ArrayList titleList){
		this.titleList = titleList;
		displayHeight = 0;
        requestLayout();
	}
}                                    
activity里这样来调用:
IntroView news_item_text = (IntroView)findViewById(R.id.news_item_text);
//不支持字符串里有
String s = "(#大笑)%$%$%3434343434343$%$%[email protected]$dfsfsfsdffds^15959224872)[email protected]&&fefrewafrewfjwio([email protected]()()()[email protected]";
news_item_text.setTitleList(ParseNewsInfoUtil.parseStr(s));
news_item_text.setOnClickLinkListener(this);
IntroView的主题思想是在onMeasure里的measureWidth和measureHeight时来获取ArrayList titleList每个LinkInfo的位置信息,并获取这个IntroView的高度和宽度,
然后onDraw的时候通过循环来绘制titleList的每个LinkInfo
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		try{
			int width = measureWidth(widthMeasureSpec);
			int height = measureHeight(heightMeasureSpec);
			setMeasuredDimension(width, height);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
 			
 private int measureWidth(int measureSpec) {
        int result = 0;
        
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int initialWidth = getPaddingLeft() + getPaddingRight();
        int width = initialWidth;
        int maxWidth = 0;
        
        TextPaint tempPaint = null;

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            if (tempPaint == null) {
                tempPaint = new TextPaint();
                tempPaint.setStyle(Style.FILL);
                tempPaint.setAntiAlias(true);
                tempPaint.setTextSize(getTextSize());
            }
            
            if (titleList != null && titleList.size() > 0) {
                maxWidth = specSize;
                
                int size = titleList.size();
forLable:                
                for (int i = 0; i < size; i++) {
                    LinkInfo info = titleList.get(i);
                    
                    if (info.isFace()) {
                    	Bitmap faceBmp = null;
        				if(mFaceType == MSG_FACE_TYPE) {
        					faceBmp = MessageFaceModel.getInstance().getFaceIcon(info.getContent());
        				}
                        if (faceBmp != null) {
                            int wSize = faceBmp.getWidth() + 4;
                            if (width + wSize >= maxWidth) { //这里表示已经计算的宽度大于控件的宽度,那就返回maxWidth就可以了
                                width = maxWidth;
                                break forLable;
                            }
                            width += wSize;
                        }
                        continue;
                    }
                    
                    String text = info.getContent();
                    text = text.replaceAll("
", " "); //因为该控件不支持
,所以把这个换成空格
                    if (!TextUtils.isEmpty(text)) {
                        float wSize = tempPaint.measureText(text);
                        if (width + wSize >= maxWidth) {
                            width = maxWidth;
                            break forLable;
                        }
                        width += wSize;
                    }
                    
                }
            }
            
            result = width;
        }

        displayWidth = result;
        return result;
    }


                                                                                

http://www.bkjia.com/Androidjc/862895.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/862895.htmlTechArticleandroid如果重写onDraw实现一个类似TextView可以显示表情和链接的控件(一) 先看效果图: 写一个超连接支持的对象: /**作为超连接显示的对象*/...

本文源自: 环亚娱乐

Copyright © 2005-2017 http://www.rheroesinc.com 环亚娱乐_环亚娱乐AG旗舰厅_环亚娱乐AG最佳线路_腾讯娱乐版权所有

扫一扫,加关注