欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android基于TextView实现的跑马灯效果实例

程序员文章站 2024-02-25 17:35:09
本文实例讲述了android基于textview实现的跑马灯效果。分享给大家供大家参考,具体如下: package sweet.venst.act; impor...

本文实例讲述了android基于textview实现的跑马灯效果。分享给大家供大家参考,具体如下:

package sweet.venst.act;
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.unsupportedencodingexception;
import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.os.parcel;
import android.os.parcelable;
import android.util.attributeset;
import android.util.displaymetrics;
import android.view.display;
import android.view.view;
import android.view.windowmanager;
import android.view.view.measurespec;
import android.view.view.onclicklistener;
import android.widget.textview;
public class autoscrolltextview extends textview {
 public final static string tag = autoscrolltextview.class.getsimplename();
 private float textlength = 0f;// 文本长度
 private int viewwidth = 0;
 private int viewhight = 300;
 private float step = 0f;// 文字的横坐标
 private float y = 0f;// 文字的纵坐标
 private float temp_view_plus_text_length = 0.0f;// 用于计算的临时变量
 private float temp_view_plus_two_text_length = 0.0f;// 用于计算的临时变量
 public boolean isstarting = false;// 是否开始滚动
 private paint paint = null;// 绘图样式
 private string text = "";// 文本内容
 public autoscrolltextview(context context)
 {
 super(context);
 }
 public autoscrolltextview(context context, attributeset attrs)
 {
 super(context, attrs);
 }
 public autoscrolltextview(context context, attributeset attrs, int defstyle)
 {
 super(context, attrs, defstyle);
 }
 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec)
 {
 // todo auto-generated method stub
 super.onmeasure(widthmeasurespec, heightmeasurespec);
 int measurewidth = measurewidth(widthmeasurespec);
 int measurehight = measureheight(heightmeasurespec);
 // int measurewidth = 1024;
 // int measurehight = 300;
 viewwidth = measurewidth;
 viewhight = measurehight;
 setmeasureddimension(viewwidth, viewhight);
 }
 private int measurewidth(int widthmeasurespec)
 {
 int specmode = measurespec.getmode(widthmeasurespec);
 int specsize = measurespec.getsize(widthmeasurespec);
 if (specmode == measurespec.unspecified) {
  return viewwidth;
 }
 else  {
   return viewwidth;
  }
 }
 private int measureheight(int widthmeasurespec) {
 int specmode = measurespec.getmode(widthmeasurespec);
 int specsize = measurespec.getsize(widthmeasurespec);
 if (specmode == measurespec.unspecified) {
  return viewhight;
 } else {
  return viewhight;
 }
 }
 public void setscrolltext(string text)
 {
 this.text = text;
 inittextlength(text);
 // invalidate();
 }
 public void init(context cx) {
 paint = getpaint();
 paint.settextsize(40);
 paint.setcolor(color.white);
 text = gettext().tostring();
 textlength = paint.measuretext(text);
 viewwidth = getwidth();
 if (viewwidth == 0) {
  displaymetrics dm = new displaymetrics();
  dm = cx.getapplicationcontext().getresources().getdisplaymetrics();
  viewwidth = dm.widthpixels;
 }
 step = textlength;
 temp_view_plus_text_length = viewwidth + textlength;
 temp_view_plus_two_text_length = viewwidth + textlength * 2;
 y = gettextsize() + getpaddingtop();
 }
 private void inittextlength(string text) {
 textlength = paint.measuretext(text);
 step = textlength;
 temp_view_plus_text_length = viewwidth + textlength;
 temp_view_plus_two_text_length = viewwidth + textlength * 2;
 }
 @override
 public parcelable onsaveinstancestate() {
 parcelable superstate = super.onsaveinstancestate();
 savedstate ss = new savedstate(superstate);
 ss.step = step;
 ss.isstarting = isstarting;
 return ss;
 }
 @override
 public void onrestoreinstancestate(parcelable state) {
 if (!(state instanceof savedstate)) {
  super.onrestoreinstancestate(state);
  return;
 }
 savedstate ss = (savedstate) state;
 super.onrestoreinstancestate(ss.getsuperstate());
 step = ss.step;
 isstarting = ss.isstarting;
 }
 public static class savedstate extends basesavedstate {
 public boolean isstarting = false;
 public float step = 0.0f;
 savedstate(parcelable superstate) {
  super(superstate);
 }
 @override
 public void writetoparcel(parcel out, int flags) {
  super.writetoparcel(out, flags);
  out.writebooleanarray(new boolean[] { isstarting });
  out.writefloat(step);
 }
 public static final parcelable.creator<savedstate> creator = new parcelable.creator<savedstate>() {
  public savedstate[] newarray(int size) {
  return new savedstate[size];
  }
  public savedstate createfromparcel(parcel in) {
  return new savedstate(in);
  }
 };
 private savedstate(parcel in) {
  super(in);
  boolean[] b = null;
  in.readbooleanarray(b);
  if (b != null && b.length > 0)
  isstarting = b[0];
  step = in.readfloat();
 }
 }
 public void startscroll() {
 isstarting = true;
 // invalidate();
 }
 public void stopscroll() {
 isstarting = false;
 // invalidate();
 }
 public void ondraw(canvas canvas) {
 canvas.save();
 canvas.drawtext(text, temp_view_plus_text_length - step, y, paint);
 if (!isstarting) {
  return;
 }
 step += 3.2;// 0.5为文字滚动速度。
 if (step > temp_view_plus_two_text_length)
  step = textlength;
 canvas.restore();
// invalidate();
 postinvalidate();
 }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。