我们来看一下效果:
文字会慢慢从右边向左边滚动,为了实现这个效果,可以用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;
}
}
};
點擊查看更多內(nèi)容
為 TA 點贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
