我们来看一下效果:
文字会慢慢从右边向左边滚动,为了实现这个效果,可以用timer来实现
每次timer回调的时候改变文字左边的位置
ondraw()绘制text的代码:
int len = stateList.size(); float curLen = x; for (int i = 0; i < len; i++) { LinkInfo info = stateList.get(i); if (info.isFace()) { Bitmap faceBmp = StateFaceModel.getInstance() .getSmallFaceIcon(info.getContent()); int xLen = faceBmp.getWidth(); txtCanvas.drawBitmap(faceBmp, curLen + 2, 0, txtPaint); curLen += xLen + 4; continue; } String strContent = info.getContent(); strContent = strContent.replaceAll("\n", " "); float xLen = txtPaint.measureText(strContent); txtCanvas.drawText(strContent, curLen, y, txtPaint); curLen += xLen; } canvas.drawBitmap(txtBmp, left, 0, paint);
我们看到关键就是curLen这个变量,每次起始点的坐标都是curLen
每次timer回调的时候我们改变一个step的变量:
public void scrollText() { if (!isStarting) { return; } invalidate(); if (viewWidth < textLength) { step += 0.5; if (step > temp_view_plus_two_text_length) { step = textLength; } } }
onDraw的完成代码:
@Override public void onDraw(Canvas canvas) { try { //初始化一个txtCanvas,里面放txtBmp的bitmap setTxtBmp(); if (txtBmp == null) { return; } Paint txtPaint = new Paint(); txtPaint.setColor(Color.WHITE); txtPaint.setStyle(Style.FILL); txtCanvas.drawRect(0, 0, txtBmp.getWidth(), txtBmp.getHeight(), txtPaint); txtPaint.setAntiAlias(true); txtPaint.setStyle(Style.STROKE); txtPaint.setTextSize(getTextSize()); txtPaint.setColor(getCurrentTextColor()); float x = 0; if (viewWidth < textLength) { x = temp_view_plus_text_length - step; } int len = stateList.size(); float curLen = x; for (int i = 0; i < len; i++) { LinkInfo info = stateList.get(i); if (info.isFace()) { Bitmap faceBmp = StateFaceModel.getInstance() .getSmallFaceIcon(info.getContent()); int xLen = faceBmp.getWidth(); txtCanvas.drawBitmap(faceBmp, curLen + 2, 0, txtPaint); curLen += xLen + 4; continue; } String strContent = info.getContent(); strContent = strContent.replaceAll("\n", " "); float xLen = txtPaint.measureText(strContent); txtCanvas.drawText(strContent, curLen, y, txtPaint); curLen += xLen; } canvas.drawBitmap(txtBmp, left, 0, paint); } catch (Exception ex) { ex.printStackTrace(); } }
Activity中这样来调用:
public class NewsActivity extends Activity implements OnClickLinkListener{ private ScrollText tvState; private ImageView ivState; private NewsModel newsModel = NewsModel.getInstance(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.news_activity); StateFaceModel.getInstance().init(this); tvState = (ScrollText)findViewById(R.id.news_statustxt); initStatus(); } private void initStatus(){ ivState = (ImageView) findViewById(R.id.news_statusinput); String s = "厦门这边的沙茶面很好吃,尤其是民族路上的一家很有名的店(#f2)"; newsModel.setStatus(s); String strModel = newsModel.getStatus(); setStateText(strModel); } private void setStateText(String strModel){ if(!TextUtils.isEmpty(strModel)){ tvState.setStateList(newsModel.getStateList()); tvState.setText(strModel); tvState.init(getWindowManager(), handler); tvState.startScroll(); tvState.start(); } } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case ScrollText.TEXT_TIMER: if(tvState != null){ tvState.scrollText(); } break; default: break; } } };