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

Android实现文字滚动效果

程序员文章站 2024-03-31 14:33:34
android 实现文字滚动效果,自己写了个timer小计时器,textview文字上下翻动效果: public class autotextview ext...

android 实现文字滚动效果,自己写了个timer小计时器,textview文字上下翻动效果:

public class autotextview extends textswitcher implements
  viewswitcher.viewfactory {

 private float mheight;
 private context mcontext;
 //minup,moutup分别构成向下翻页的进出动画
 private rotate3danimation minup;
 private rotate3danimation moutup;
 
 //mindown,moutdown分别构成向下翻页的进出动画
 private rotate3danimation mindown;
 private rotate3danimation moutdown;
 
 public autotextview(context context) {
  this(context, null);
  // todo auto-generated constructor stub
 }

 public autotextview(context context, attributeset attrs) {
  super(context, attrs);
  // todo auto-generated constructor stub
  typedarray a = context.obtainstyledattributes(attrs, r.styleable.auto3d);
  mheight = a.getdimension(r.styleable.auto3d_textsize, 16);
  a.recycle();
  mcontext = context;
  init();
 }

 private void init() {
  // todo auto-generated method stub
  setfactory(this);
  minup = createanim(-90, 0 , true, true);
  moutup = createanim(0, 90, false, true);
  mindown = createanim(90, 0 , true , false);
  moutdown = createanim(0, -90, false, false);
  //textswitcher主要用于文件切换,比如 从文字a 切换到 文字 b,
  //setinanimation()后,a将执行inanimation,
  //setoutanimation()后,b将执行outanimation
  setinanimation(minup);
  setoutanimation(moutup);
 }
 
 private rotate3danimation createanim(float start, float end, boolean turnin, boolean turnup){
  final rotate3danimation rotation = new rotate3danimation(start, end, turnin, turnup);
  rotation.setduration(800);
  rotation.setfillafter(false);
  rotation.setinterpolator(new accelerateinterpolator());
  return rotation;
 }

 //这里返回的textview,就是我们看到的view
 @override
 public view makeview() {
  // todo auto-generated method stub
  textview t = new textview(mcontext);
  t.setgravity(gravity.center);
  t.settextsize(16);
  t.setmaxlines(1);
  t.settextcolor(mcontext.getresources().getcolor(r.color.textcolor));
  return t;
 }
 //定义动作,向下滚动翻页
 public void previous(){
  if(getinanimation() != mindown){
   setinanimation(mindown);
  }
  if(getoutanimation() != moutdown){
   setoutanimation(moutdown);
  }
 }
 //定义动作,向上滚动翻页
 public void next(){
  if(getinanimation() != minup){
   setinanimation(minup);
  }
  if(getoutanimation() != moutup){
   setoutanimation(moutup);
  }
 }
 
 class rotate3danimation extends animation {
   private final float mfromdegrees;
   private final float mtodegrees;
   private float mcenterx;
   private float mcentery;
   private final boolean mturnin;
   private final boolean mturnup;
   private camera mcamera;

   public rotate3danimation(float fromdegrees, float todegrees, boolean turnin, boolean turnup) {
    mfromdegrees = fromdegrees;
    mtodegrees = todegrees;
    mturnin = turnin;
    mturnup = turnup;
   }

   @override
   public void initialize(int width, int height, int parentwidth, int parentheight) {
    super.initialize(width, height, parentwidth, parentheight);
    mcamera = new camera();
    mcentery = getheight() / 2;
    mcenterx = getwidth() / 2;
   }
   
   @override
   protected void applytransformation(float interpolatedtime, transformation t) {
    final float fromdegrees = mfromdegrees;
    float degrees = fromdegrees + ((mtodegrees - fromdegrees) * interpolatedtime);

    final float centerx = mcenterx ;
    final float centery = mcentery ;
    final camera camera = mcamera;
    final int derection = mturnup ? 1: -1;

    final matrix matrix = t.getmatrix();

    camera.save();
    if (mturnin) {
     camera.translate(0.0f, derection *mcentery * (interpolatedtime - 1.0f), 0.0f);
    } else {
     camera.translate(0.0f, derection *mcentery * (interpolatedtime), 0.0f);
    }
    camera.rotatex(degrees);
    camera.getmatrix(matrix);
    camera.restore();

    matrix.pretranslate(-centerx, -centery);
    matrix.posttranslate(centerx, centery);
   }
 }

demo下载链接:http://xiazai.jb51.net/201611/yuanma/androidtextview(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。